Project9.Learn About Shell Scripts


Project 9. Learn About Shell Scripts

In this project, you'll learn how shell scripts are used to automate a series of Unix commands, and you'll study the anatomy of a basic shell script.

Learn More

Refer to the projects in Chapter 9 when you crave more scripting power.


A shell script is a sequence of commands written in a shell scripting language and stored in a file the shell can read. When a shell script is run, the shell reads the file and runs the commands it contains in much the same way as it reads and executes commands you type at the command line. Of course, the same file can be executed many times.

The components of a shell script fall into two categories:

  • Normal Unix commands as you would issue them on the command line, such as cd, ls, mv, and so on. This project focuses on them.

  • Features of the shell scripting language itself, such as variables, and control constructs that enable techniques like loops and conditional processing. We might have the script test to see whether a file exists, rename it if it does, and display an error message if it doesn't. We'll look at scripts that use these techniques in Project 10.

Any serious attempt at scripting requires the use of a text editor, and you'll need to use one to complete the examples in this project. If you're not familiar with any of the available Unix text editors, such as Nano, Emacs, and Vim, use Apple's TextEdit, but remember to save the file as plain text. In TextEdit, select menu Format and then item Make Plain Text. If you see the item Make Rich Text instead, the file is already in plain text.

Learn More

The projects in Chapter 4 cover Unix text editors at greater length.


A Simple Script

Let's look at a ready-made script called info that illustrates the basics. This sample script does nothing more than invoke a few Unix commands, exactly as you could do interactively on the command line. It doesn't exist on your Mac, so you're going to have to create it by typing it in a text editor and saving it. You can save it anywhereDocuments might be a good place to choose. This is how it looks displayed in cat.

$ cat info #!/bin/bash # A simple Bash shell script echo "Information produced on " $(date) echo "Host information" hostinfo echo; echo "Hostname" hostname echo; echo "Uptime" uptime


The first line of the script (and it must be the very first line) is #!/bin/bash. It says this is a Bash script and that it should be passed to /bin/bash for execution. When you execute a shell script, it doesn't run in your interactive shell directly; instead, your shell launches a new shell, inside itself, to run the script. This approach lets users run scripts that weren't written for their preferred shell; a Z shell user, for example, can run a Bash shell script, relying on the first line of the script to tell the Z shell to find and launch /bin/bash. The approach also has advantages with scripts written in the "native" language of the user's interactive shell. Running in a shell-within-the-shell (or subshell), a script can set its own environment settings without affecting those of the user's current shell. (See "Scope" in Project 4.)

The second line is a comment, denoted by a hash (#) symbol and not immediately followed by an exclamation point. The shell ignores lines that start with #; comments are notes describing what the script does and how it does it.

The remainder of the script simply runs Unix commands echo, date, hostinfo, hostname, and uptime. Several commands can be placed on the same line if they are separated by a semicolon.

Executing the Script

To execute the script, type its full pathname or precede it with./. Let's see what happens when we do so.

$ ./info -bash: ./info: Permission denied


Learn More

Refer to Project 8 for more on permissions and how to set them. See Project 50 to understand why the script name must be preceded by an absolute pathname or ./.


Oops! Before a script can be run, it must have execute permission. We'll fix that with chmod.

$ chmod +x info


Permissions set, let's try it again.

$ ./info Information produced on Mon Jul 18 16:55:15 BST 2005 Host information Mach kernel version:      Darwin Kernel Version 8.2.0: Fri Jun 24 17:46:54... Kernel configured for a single processor only. 1 processor is physically available. Processor type: ppc970 (PowerPC 970) Processor active: 0 Primary memory available: 1.00 gigabytes Default processor set: 86 tasks, 271 threads, 1 processors Load average: 1.64, Mach factor: 0.24 Hostname sauron.mayo-family.com Uptime 16:55 up 3 days, 4 hrs, 2 users, load aves: 1.04 0.68 0.73


That's about all there is to writing and running a simple shell script.

Sourcing a Script

If you want a script to run directly in your interactive shell, not a new shell inside it, use the command source. A script run using "source script-name" or the equivalent of". script-name" can affect the environment of the current interactive shell (see "Scope" in Project 4) and may be used to set environment variables and shell options and variables.

You may recall that we used this command in our login scripts in "Bash Initialization" in Project 4; the script /etc/profile sources the script /etc/bashrc.





Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net