Shell scripting notes

Posted by on May 17, 2010 in code, how to, Mac | No Comments

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.

Example:

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

Save this code to a text file named hello.sh, 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/hello.sh, to make the file executable. To run the script type: ~/Desktop/hello.sh 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/hello.sh ] is the argument. It means: if the file hello.sh 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:

ls
, 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:

http://developer.apple.com/mac/library/documentation/opensource/conceptual/shellscripting/Introduction/Introduction.html

Advanced Bash-Scripting Guide

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

Email this to someoneTweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on StumbleUponShare on RedditDigg this

Leave a Reply