<<

. 25
( 69 .)



>>

list of commands to be executed with fi.
condition you want to test enclosed in parentheses,
followed by the word then. To test if a number is equal to
You can also specify a list of commands that should run if
0, for example, your command is if ( $number == 0 )
the specified conditions are not met. In both forms of if
then. To test whether a variable contains a particular string,
commands, you use the word else to begin this list. An
enclose the string in quotes: if ( $answer == "yes" )
else class must follow the if clause that it is related to.
then.

WRITE A CONDITIONAL SHELL SCRIPT




‹ Type set day=`date +%a` ˇ Type echo Sleep in and ‡ Type echo Go to work and
USING TCSH
and press Return twice. press Return. press Return.
„ Start the Pico editor to
Á Type else and press ° Type endif.
create a file named Note: Use single back quotes
work2day3. (backticks) in step 3. Return.
· Save your file, and exit
¤ Type #!/bin/tcsh and press › Type if ( $day == "Sat" || Pico.
Return twice. $day == "Sun" ) then and press
Return.
114
8
AUTOMATE SHELL TASKS



It is often useful to collect information TYPE THIS:
from the system by running a
[ferro:˜] user% foreach FILE ( 'ls -l' )
command and assigning its output to
foreach -> echo $FILE is a file
a variable. The work2day scripts in foreach -> end
this chapter use backticks ( `) for this.
The output from the command
enclosed in backticks is assigned to
RESULT:
the variable and can then be
displayed or tested. total is a file
64 is a file
You can use backticks to run almost -rw-r”r” is a file
any simple Unix command. However, 1 is a file
user is a file
you should always take care to
staff is a file
consider the output that will be 134 is a file
assigned to your variable. It may be May is a file
5 is a file
different than you expect. Be sure 07:21 is a file
you use tcsh or csh when you addloop is a file
loop on the command line.




‹ Type day=`date +%a` and ˇ Type echo Sleep in and Á Type echo Go to Work and
USING TCSH
press Return. press Return, then type else press Return, then type fi.
„ Start the Pico editor to and press Return again.
‡ Save your file, and exit
create a file named Note: Use single back quotes
work2day4. (backticks) in step 3. Pico.
¤ Type #!/bin/sh and press › Type if [ $day == "Sat" ] || [
Return twice. $day == "Sun" ]; then and
press Return.
115
UNIX FOR MAC




EXTRACT INFORMATION WITH AWK
programming language, it is most popularly used within

Y
ou can use the awk command to extract text from
shell scripts or on the command line to select data from
files or from the output of other commands. It is
files or the output of other commands.
particularly useful for extracting a portion of each
line. You can then use the output from awk for further
Any command that you write for awk and awk scripts
processing.
begins with an opening brace ({) and ends with a closing
brace (}). The awk command processes lines of text one at
Like many other Unix commands, awk works well with the
a time and assigns each portion of a line to a variable such
concept of white space ” any combination of spaces and
as $1, $2, and so on. The awk command language has some
tabs that might exist between strings of text and numbers. If
built-in variables such as NF and NR that represent the
you want to display the first or the third word on every line
number of fields on each line and the record number of the
of a text file, awk is the tool to use. If you want to display
current line. Fields are portions of text separated by white
the last word on each line, awk can handle that as well. If
space or some other delimiter that you specify.
the information that you want to display is not separated by
white space, you can still use awk to extract it.
You can write many useful scripts and commands using
awk. Awk is a scripting language that includes conditional
You can use awk on the command line, or you can write
tests and looping.
scripts using nothing but awk commands. Though awk is a


EXTRACT INFORMATION WITH AWK




‹ Type the name of a text › Type ls -l followed by a Á Type awk '{print $5}' | and
USING AWK ON THE
COMMAND LINE file and press Return. space. press Return.
„ Type awk followed by a – Awk prints the first word of – Awk prints the size of each
ˇ Type | followed by a
space. every line of your file. space. file in the current directory.
¤ Type '{print $1}' followed
by a space.


116
8
AUTOMATE SHELL TASKS


Some features of the awk language give it a very different appearance than
shell scripts. For example, variable names in awk do not start with a $. The
variables $1, $2, and so on represent fields in the text being processed. If
you use a variable name that has no value, awk ignores the command and
continues processing.
The command to display lines of text in awk is print, not echo. You must
include lines of text in single quotation marks. Awk uses { and } markers to
begin and end blocks of code.

TYPE THIS:
TYPE THIS:
[ferro:˜] cat work2day | awk '{print $NF}'
[ferro:˜] cat work2day | awk '{print NF}'




RESULT:
RESULT:
#!/bin/tcsh
1
0
)
9
$weekday
6
end
1
+%a'
5




‹ Type set USERCOUNT ˇ Save your file, and exit ‡ Type ./count_users and
USING AWK IN A SCRIPT
Pico. press Return.
=`uptime | awk '{print $4}'`
„ Start the Pico editor to and press Return twice.
– Your script runs.
Á Type chmod a+x
create a file named
› Type if ( $USERCOUNT >
count_users. count_users and press Return.
10) then and press Return,
¤ Type #!/bin/tcsh and press then type echo $USERCOUNT
Return twice.
users logged in and press
Return again, and type endif.
117
UNIX FOR MAC

EXTEND SCRIPTS WITH SED
command. The command sed "s/a/z/g" changes every

T
he sed command lets you modify the input or output
occurrence of the letter a in its input to the letter z in its
of a Unix command or the content of a file without
output.
having to use a text editor. Sed is a special editor that
is unlike Pico, emacs, and vi. It can change the contents of
If you want to change a number of strings at the same time,
files or the output of other commands on the command
create a file containing all of your substitutions. The
line. Think of sed as a pipe into which you pour text and
command sed -f subs < infile > outfile looks
out of which modified text flows. The shape of this pipe,
for a series of change requests in the file subs, applies
representing a set of editing commands that you specify,
these changes while processing the file called infile, and
determines what text changes and how that text changes.
writes the changed text to outfile.
The simplest sed command replaces one string with
If the text that you want to change includes a forward slash
another string. The command echo Please go to work
(/), you can see where sed might be confused as to what
today | sed "s/go to work/stay home/" prints
portion of text it needs to change. However, you can specify
"Please stay home today".
a different delimiter to separate the old and new text. The
command sed "s:old:new:" works as well as sed
Sed changes one instance of the first string to the second
"s/old/new/". Whichever delimiter you use, be sure to
on each line. If you want the same text to change every
use three of them.
time it appears in a line, add the letter g to the end of the

EXTEND SCRIPTS WITH SED




‹ Type Don't forget to take ˇ Type cat message followed ‡ Type sed "s/go to work/stay
USING SED ON THE
COMMAND LINE by a space. home/" to replace one piece
your lunch.
„ Start the Pico editor to of text with the other and
› Save your file, and exit Á Type | followed by a press Return.
create a file named message. Pico. space.
– The screen displays text
¤ Type Please go to work
that asks you to stay home
today. and press Return.
today.




118
8
AUTOMATE SHELL TASKS


As with awk, you can use TYPE THIS INTO A FILE NAMED ONELINE:
sed to create fairly
complex scripts, but you #!/bin/sed -nf
H
most often use the ${
command to extend shell x
s/\n//g
scripts. Scripts written
p
entirely in sed can be }
simple or extremely
challenging. Try a simple
TYPE THIS:
sed script like that shown
here. Enter this script in a [ferro:˜] echo a > mytext; echo b > mytext; echo c > mytext
file called oneline and [ferro:˜] user% ./oneline < mytext
make it executable. It
joins lines of a file into a
RESULT:
single line by removing
linefeeds. Then, try the abc
following commands.

<<

. 25
( 69 .)



>>