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.

Shell scripting in macOS – Part 1

The scripts in the following articles are written in macOS Big Sur. You can use these concepts to create scripts on UNIX and Linux too.

This is the first part of a multipart series. You can find links to further articles at a table located at the bottom of this article. I will be updating this article with links to new articles as I publish them.

What is shell scripting?

Shell scripts are simply files that list out a series of commands in the sequence in which they are to be executed. By commands we typically mean other shell commands. But these could also be other executables, scripts, or commands from other languages.

Why do we need it?

There are several reasons. The most common reason being automation. For example, If there are steps that we perform on a repeated basis such as checking for the presence or absence of particular files we could easily automate this task with the help of a script. Or If we want to perform certain tasks at scale: such as creating a set of files and folders that should always be there within the user’s home folder.

Scripting also has the added benefit of consistency. By performing the tasks the same way we can ensure that our desired outcome is the same every time. 

What is required for creating shell scripts?

Before we go ahead and look at how to create our own scripts there are a few things we need to keep ready at hand.

  • First we would need to know the commands we would have to execute to achieve our goal. This list is quite large and one would not necessarily know all the commands supported. But overtime your knowledge of these commands will grow. So do not worry!
  • Second, We need to pick a shell interpreter.
  • We also need to decide how we will be accessing the command line interface. This would most likely be via the Terminal application, but there are other ways too.
  • Finally we need to decide on the editor we will use to create our scripts. I will talk about this a little later.

Shell interpreters

The shell interpreter is as the name says the object that will interpret the commands and execute them. The default shell interpreter for macOS is zsh starting macOS Catalina. We can choose to use that or any other interpreter. While most commands we will be using will be common ones that are available across all interpreters be aware that some commands may be unique to certain interpreters only.

Commands

We will need to know some basic commands that will help us compose our scripts.There are several commands available in macOS. We will be learning about quite a few of those over the course of the next few articles. The table below lists some of the commands that we will be using.

CommandDescription
cdChange directory. This command changes the current working directory to the specified path. We use this command to navigate to another folder.
mvThis command moves the contents from the specified folder to another folder.
lsLists the contents of the folder.
rmRemove the specified content.
cpCopy the contents of a folder to another folder.
touchUpdate the timestamp for a file or folder.
pwdPrint the complete path to the present working directory.
mkdirCreate a folder.
echoPrint the string out onto stdout.

Be aware that many commands will create/modify/delete items in the current folder if the absolute path is not specified in the command. This may result in unexpected or unintended behavior.

Editor

I will be using Xcode as the editor for our scripts. However, you can use any editor you wish. You will find the a list of editors at the bottom of the article.

Using Xcode as an editor for scripting may be a bit of an overkill. It is a very heavy application primarily designed for app development. If you are currently developing apps and are already using Xcode then you can go ahead and use it for scripting too. Otherwise it might be a good idea to go in for a different tool.

Building our first shell script

In order to build our script. Let us take a simple scenario. Let us suppose that every user in our organisation must have the following folders:

  • Tools
  • Reports
  • Help

All these folders must be located in the home folder for each user. So let us take it step by step. We will perform these commands manually from the Terminal application.

  1. The first command is the command to navigate to the home folder.
cd ~/

The ~/ represents the path to the current user’s home folder. The cd command is used to change the working directory to the newly specified path.

  1. Now we will create the 3 folders.
mkdir Tools
mkdir Reports
mkdir Help

All the 3 commands are creating a new folder. Since we did not specify the complete path to the folder. These items are created in the working directory.

  1. Now we will step into each folder and create an empty hidden file.
cd Tools
touch .ToolsFolderCreated
cd ..

Let us break down these commands one by one.

First we go into the Tools folder.

Then we use the touch command to update the timestamp of the.ToolsCreated file. Since the file doesn’t exist the touch command creates the file for us. Also as the file starts with the . character it is hidden by default. Creating a hidden file like this is a good way of leaving behind some flag indicating that the script ran successfully. Of course, in our example this can be determined simply by seeing the folders that are created. But in more elaborate situations they are a very useful way of laying down milestones for a script.

The next command takes us back a step outside the enclosing folder. In our case the Tools folder is inside the home folder. So we are going back to the home folder.

We will repeat the steps again for the Reports and Help folders.

cd Reports
touch .ReportsFolderCreated
cd ..

cd Help
touch .HelpFolderCreated
cd ..

Those are the commands we execute to get the desired result. You can switch to the graphical user interface to see if the items have been created. Note that the files created with the touch command will not be visible by default.


Now that we have seen how these commands work. Let us create a script.

  1. Use any editor you like. I will start off with TextEdit. Create a new file. If you are using TextEdit then do not forget to convert the formatting to plain text. Format > Make Plain Text.
  2. Give the file any name you want. I will call it folderCreator.zsh.
  3. Save the file where ever you wish. I will save it on the Desktop folder for now.
  4. On the first line we need to specify our interpreter. This indicates that the commands in our script need to be interpreted by the zsh interpreter.
#!/bin/zsh
  1. One the next line we will type the command to go to the home folder.
#!/bin/zsh

cd ~/
  1. Next we will type the command to create the 3 folders.
#! /bin/zsh

cd ~/

mkdir Tools
mkdir Reports
mkdir Help
  1. Finally we will add the code to create the hidden files.
#! /bin/zsh

cd ~/

mkdir Tools
mkdir Reports
mkdir Help

cd Tools
touch .ToolsFolderCreated
cd ..

cd Reports
touch .ReportsFolderCreated
cd ..

cd Help
touch .HelpFolderCreated
cd ..

  1. A nice addition to the script would be the echo command. This command would let the person who is running the script know about the different events taking place.
#! /bin/zsh

echo "Running script to create folders."

cd ~/

echo "Creating folders: Tools, Reports, Help"
mkdir Tools
mkdir Reports
mkdir Help

echo "Creating hidden file for Tools folder."
cd Tools
touch .ToolsFolderCreated
cd ..

echo "Creating hidden file for Reports folder."
cd Reports
touch .ReportsFolderCreated
cd ..

echo "Creating hidden file for Help folder."
cd Help
touch .HelpFolderCreated
cd ..

echo "Task completed. Have a nice day!"

Your completed script should look like:

#! /bin/zsh

echo "Running script to create folders."

cd ~/

echo "Creating folders: Tools, Reports, Help"
mkdir Tools
mkdir Reports
mkdir Help

echo "Creating hidden file for Tools folder."
cd Tools
touch .ToolsFolderCreated
cd ..

echo "Creating hidden file for Reports folder."
cd Reports
touch .ReportsFolderCreated
cd ..

echo "Creating hidden file for Help folder."
cd Help
touch .HelpFolderCreated
cd ..

echo "Task completed. Have a nice day!"
  1. Save the script.

That’s it. You have just created your first script.

Running our first shell script

The next step would be to run our script. There are 2 ways of doing this. We will look at both the options.

Option 1

We can directly run the script using the zsh command.

zsh ~/Desktop/folderCreator.zsh

Note that we will need to provide the path to the script file.

This is a straightforward way. We simply tell the interpreter to execute the commands in our script.

Option 2

This option requires a few more steps.

  1. First we need to change the permissions on the script. We need to make sure that all 3: Owner, Group, Everyone else have the read and execute permissions. Of course, you are free to change the permissions to whatever you want. But the execute capability is required. We will change the permissions from the command line.
chmod ugo+x ~/Desktop/folderCreator.zsh

There are other ways of writing this command too. But for now we are simply saying that we want to add the execute capability to the Owner, Group, Everyone else. If you look at the file in the GUI, you will see its icon has changed to the executable icon.

  1. Next we will simply run the following command from the terminal application.
./Desktop/folderCreator.zsh

Now we can simply run the script by invoking it from the terminal application. Or we can trigger it from the graphical user interface by simply double clicking on the file.

There you go. You have successfully created and tested your own script. Try to play around with some of the terminal commands and create your own scripts.

Video

You can watch the video I have created in case you wish to see the steps.

Download script

You can download this version of the script from here.

Popular editors for shell scripts

Here are some links for popular editors.

Coderunner

Emacs

Atom

Xcode

Shell scripting topics

Here are the links to more parts in this series. I will add the links as I publish the articles.

Part 2: Managing information

Part 3: Conditional Checks

List of macOS Terminal commands

This article lists out different macOS terminal commands you might encounter. You can use this list as a starting point in your search for a command to perform a specific task. This list is by no means exhaustive.

Basic terminal commands are not listed here. Some of them are listed in the following Terminal command articles.
Terminal Commands – Basic
Terminal Commands – Part 2
Terminal Commands – Part 3

Many of the commands have also been used in the article I wrote some time back. You can have a look at the scripts to see some of the commands being used.

To get more information about the commands simply run the following command from within Terminal Application. For example, to view the manual page for tmutil simply type:

man tmutil

For fdesetup

man fdesetup
Here is a nice command to quickly open the man page in the Preview App.
man -t tmutil | open -f -a /System/Applications/Preview.app

Note

  • This is not a complete list of commands
  • Some commands are available through the macOS Recovery Volume only
  • Some commands required other resources such as the OS installer
  • Some commands are available with certain versions of the OS only

Please read the documentation for more details. Use the commands with care. Improper use of commands may result in loss of data or damage to the computer.

Commands


Installation

CommandDescription
startosinstallUsed to start the installation of macOS from the command line.
createinstallmediaUsed to create an external install disk.

Security

CommandDescription
fdesetupManage FileVault configuration.
securityManage keychain and security settings
spctlManage security assessment policy
csrutilConfigure System Integrity Protection (SIP) settings
resetpasswordPassword reset utility located in the Recovery Partition

File System

CommandDescription
hdiutilUsed to manipulate and manage disk images.
diskutilUsed to modify, verify, & repair local disks.

Data Management

CommandDescription
tmutilUsed to configure Time Machine settings in macOS
screencaptureTakes screenshot of the specified screen and saves the image at the specified location.
mdlsUsed to get metadata attributes for a given file
mdutilUsed to manage metadata stores that are used by Spotlight

Settings

CommandDescription
defaultsUsed to modify plist files. Typically used to update preference files.
ioregUsed to view the I/O kit registry
system_profilerUsed to generate system hardware & software reports.
plutilUsed to check syntax of property lists or covert property lists from one format to another
AssetCacheManagerUtilUsed to configure content caching settings.
openUsed to open documents from within the command line.
networksetupPerform network configuration.
systemsetupUsed to configure machine settings in System Preferences.
launchctlUsed to manage and inspect daemons, agents, & XPC Services

Applications

CommandDescription
codesignUsed to create, check, display code signatures.
pkgbuildUsed to build installer packages
productbuildBuilds a product archive
installerSystem software and package installer tool

User Account Management

CommandDescription
dsclThis is a command line Directory service utility that allows us to create, read, and manage Directory Service data.
sysadminctlUser account management
passwdChange user password
loginUsed to login to another user account.

Server & Device Management

CommandDescription
profilesUsed to install, remove, list, or manage Configuration profiles.
serveradminUsed to manage the services in macOS
mdmclientLocated in /usr/libexec/mdmclient it is used to manage interactions with the MDM.
asrApple Software restore: Used to copy volumes.

Scripting

CommandDescription
osascriptUsed to execute the given AppleScript

Share any commands you may know of in the comments window.

Disclaimer

The information 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 information provided Or The Use Or Other Dealings In The information.