Shell scripting notes

A shell script is a script written for the shell, or command line interface: Terminal. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

Examples of uses for shell scripts:

  • Editing large text or data files.
  • Automating tasks, such as backup operations.

You can write a shell script to a text file to perform various tasks on your Mac.


#!/bin/sh if [ -f ~/Desktop/ ]; then say Hello! fi

Save this code to a text file named, on your desktop. It is best to use a program like Text Wrangler, or vi. Open the Terminal (in the Applications/Utilities/ folder) and type: chmod +x ~/Desktop/, to make the file executable. To run the script type: ~/Desktop/ in a terminal window.

#!/bin/sh tells the shell to run the program in sh, a type of shell program.
if and fi are the beginning and end of an “if statement”. Allows execution of certain commands if certain arguments are true.
[ -f ~/Desktop/ ] is the argument. It means: if the file exists on the desktop, do the commands that follow the “then” statement.
say Hello! is the command. This should cause your Mac to say “Hello!”.

You will need learn some basic Unix commands to get around in the command line interface. Read a Mac Terminal tutorial (pdf). The basics can be learned quickly. Below are a few commands that you need to know:

, list the contents of the current directory.
pwd, print the path to the current directory to screen.
cd, change directory. Ex: cd ~/ will change directories to your user home directory.
chmod, changes permissions. Vital, but best to do a tutorial on this one.
mkdir, create a new directory.

Using the Terminal in OSX and Linux is similar.

Utilities/commands useful for shell scripting.

awk A programming language that is designed for processing text-based data. awk video
grep Text search utility originally written for Unix. grep video grep video 2
cat Unix program used to concatenate and display files. cat video
sed is a Unix utility that (a) parses text files and (b) implements a programming language which can apply textual transformations to such files. Sed video

Example grep: this lists (ls) the items in the current directory, and recognizes directories in the list by using the -F option ( you will note the / after list items that are directories). The pipe (|) forwards the result to grep, which filters for directories because we used “/” as the argument.

ls -F | grep /

Example grep and awk: df lists the drives and partitions and disk space on the computer. -h makes it more (h)uman readable. Pipe (|) into grep is filtered for one of the disks, named disk0s2. Pipe (|) awk prints only the 5th column ($5). The result is just the disk space used ex: 39%.

df -h | grep disk0s2 | awk '{print $5}'

or just use awk in this format to get the same result

# more efficient df -h | awk '/disk0s2/ {print $5}' # prints it out in human readable formatting. df -h | awk '/disk0s2/ {print $1 " is " $5 " used. "}'

More info from shell scripting primer from Apple:

Advanced Bash-Scripting Guide

An example of a shell script in action, and a detailed explanation of its functionality.

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>