An Introduction to Tiger Terminal, Part 4by Mary Norbury-Glaser
What are shell scripts, and why would you want to create them? Well, if you begin to use the Terminal with regularity, you'll find that you are repeating many of the same commands, often in sequence, in order to accomplish some task. You can automate these tasks by writing a shell script file.
What You Need To Know--And Do
In part one of this series, we ran the
cat /etc/shells command to see a list of shells available in Mac OS X Tiger. Each shell has a standard scripting language associated with it. Since our default shell in Mac OS X Tiger is
bash, we'll stick with that for this scripting tutorial.
You'll also need a text editor to write your scripts. In part one, I introduced Mac OS X Tiger's new default text editor,
nano, which we'll use here to write and save our new shell scripts. You can use any text editor, whether it's a command-line editor or a graphical editor like SubEthaEdit (www.codingmonkeys.de/subethaedit) or BBEdit (www.barebones.com/products/bbedit/index.shtml). If you choose a graphical editor, make sure you select the option to use Unix line breaks when saving files.
Let's assume you'll be creating shell scripts on a regular basis, so it's the best practice to create a directory where you'll save all your scripts. Create a new folder in your /Documents directory and name it scripts. From the Terminal command line, now!
cd Documentsnorburym15:~/Documents norburym$
mkdir scriptsnorburym15:~/Documents norburym$
Remember that your default location when you open the Terminal app is your home directory. Here, you've changed into your Documents directory and then created a new directory (folder) called scripts, using the
mkdir (make directory) command. The Terminal doesn't tell you it's been created nor does it move you into the directory automatically. Run the
ls command at the last prompt above to see that the new directory has, indeed, been created:
Figure 1. ~/Documents/scripts folder
The next thing you'll want to do is tell the shell where to find this directory. You'll do that by adding the location of your new scripts directory to the
PATH variable in your .bash_profile file. Huh? OK, good time for an aside! When you open the Terminal app, the default shell,
bash, starts up and reads some files in order to get configuration information. The primary files are bashrc and profile, which live in the system /etc directory (partial listing shown below):
However, to set user-specific configuration variables, it's best to create a file in your own home directory that
bash will read when it starts up.
bash will read the system-wide files first, and then the user-specific ones. The user-specific file settings will override those in the system-wide files.
You can see your current
PATH by executing the
echo $PATH command at the shell prompt:
echo $PATH/bin:/sbin:/usr/bin:/usr/sbin norburym15:~ norburym$