. 24
( 69 .)


¤ Type echo followed by a
space. contains your username.

You can write scripts that are easier to understand and maintain by
adding comments that explain the complicated commands and
describe how the scripts work. Comments are lines in scripts that the
system ignores when it runs the script. Comments serve the function of
explaining your script to others who read it so that complex commands
are easier to understand and so other users do not have to read the
entire script to know what it is supposed to do.
To turn a line of text in a script into a comment, insert a pound sign (#)
in the first column. The shell ignores that line when it runs the script,
so you can place anything you like in a comment. You can also add
comments to the end of a line of code by adding # at the end of the
Unix command and before the text of your comment. Always place at
least one space or tab on each side of the #.
While it is a good idea to include some comments in a script to explain
what the script does, you do not need to comment every line of code.
Inserting one or two lines of comments at the beginning of a script is
useful. Placing a comment on every other line is distracting. When you
run this script, the shell ignores the comments.

hello, user
# this is a script that greets the user
echo hello, $USER # address user by his username

› Press Control + O to write Á Press Return. ‡ Press Control + X to exit
out the file. Pico.
ˇ Type a name for your new
shell script.

typing each line in the Terminal window. The second way is

ou can save yourself a lot of work and better
to first make the script executable and then execute it by
remember complicated Unix commands by placing
typing its name. See Chapter 2 for more information on file
commands in scripts and running the scripts. When
you run a script, you can execute a sequence of commands
by typing no more than a single filename. After you turn a
For a script that you intend to run many times, changing the
series of commands into a script, you can type the filename
file permissions so that you can run it by entering only its
of that script just as if it were another Unix command. In
name can save you time. If you use the chmod a+x
fact, many Unix advocates like to think of scripts as
command, other users can execute the script too.
extensions to the operating system. In a sense, when you
create a script, you add a new command to your system. The source command is especially handy when you want
to run scripts that do not have execute permissions set. If
You can execute a script in two ways. The first way is to
you do not own a script and cannot change its permissions,
type the command source, followed by the name of the
you may still be able to run it by using the source
script. When you use the source command, the shell reads
command. You must have read permission, which enables
and executes the file one line at a time just as if you were
you to read a file, to run it using the source command.


– The shell runs the
„ Type source followed by a ‹ Type chmod followed by a ˇ Type the name of a script
space. commands in the specified space. and press Return.
– The chmod command
¤ Type the name of a script › Type a+x followed by a
and press Return. space. adds execute permissions for
all users.

While the source command is very handy, especially for running
scripts that do not have execute permissions set, an important TYPE THIS:
restriction applies. The commands in the script must be
compatible with the shell that you are using. If a script contains [ferro:˜] user% source macosx
any shell-specific syntax, such as the looping commands discussed
in the section "Write A Conditional Shell Script," you can only
successfully execute the file using the source command when you
run the shell that accepts that syntax. Keep in mind that this is just
like entering the commands by hand. If you cannot use a command I love Mac OS X
in your current shell because your shell does not accept the syntax,
you also cannot use the source command to run a script that
contains that command. For example, the set command assigns a
value to a variable in tcsh. This tcsh-specific command, referred to
as a built-in, makes no sense to sh and zsh. If you are a tcsh user,
you can source a file that contains a set command to establish the [ferro:˜] user% /bin/sh
sh-2.05a$ source macosx
value of a variable; this will not work in the Bourne shell.
Type the following lines into a file called macosx. Notice that the
shells do not process the file in the same way.
sh-2.05a$ source macosx
set OS = "Mac OS X" I love
echo I love $OS

– The screen displays the – The shell executes the
Á Type ls -l followed by a ° Type ./ followed by the
space. script's file permissions. name of a script and press commands in the script.
‡ Type the name of a script
and press Return.


listed. Each time through the loop, the next number is

ou can issue a command or a list of commands many
assigned to the variable $number. Following the foreach
times by placing a loop in your shell script. Adding a
command, you can enter whatever commands you want to
loop keeps you from having to run your script for
execute for each $number. You terminate your loop by
every file in a directory or every value in a list.
entering the word end on a line by itself.
When you want your script to loop through a series of files,
For some tasks, you want the number of times that you
numbers, or other values, you can use a looping command.
loop through a set of commands to depend on the value of
To ensure that the proper shell is used when your script
a certain variable or some other condition. For example, if
runs, you can add a line to the top of your script beginning
you want to write a script that adds two numbers, you can
with #! followed immediately by the full pathname for the
write it so that it adds the numbers until the person running
particular script. For example, a script written to run in
the script stops entering numbers. In this case, you use a
tcsh starts with #!/bin/tcsh.
while loop. A while loop continues executing as long as
the specified test condition is true.
For tcsh, you can use the foreach command to loop
through a set of values. The command foreach number
(4 5 6) runs three times, once for each of the numbers


› Type echo Go to work on Á Save your script, and exit ° Type ./work2day and press
$weekday and press Return. Pico. Return.
„ Start the Pico editor to
– The script executes.
ˇ Type end, press Return, ‡ Type chmod a+x work2day
create a file named work2day.
and then type echo Today is and press Return.
¤ Type #!/bin/tcsh and press
`date +%a`.
Return twice.
– Your script is complete.
‹ Type foreach weekday (
Mon Tue Wed Thu Fri ) and
press Return.


You can often reduce the lines in a script
by placing repetitious commands in a
loop. You can loop by using any of the TYPE THIS:
shells provided with Mac OS X. The
[ferro:˜] $user /bin/sh
foreach and while commands are sh-2.05a$ for weekday in Mon Tue Wed Thu Fri
built-ins associated with tcsh and csh. > do
> echo Go to work on $weekday
You must use a different syntax if you
> done
are writing scripts in bash, zsh, or sh.
The for command syntax for these
shells uses the word for, followed by
a variable name and the word in,
followed by list of values. The words do
and done mark the beginning and end Go to work on Mon
of the commands to be executed in your Go to work on Tue
Go to work on Wed
loop. Make sure that you use /bin/sh,
Go to work on Thu
/bin/bash, or /bin/zsh when you Go to work on Fri
run the following exercise that loops
through the days of the week.

› Type while $num1 != "" Á Type echo “n "number> " ° Type end and press
and press Return. and press Return. Return twice.
„ Start the Pico editor to
ˇ Type set sum = `expr $sum ‡ Type set num = $< and · Type echo $sum.
create a file named addloop.
+ $num` and press Return. press Return.
¤ Type #!/bin/tcsh and press ‚ Save your file, and exit
Return twice. Note: Use single back quotes Pico.
(backticks) in step 5.
‹ Type set num = 0; set sum
= 0 and press Return twice.


You then enter a list of commands that you want to execute

onditional statements make scripts more useful. You
if the condition you specified is true. For readability, script
can use an if command to test conditions in shell
writers usually indent these lines. After this list of
scripts and proceed with different commands
commands, end your conditional statement with the word
depending on the outcome of your test. This type of
endif on a line by itself.
operation is called conditional logic. All Unix shells provide
some form of conditional logic. However, like looping, the
In bash, sh, and zsh, the if statement takes a different
syntax depends on the shell. The if command is a built-in,
form. You follow the word if by a set of square brackets
so you cannot find a file if you enter the command which
that enclose the test condition. Follow the brackets with a
if in your Terminal window.
semicolon (;) and the word then. You can type the word
then on the following line if you prefer. You terminate the
For tcsh and csh, you follow the if command with the


. 24
( 69 .)