Shell scripting in macOS – Part 3 Condition checks

This article is a continuation of the previous article. We will be taking the previous script and using it to build on the concepts we will learning in this article.

Performing tasks conditionally

So far our script has been performing tasks uninterrupted one after the other. But often times you will come across a situation where you need to perform some checks before going ahead.

The main reason why we would want to perform checks is to make sure that certain criteria are met or if certain resources are present.

Only if these conditions are satisfied will we proceed ahead. Or take an alternative course of action incase the condition isn’t met.

We can find all these checks in the man page for the test command.

Let us look at some of those checks.

Test operations

You can run the following command to view all the operations possible.

man test

There are different comparison operations possible.

  • The – followed by a letter and then the file name allows us to check for different aspects of a file. Such as if it exists, whether it is a directory and more…
  • We can even compare files with each other.
  • We can compare strings.
  • And we can compare numbers.

Conditional code

Now that we have seen the different kinds of condition checks available. Let us explore how we can use the condition checks.

If statement

The if statement has various forms. We will look at the simplest one first.

if [[ -d "$HOME/Applications" ]]; then
	echo "The applications folder exists in the home folder."
fi

If else statement

If-elif-else statement

Switch on case statement

Modify our code

We will be adding checks to make sure that the arguments passed in contain values. We will also check to see if the folders exist before trying to create them.

#!/bin/zsh

echo "$(date) Running script $0 to create folders."

TOOLS_FOLDER="Tools"
REPORTS_FOLDER="Reports"
HELP_FOLDER="Help"

if [[ $1 != "" ]]; then
	TOOLS_FOLDER=$1
fi

if [[ $2 != "" ]]; then
	REPORTS_FOLDER=$2
fi

if [[ $3 != "" ]]; then
	HELP_FOLDER=$3
fi

TOOLS_FOLDER_CREATED=".$TOOLS_FOLDER-FolderCreated"
REPORTS_FOLDER_CREATED=".$REPORTS_FOLDER-FolderCreated"
HELP_FOLDER_CREATED=".$HELP_FOLDER-FolderCreated"

TODAY=$(date)
PATH_TO_LOG="$HOME/Library/Logs/folderCreator_log_v1-1.log"

echo "$(date) Starting" >> $PATH_TO_LOG

cd $HOME

echo "$(date) Creating folders: $TOOLS_FOLDER, $REPORTS_FOLDER, $HELP_FOLDER" >> $PATH_TO_LOG
if [[ -d $TOOLS_FOLDER ]]; then
	echo "$(date) Not creating $TOOLS_FOLDER as it already exists." >> $PATH_TO_LOG
else
	echo "$(date) Creating $TOOLS_FOLDER" >> $PATH_TO_LOG
	mkdir $TOOLS_FOLDER
fi

if [[ -d $REPORTS_FOLDER ]]; then
	echo "$(date) Not creating $REPORTS_FOLDER as it already exists." >> $PATH_TO_LOG
else
	echo "$(date) Creating $REPORTS_FOLDER" >> $PATH_TO_LOG
	mkdir $REPORTS_FOLDER
fi

if [[ -d $HELP_FOLDER ]]; then
	echo "$(date) Not creating $HELP_FOLDER as it already exists." >> $PATH_TO_LOG
else
	echo "$(date) Creating $HELP_FOLDER" >> $PATH_TO_LOG
	mkdir $HELP_FOLDER
fi

echo "$(date) Creating hidden file for $TOOLS_FOLDER folder." >> $PATH_TO_LOG
cd $TOOLS_FOLDER
touch $TOOLS_FOLDER_CREATED
cd ..

echo "$(date) Creating hidden file for $REPORTS_FOLDER folder." >> $PATH_TO_LOG
cd $REPORTS_FOLDER
touch $REPORTS_FOLDER_CREATED
cd ..

echo "$(date) Creating hidden file for $HELP_FOLDER folder." >> $PATH_TO_LOG
cd $HELP_FOLDER
touch $HELP_FOLDER_CREATED
cd ..

echo "$(date) Task completed. Have a nice day!"

Your completed code should look like.

#!/bin/zsh
echo "$(date) Running script $0 to create folders."
TOOLS_FOLDER="Tools"
REPORTS_FOLDER="Reports"
HELP_FOLDER="Help"
if [[ $1 != "" ]]; then
TOOLS_FOLDER=$1
fi
if [[ $2 != "" ]]; then
REPORTS_FOLDER=$2
fi
if [[ $3 != "" ]]; then
HELP_FOLDER=$3
fi
TOOLS_FOLDER_CREATED=".$TOOLS_FOLDER-FolderCreated"
REPORTS_FOLDER_CREATED=".$REPORTS_FOLDER-FolderCreated"
HELP_FOLDER_CREATED=".$HELP_FOLDER-FolderCreated"
TODAY=$(date)
PATH_TO_LOG="$HOME/Library/Logs/folderCreator_log_v1-1.log"
echo "$(date) Starting" >> $PATH_TO_LOG
cd $HOME
echo "$(date) Creating folders: $TOOLS_FOLDER, $REPORTS_FOLDER, $HELP_FOLDER" >> $PATH_TO_LOG
if [[ -d $TOOLS_FOLDER ]]; then
echo "$(date) Not creating $TOOLS_FOLDER as it already exists." >> $PATH_TO_LOG
else
echo "$(date) Creating $TOOLS_FOLDER" >> $PATH_TO_LOG
mkdir $TOOLS_FOLDER
fi
if [[ -d $REPORTS_FOLDER ]]; then
echo "$(date) Not creating $REPORTS_FOLDER as it already exists." >> $PATH_TO_LOG
else
echo "$(date) Creating $REPORTS_FOLDER" >> $PATH_TO_LOG
mkdir $REPORTS_FOLDER
fi
if [[ -d $HELP_FOLDER ]]; then
echo "$(date) Not creating $HELP_FOLDER as it already exists." >> $PATH_TO_LOG
else
echo "$(date) Creating $HELP_FOLDER" >> $PATH_TO_LOG
mkdir $HELP_FOLDER
fi
echo "$(date) Creating hidden file for $TOOLS_FOLDER folder." >> $PATH_TO_LOG
cd $TOOLS_FOLDER
touch $TOOLS_FOLDER_CREATED
cd ..
echo "$(date) Creating hidden file for $REPORTS_FOLDER folder." >> $PATH_TO_LOG
cd $REPORTS_FOLDER
touch $REPORTS_FOLDER_CREATED
cd ..
echo "$(date) Creating hidden file for $HELP_FOLDER folder." >> $PATH_TO_LOG
cd $HELP_FOLDER
touch $HELP_FOLDER_CREATED
cd ..
echo "$(date) Task completed. Have a nice day!"

Video

Download

You can download the completed script from here.

Shell scripting in macOS – Part 2: Managing information

This article is a continuation of the previous article. We will be taking the previous script and using it to build on the concepts we will learning in this article.

Using Variables to store information

First up we will look at variable. Variables are containers that can hold information. The biggest advantage of this is the fact that we can use information in our tasks simply by reusing the variable it is stored in. This means if there is any change at a later date, then we only have to change the value in the variable. 

So, in the future, if there is a need to modify the information, we only have a single point of change to make. This greatly aids  in the ease of maintenance of the code.

It also makes the script more readable.

NOTE: The value of a variable can be changed at a later point of time within the script. 

Creating variables is very easy. You simply declare a name and assign it a value using the = operator. For example, if we are going to be using the path to the logs folder then storing it in a variable called PATH_TO_LOGS makes sense. We would then follow it up with the = sign and follow that up with the path in quotes. 

PATH_TO_LOGS=“/Library/Logs/“

To use this variable in a command we would simple callout the name with the $ symbol prefixed before it. 

echo $PATH_TO_LOGS

The $ symbol is necessary to access the value being held by the container.

While declaring variables try to use names which explain the purpose of the variable.

Built in variables

We can see that it is very easy to define our own variables. However, we are not restricted to creating our own variables. The system provides us with predefined variables. These give us access to useful information such as:

  • Path to the current user’s home folder.
  • The shell interpreter being used.
  • The currently logged in user name. 

We can get the complete list of commands with the help of the printenv command.

printenv

How about using these variables? Well, we will use it the same way we would use our own variables. Just prefix the $ symbol before the variable name. 

echo "The path to the home folder is $HOME"

Let us update the script from the previous article.

#!/bin/zsh

echo "Running script to create folders."

TOOLS_FOLDER="Tools"
REPORTS_FOLDER="Reports"
HELP_FOLDER="Help"

TOOLS_FOLDER_CREATED=".$TOOLS_FOLDER-FolderCreated"
REPORTS_FOLDER_CREATED=".$REPORTS_FOLDER-FolderCreated"
HELP_FOLDER_CREATED=".$HELP_FOLDER-FolderCreated"

cd $HOME

echo "Creating folders: $TOOLS_FOLDER, $REPORTS_FOLDER, $HELP_FOLDER"
mkdir $TOOLS_FOLDER
mkdir $REPORTS_FOLDER
mkdir $HELP_FOLDER

echo "Creating hidden file for $TOOLS_FOLDER folder."
cd $TOOLS_FOLDER
touch $TOOLS_FOLDER_CREATED
cd ..

echo "Creating hidden file for $REPORTS_FOLDER folder."
cd $REPORTS_FOLDER
touch $REPORTS_FOLDER_CREATED
cd ..

echo "Creating hidden file for $HELP_FOLDER folder."
cd $HELP_FOLDER
touch $HELP_FOLDER_CREATED
cd ..

echo "Task completed. Have a nice day!"

Capturing command output

Now that we have seen how variables can be created and used, then next logical step is to use them to store the outcome of a command. Why would we need to do this? Let us suppose that a command returns the path to a folder and we would like to perform multiple tasks on this folder. We can simply save the path in a variable and then use the variable across the script. 

If storing the result of the command in a variable wasn’t possible then we would have to execute the command over and over again every time we needed the result.

But before we store the outcome of the command we first need to understand how we can capture the output of a command itself. This is done with the help of command substitution. The command to be executed is placed within the $ symbol followed by parentheses.

So to store it in a variable we would just place the command we would just place this on he right hand side of the = sign. For example, if we wanted to store today’s date we would use the date command placed within the $() on the right hand side of the = sign. On the left hand side of the = sign would be the name of our variable.

TODAY=$(date)

There is an older way of doing the same thing, instead of using the $() the command would be placed within 2 back ticks.

TODAY=`date`

Writing to files

While it is useful to store information within variables there are some limitation with this. Sometimes we would like to store our data outside the script for example on some other file. The advantage with this approach is that it allows us to access the information across multiple invocations of the script. 

The way we write to a file is by redirecting the output of the command from standard output to a file. There are 2 operators that help us with this.

The redirect operator with a single angle bracket will write the contents to a file. This will replace the existing content fo the file.

echo "Hello, World!" > /Users/Shared/message.txt

The redirect operator with 2 angle brackets will also write contents to a file. But this will append or add the existing content. 

echo "Hello, World!" >> /Users/Shared/message.txt

Depending on what you want you can use one of the 2 approaches. 

Logging events taking place in the script

A log file is used to note done certain events being performed by an app, script, process, or any task. It is a very useful troubleshooting tool. This would be a nice feature to add to our script. We can log the different events that are taking place. To do this we will use the same redirect operator to write to a file.

Log files are typically stored in one of two locations in macOS:

  • ~/Library/Logs/
  • /Library/Logs

For our demo we will store it in the ~/Library/Logs/ folder. This makes sense because our script will be making changes to a user’s home folder. So ideally, the log file should also stay in the user’s home folder.

The way we will generate our log file is by redirecting the output of the echo command to our file.

echo "Hello, World!" >> ~/Library/Logs/folderCreator_log_v1-1.log

So all the echo statements we have will be modified to redirect to the log. Additionally, we will use command substitution to include the date and time in out message. Let us modify the script above to reflect these new changes.

#!/bin/zsh

echo "$(date) Running script to create folders."

TOOLS_FOLDER="Tools"
REPORTS_FOLDER="Reports"
HELP_FOLDER="Help"

TOOLS_FOLDER_CREATED=".$TOOLS_FOLDER-FolderCreated"
REPORTS_FOLDER_CREATED=".$REPORTS_FOLDER-FolderCreated"
HELP_FOLDER_CREATED=".$HELP_FOLDER-FolderCreated"

TODAY=$(date)
PATH_TO_LOG="$HOME/Library/Logs/folderCreator_log_v1-1.log"

echo "$(date) Starting" >> $PATH_TO_LOG

cd $HOME

echo "$(date) Creating folders: $TOOLS_FOLDER, $REPORTS_FOLDER, $HELP_FOLDER" >> $PATH_TO_LOG
mkdir $TOOLS_FOLDER
mkdir $REPORTS_FOLDER
mkdir $HELP_FOLDER

echo "$(date) Creating hidden file for $TOOLS_FOLDER folder." >> $PATH_TO_LOG
cd $TOOLS_FOLDER
touch $TOOLS_FOLDER_CREATED
cd ..

echo "$(date) Creating hidden file for $REPORTS_FOLDER folder." >> $PATH_TO_LOG
cd $REPORTS_FOLDER
touch $REPORTS_FOLDER_CREATED
cd ..

echo "$(date) Creating hidden file for $HELP_FOLDER folder." >> $PATH_TO_LOG
cd $HELP_FOLDER
touch $HELP_FOLDER_CREATED
cd ..

echo "$(date) Task completed. Have a nice day!"

Passing information to a script

While storing information and capturing information within a script is useful. It is also useful to have the ability to give information to a script at the time of running the script. This allows the user of the script to have greater control over the end result or outcome. 

The information that is passed into the script is store in predefined variables known as positional variables. They are named $0, $1, $2 and onwards. Let us modify the script to use these variables.

#!/bin/zsh

echo "$(date) Running script $0 to create folders."

TOOLS_FOLDER=$1
REPORTS_FOLDER=$2
HELP_FOLDER=$3

TOOLS_FOLDER_CREATED=".$TOOLS_FOLDER-FolderCreated"
REPORTS_FOLDER_CREATED=".$REPORTS_FOLDER-FolderCreated"
HELP_FOLDER_CREATED=".$HELP_FOLDER-FolderCreated"

TODAY=$(date)
PATH_TO_LOG="$HOME/Library/Logs/folderCreator_log_v1-1.log"

echo "$(date) Starting" >> $PATH_TO_LOG

cd $HOME

echo "$(date) Creating folders: $TOOLS_FOLDER, $REPORTS_FOLDER, $HELP_FOLDER" >> $PATH_TO_LOG
mkdir $TOOLS_FOLDER
mkdir $REPORTS_FOLDER
mkdir $HELP_FOLDER

echo "$(date) Creating hidden file for $TOOLS_FOLDER folder." >> $PATH_TO_LOG
cd $TOOLS_FOLDER
touch $TOOLS_FOLDER_CREATED
cd ..

echo "$(date) Creating hidden file for $REPORTS_FOLDER folder." >> $PATH_TO_LOG
cd $REPORTS_FOLDER
touch $REPORTS_FOLDER_CREATED
cd ..

echo "$(date) Creating hidden file for $HELP_FOLDER folder." >> $PATH_TO_LOG
cd $HELP_FOLDER
touch $HELP_FOLDER_CREATED
cd ..

echo "$(date) Task completed. Have a nice day!"

The final script should look like:

#!/bin/zsh
echo "$(date) Running script $0 to create folders."
TOOLS_FOLDER=$1
REPORTS_FOLDER=$2
HELP_FOLDER=$3
TOOLS_FOLDER_CREATED=".$TOOLS_FOLDER-FolderCreated"
REPORTS_FOLDER_CREATED=".$REPORTS_FOLDER-FolderCreated"
HELP_FOLDER_CREATED=".$HELP_FOLDER-FolderCreated"
TODAY=$(date)
PATH_TO_LOG="$HOME/Library/Logs/folderCreator_log_v1-1.log"
echo "$(date) Starting" >> $PATH_TO_LOG
cd $HOME
echo "$(date) Creating folders: $TOOLS_FOLDER, $REPORTS_FOLDER, $HELP_FOLDER" >> $PATH_TO_LOG
mkdir $TOOLS_FOLDER
mkdir $REPORTS_FOLDER
mkdir $HELP_FOLDER
echo "$(date) Creating hidden file for $TOOLS_FOLDER folder." >> $PATH_TO_LOG
cd $TOOLS_FOLDER
touch $TOOLS_FOLDER_CREATED
cd ..
echo "$(date) Creating hidden file for $REPORTS_FOLDER folder." >> $PATH_TO_LOG
cd $REPORTS_FOLDER
touch $REPORTS_FOLDER_CREATED
cd ..
echo "$(date) Creating hidden file for $HELP_FOLDER folder." >> $PATH_TO_LOG
cd $HELP_FOLDER
touch $HELP_FOLDER_CREATED
cd ..
echo "$(date) Task completed. Have a nice day!"

Script locations

One last thing to talk about now is script locations. So far we have been placing our scripts where ever we wish and running them from there. But it may be a good idea to use a consistent location for the same. There are several candidates for this:

  • ~/Library/Scripts/
  • /Library/Scripts/

These are the more standard locations.

The only decision that needs to be made is whether it is the Library folder in the user’s home folder or the library folder located at root. This affects if the script is available only for a specific user or for all users on a computer.

There are other locations possible too. Developers often have a folder in the home folder called “Developer”. This needs to be manually created, but once created the system recognises it as the folder where files related to development are kept. You can create a scripts folder and place it in there.

Another popular location is the Application Support folder within the library folder. You can create a folder that represents items related to your scripts and then place the script in that folder. Note that these folders will have to be created by manually.

  • ~/Developer/Scripts/
  • ~/Library/Application Support/<your folder>/

These 2 locations would need to be created.

Scripts are not typically exposed to the end user. There is typically some kind of scheduling mechanism that triggers them. However, if a script is designed to be used by the end user you could even place them in:

  • /Applications/Scripts/
  • ~/Applications/Scripts/

Like the developer folder the applications folder in the home folder needs to be created. But once created the system recognises what it is intended for and gives it special privileges. The scripts folder within it will have to be created manually.

While this may not seem like a big deal. Placing your scripts in the correct location can lead to more consistent experiences, make troubleshooting easy, and also hide potential complexity.

Conclusion

The ability to store data within a script, pass data to a script or store data on an external file from within a script has several advantages. This makes the script more power and compact at the same time. It also makes the script less susceptible to errors and mistakes.

Video

Download

You can download the script from the same git repository as the previous one. The script is named folderCreator_v1-1.zsh.

Useful scripts for macOS

Getting Started

You might find these articles useful

One of the advantages with scripts is the fact that you can easily automate many tasks. Here is an article that walks you through that process.

If you come across a situation where you want to perform a set of tasks on multiple computers then scripts come in very handy.

I will be providing the Shell Script version of the task. Feel free to make changes to the scripts as required. I will try to provide an AppleScript version of the tasks a little later.

This is not the only way to implement the scripts. There may be multiple approaches towards achieving the same result. You will have to explore and examine the correct approach.

This is not a comprehensive list. The scripts should give you some ideas and act as a useful reference when you are creating your own scripts.

I have tested these scripts on macOS Catalina 10.15

Download

You can download all the scripts from here.

Script CategoryPage Number
Settings and Accounts1
Security2
Data3
Information Collection4
File System5

Disclaimer

The Software Is Provided “As Is”, Without Warranty Of Any Kind, Express Or Implied, Including But Not Limited To The Warranties Of Merchantability, Fitness For A Particular Purpose And Noninfringement. In No Event Shall The Authors Or Copyright Holders Be Liable For Any Claim, Damages Or Other Liability, Whether In An Action Of Contract, Tort Or Otherwise, Arising From, Out Of Or In Connection With The Software Or The Use Or Other Dealings In The Software.


WARNING

Please try these scripts on a test computer. Some of the scripts do make changes to the system. Always test before using these scripts.

Automation on the Mac

Automating tasks on the Mac is very useful for a wide variety of reasons. In this article we are going to look at the different technologies available for automating tasks.

TOOLS

Automator

The simplest way of achieving automation. Automator which is a built in application allows you to create task workflows by simply dragging in a set of predefined routines into a specified sequence. Let us explore how it works by creating a watermarking print plugin

Let us look at how we can create a print plugin that automatically adds a watermark to the pdf file.

  1. First get hold of an image that you will use as a watermark.
  2. Open Automator.
  3. Click on “New Document”
  4. Choose Print Plugin as the type of task to createScreen Shot 2018-03-21 at 11.58.26 AM
  5. From the left hand side drag the “Watermark PDF Documents” option. You will be able to locate this from the PDF library on the extreme right.1
  6. Add the image that will be used as a watermark. Customise the settings to your desired level. You may have to use trial and error till you get the desired output.
  7. Similarly drag the Move finder Items to the right. You will be able to locate this from the Files & Folders library.2
  8. Save the task as WatermarkCreator.
  9. Open a text file.
  10. Select File > Print
  11. Click on the PDF drop down in the print dialog.3.4
  12. Select the newly created task.
    3
  13. You have now successfully setup your own watermark creator.

Shell Scripting

For those coming from a Linux/Unix background this might be a familiar option. Very often users need to run a series of terminal commands repeatedly. While it is not difficult to do this, wouldn’t it be nice if we could write all the commands in a single file? Shell Scripts help users do just that.

To create a shell script:

  1. Open TextEdit
  2. Write the following code in there (We will write code to create a series of files and folders in our home folder for a user called admin):
    #! /bin/sh
    cd /Users/admin/
    if [ -d "/Users/admin/Applications/" ]; then
    echo "Applications Folder Exists"
    else
    mkdir Applications
    fi
    if [ -d "/Users/admin/Sites/" ]; then
    echo "Sites Folder Exists"
    else
    mkdir Sites
    fi
    if [ -d "/Users/admin/Developer/" ]; then
    echo "Developer Folder Exists"
    else
    mkdir Developer
    fi
    cd Developer
    if [ -d "/Users/admin/Developer/iOSProjects/" ]; then
    echo "iOSProjects Folder Exists"
    else
    mkdir iOSProjects
    fi
    if [ -d "/Users/admin/Developer/macOSProjects/" ]; then
    echo "macOSProjects Folder Exists"
    else
    mkdir macOSProjects
    fi
    
  3. Save the file with the name FolderCreator on the Desktop.
  4. Open the Terminal Application
  5. Let us make the script executable. To do that, run the commands:
    cd ~/Desktop
    chmod 777 FolderCreator
    
  6. Now run the command:
    ./FolderCreator

You have now easily created your own shell script. For more information about terminal commands you can read the following articles: Terminal Commands for OS X – BasicTerminal Commands for OS X – Part 2Terminal Commands – Part 3, & Configuring/Troubleshooting OS X Using Command Line

AppleScript

AppleScript is Apple’s proprietary scripting technology. It comes bundled as a part of macOS. To create AppleScript tasks we need to use the built in AppleScript editor.

Here is an example of a small AppleScript

tell application “Finder” to set the view for all Finder Windows as column view
tell application “Finder” to close every Finder Window
tell application “Safari”
open location “<a href="http://www.arunpatwardhan.com">http://www.arunpatwardhan.com</a>
open location “<a href="http://www.amaranthine.in/feedback">http://www.amaranthine.in/feedback</a>
open location “<a href="http://www.amaranthine.in/gallery">http://www.amaranthine.in/gallery</a>
end tell

Copy that block of commands in your AppleScript editor and see what comes up.

There are many more things that can be done with AppleScript. You can have popup windows asking users for commands, turn off the computer. Change the settings for different parts of the OS and for different applications. All this with commands written in a single file. All the user has to do is double click the file.

For more information about AppleScript visit Apple’s Developer site.

Launch Agents, Launch Daemons

NOTE: Scheduling Launch Agents/Launch Daemons improperly may leave your computer in an unusable state. Always test this on a computer that does not contain important data. If you are unsure, please consult someone with knowledge of the same before proceeding ahead.

Launch Agents/Launch Daemons allow you to schedule tasks which are to be performed at intervals. You can also use them to ensure that tasks are kept running and that the user does not have the possibility to quit them. To setup a launch daemon:

  1. First create a Plist file that looks like the one below. I have created a script called echoer and placed it in the /Users/admin/Applications folder where admin is the user.Screen Shot 2018-03-22 at 10.34.18 AM
  2. Place the file in the ~/Library/LaunchAgents folder. Name it in.amaranthine.demod.plist
  3. Run the command in terminal to load the Launch Agent.
    launchctl load ~/Library/LaunchAgent/in.amaranthine.demod.plist

That’s it you have just setup a simple launch agent which will ensure that your script runs every 6 seconds.

For more information or to create detailed Launch Agents/Launch Daemons visit:Creating Launch Agents & Launch Daemons

Login Items

An easy way to automatically load, Applications/Files/Folder, as soon as well login is to use Login Items. This is very easy to do.

  1. Open System Preferences > Users & Groups
  2. Switch to the Login Items tab.IMG_1560
  3. Click on the ‘+’ sign at the bottom to add new Applications. Let’s add Maps so that it launches as soon as we login. You should see it appear in the list.IMG_1561

That’s it. You have setup login items. You can repeat this process for as many applications as you wish.

Others

PHP, Perl, Python, Javascript, Swift allow you to create custom automated tasks and routines. These require knowledge of programming.

Choosing the right approach

Which one to choose depends on a lot of factors but we can break it down to 2:

  • You are a technically qualified person and understand things like programming, scripting and command line
  • You are an end user working either at home or in office.

End User

If you are an End user then you should really stick to Automator and Login Items. These are the ones that are the easiest to implement and least likely to cause any issues. You could venture and explore other options if you have a good understanding of them. Or you can ask the IT or Tech Support teams to help you with scripting and other technologies.

Tech Support or IT Person

Any of the tools mentioned above can be used by you. Make sure that you have a good command over the tools and are able to troubleshoot issues arising out of their usage.

Note: The programs/applications/tools and languages mentioned in this article may not cover all the available options. Also, anyone who uses or implements the items mentioned in the article does so at their own risk. The author does not take responsibility for any loss or damage that may arise from the use of the programs/applications/tools and languages mentioned above.

 

Terminal Commands – Part 3

This post follows up on the previous 2 posts. Here we will look at some advanced commands. Note that this is by no means a comprehensive list. You can visit the online version of the man pages: Apple Man Pages

Here are the links for the previous posts.

Terminal Commands – Part 1

Terminal Commands – Part 2

The commands are explained with some common tasks in mind. Some of the steps are there to illustrate the usage of the commands. To find out other tasks that can be performed using these commands simply refer to the man pages.

FILE SYSTEM

diskutil

The diskutil command is a handy tool to run disk utility via the terminal. A full list of tasks that can be executed are available via the man command.

Example:

There could be situations where you have encrypted your USB drive & have forgotten the password for the same. While there is no way to retrieve the data, it is still possible for you to use the USB disk. Let’s use the diskutil command to do this.

– Launch terminal

– Make sure that the USB is properly connected

– Run the command diskutil cs list

– This lists all the core-storages that are connected to your system.

– Next to the Logical Volume Group select the alphanumeric id & copy it (make sure you select the correct logical volume group).

– Type the command diskutil cs delete <id>

– Replace the id with the alphanumerics id you copied earlier.

– This will completely remove the encrypted partition. As mentioned earlier you will loose all the data that is already there on the partition.

diskutil man page

fsck

File System Check command. This command is used to check different Filesystems by invoking the corresponding sub command.

fsck man page

hdiutil

hdiutil is used to perform some other tasks related to storage devices. A good example of this is the creation of different kinds of disk images.

Example:
Let us create a disk image for the ~/Documents directory

– Launch the terminal application

cd ~/

mkdir ImageDemo

hdiutil create -srcfolder ImageDemo ImageDemo.dmg

Apart from the above example, there are a lot of other tasks that can be performed using hdiutil. For full information run the man command.

hdiutil man page

mount

The mount command is used to mount a volume via the terminal. A volume can be mounted as a Read-Write or a read only. For full details on the functionality run the man mount command

Syntax: mount <options> <volume location>

mount man page

GETTING INFORMATION RELATED TO THE SYSTEM & FILES

uptime

Shows how long the system has been running.

uptime man page

ioreg

Get information out of the io registry.

Example:
Suppose we want to get information about the serial number. We would use the ioreg command.

– launch terminal

cd ~/Documents

ioreg -l | grep “IOPlatformSerialNumber”

Optionally you can pipe the information to a file & read it later.

ioreg man page

system_profiler

Used to get system related information. it can generate a small or a detailed report depending on what option is used. For full information access the man pages for the command.

system_profiler man page

top

Displays information about the various processes running within the system including the usage 7 state. To get more information about the full functionality visit the man pages.

top man page

METADATA

mdls

Used to get the metadata for a given file or folder. The data is displayed on the screen.

mdls man page

mdutil

Manages spotlight search indexes.

mdutil man page

NETWORKING

networksetup

This command is used to access the information that is found within the System Preferences > Network Preferences pane.

You can use this command to do some quick command line configuration of the network settings.

A good example is to manually configure the settings while booted in the OS X Recovery partition. Here is an Apple Support Document which talks about the same.

http://support.apple.com/kb/HT5034?viewlocale=en_US

networksetup man page

MISCELLANEOUS

tar

Used to compress file(s) into a single tar file or decompress.

Example:

This example demonstrates how to use the tar command.

– Launch terminal

cd ~/Documents

mkdir Docs

cd Docs

echo “A” | cat > file1

echo “A” | cat > file2

echo “A” | cat > file3

echo “A” | cat > file4

Now to compress all the files

tar -cvf compressedFiles.tar file*

To uncompress them all

tar -xvf compressedFiles.tar

tar man page

tmutil

Manage Time machine from the terminal. Useful command to manage time machine from the command line.

tmutil man page

fdesetup

Used to mange file-vault from the command line

fdesetup man page

Terminal Commands for OS X – Part 2

Continuing from the previous post on.

RENAMING FILES & FOLDERS
The way files/folders are renamed is by using the mv command. In the mv command make sure that the destination folder is the same as the source folder for your target, its just the target name that changes.

So if we have a folder called Documents & inside it we have a folder called Files. To rename Files to OfficeFiles we run the following command.

mv Files OfficeFiles

MODIFYING PERMISSIONS
Top modify the basic UNIX or POSIX permissions on a Mac we need to use the chmod command. The standard permissions apply to Users, Group & Everyone else. Each of these entities has 3 flags assigned to it: rwx Read-Write-Execute. Each flag is a boolean flag holding either true or false, indicated by 1 or 0.

So for example if we wanted to modify the permissions of the OfficeFiles folder to be read-write only for the user & read only for group & everyone else then the command would need the following information.
User: rwx = 110 -> 6
Group: rwx = 000 -> 0
Everyone else: rwx = 000 -> 0

So the command would look like: chmod 600 OfficeFiles
FOLDER PATHS & NAVIGATING FOLDERS
A good understanding of the folder structure within a Mac is necessary while dealing with terminal commands.
All folders with the Mac begin at root indicated by ‘/’
Root contains the following folders
Applications
Library
Systems
Users

rootFolderMost of the work that is done is done within the Users folder. In most cases users would not need to go to the other folders for their day to day work.

homeFolder

As you can see from the screenshots, the terminal shows more folders than are visible through finder.

rootFolderInTerminal

homeFolderInTerminal

Within the Users folder all the Home Folders for the different users on the machine are listed. Again, users typically have access only to their own login folder.

Each users Home Folder contains the following Folders
Documents
Downloads
Music
Pictures
Public
Desktop

Depending on your usage you may see a different view from the one shown below.

The following are some examples of navigating the File System.

1)Accessing the root folder

cd /

2)Accessing the home folder. Example home folder called admin

cd /Users/<home folder name>

cd /Users/admin

or cd ~/

NOTE: the ‘~/’ is a shortcut for accessing the home folder directly. Using the shortcut access will only be given to the home folder for the user currently logged in.

3)To access the OfficeFiles folder

cd ~/Documents/OfficeFiles

or

cd /Users/admin/Documents/OfficeFiles
EDITORS
There are a number of built in editors available within the Mac. Apart from TextEdit, which is a GUI based editor, there are many editors available for direct use from the terminal too.

Emacs:
Emacs is a basic text editor that is built into the mac. There are various versions available but those may not be built in. You may need to download them manually.

Here are some common emacs commands to perform operations. For an even bigger list visit http://www.cse.iitb.ac.in/~br/courses/cs699-autumn2013/refs/emacs-commands2.html

http://www.gnu.org/software/emacs/manual/html_mono/emacs.html#Commands

Vi:
This is yet another editor that is built into most UNIX like OS. This is the editor that is used when running the less or more commands.

Here are some common commands used in Vi.
http://www.tutorialspoint.com/unix/unix-vi-editor.htm

Apart from this there are other editors such as gedit & xemacs which has a GUI interface.

NETWORKING RELATED TERMINAL COMMANDS
1)ping
Used to test connectivity to a particular IP address
ping http://www.google.com

2)traceroute
Used to check the hop trace between your machine & destination
traceroute http://www.google.com

3)ipconfig
Used to get network interface related information
ipconfig getoption en1 <option>
There is a lot more information that can be gleaned using ipconfig. For a full list run the man command for ipconfig

4)ifconfig
Used to configure ifnormation related to network interface
ifconfig en0 inet <ip address> netmask <subnet mask>

5)lookup
There are different ways to get the forward & reverse lookup to happen
nslookup <domain name>
nslookup <ip address>

dig <domain name>
dig -x <ip address>
NOTE: Please use the man command to get more information.

Terminal Commands for OS X – Basic

This is the first part of the Terminal Commands topic. This article covers the basic commands which a user can use on the terminal.
To launch the terminal application simply navigate to /Applications/Utilities/Terminal. Alternatively, you can even search for the same using spotlight.

cd
This command is used to change the current directory we are. So if we want to navigate to a new folder we simply run the cd command.
Syntax:  cd <folder path>
Note that the folder path has to be the absolute path of the folder. Beginning from root. If you simply place the name for the folder, the OS is going to search for that folder within the current directory. The command won’t work if it doesn’t find the specified folder.

ls
This command is used to list the contents of the current directory.
Syntax: ls or ls <folder name>
There are a lot of switch based options available to get more detailed results.
-a shows hidden files
-l shows in a list format with more information
-r oldest entries first
-t most recently modified entries first
View the manual for more switches.

rm
This is the remove command. This command is used to remove one or more than one files &/or folders from the specified directory.
Syntax: rm <folder name>
To remove folders we need to use the -r switch.
NOTE: The rm command should be run with caution. Running the above command does not move the contents to trash. There is no way to undo the rm command. Also care must be given to the folder from which the command is run, make sure you are in the folder you wish to be in before running this command.

pwd
Present Working Directory. This command tells us the directory we are currently in.
Syntax: pwd

cp
This command is used to copy the specified file or folder to a specified location.
Syntax: cp <source file> <destination folder>
In order to copy folders we need to use the -r switch.

mv
This command is used to move files or folders to a specified location.
Syntax: mv <source file/folder> <destination folder>
This command can also be used to rename files & folders by giving a different name but keeping the destination the same. The destination folder is replaced with new name.

less
This command is used to display the contents of a file.
Syntax: less <name of file>
This command displays the contents of a file in simple text. It displays the content in a page by page format. To stop viewing the file simply type ‘q’.

chmod
This command is used to modify the ACLs of a specified file or folder.
Syntax: chmod <rwx-rwx-rwx> <file or folder name>
The rwx indicate read-write-execute permissions for the owner-group-everyone else. The permissions are given by either indicating 1 or 0 in place of each rwx. 1 indicating true, 0 indicating false. The resulting binary numbers should be replaced by their octal equivalents. So to give only the owner rwx permission on a file the command would need 111-000-000 which is 700, so the command would look like chmod 700 <file/folder name>. If we wanted to give rw permission to owner & r permission for group & everyone else then the command would look like 110-100-100 or 644, chmod 644 <file/folder name>.

mkdir
This command is used to create a directory in the current working directory.
Syntax: mkdir <directory/folder name>
This will create a directory in the present working directory. Do keep track of the current directory before creating a new one, to make sure you are creating it in the correct directory.

sudo
Super User Do.
Syntax: sudo <command to be executed by super user>
This command is used as a prefix before other commands when we want those commands to be executed as a super user (aka root user). You are required to authenticate as an administrator for the command to work. NOTE: Do exercise caution while using the sudo command. Root users don’t have the built is permission checks & security checks. Improper usage of sudo may leave your machine in an unusable state.

man
Terminal command manual.
Syntax: man <terminal command>
This command is used to open the manual for the different commands that are available within the system. It is a good starting point to understand the full functionality of the different commands along with examples of usage.

cat
The cat command is used to perform 3 different tasks: Display text files, copy text files, combine text files, create new text files.
Display Text Files
Syntax: cat <file name>
This will display the text files on the screen.

Copy Text Files
Syntax: cat <first file name> > <second file name>
This will copy the contents of the first file to the second file using the redirect ‘>’ operator.

Combine Text Files
Syntax: cat <first file name> <second file name> > <third file name>
This will combine the contents of the 2 files & save it in the third file.

Create New Files
Syntax: cat > <filename>
This will create a new file.

echo
Display given sentence on the screen.
Syntax: echo <sentence>

grep
Used to find a particular string within a file
Syntax: grep <string pattern> <file name>
This will look for the given string pattern in the specified file. You can use different switch options to modify your result. View the man pages for the same.

find
The find command is used to find files within the system.
Syntax: find <search path> <search criteria> <name of the file> <operation to perform>
Search path – specifies the folder where we want to search
Search Criteria – specifies what to search eg: name
Name of File – name of file to search
Operation to perform – how to show the results