Shell Programming

Download Report

Transcript Shell Programming

Shell Programming – Part 2
Looping with a List: for Statement
for lets a set of instructions be repeated on a set of items in a list.
The general form is:
for variable in list
do
commands
done
Example:
for file in file1 file2 file3
do
echo $file has `wc –l` lines
echo $file has `wc –c` chars
done
Whitespace separates items in the list. Use quotes if items have whitespace.
A for List’s Items May Come from Anywhere
for list items may come from variables or wildcards as in this example.
# This shell program renames files with
# a txt extension to files with a doc extension
# e.g., rick.txt  rick.doc
for file in *.txt
do
leftname=`basename $file .txt`
mv $file ${leftname}.doc
done
Wildcards are interpreted by the shell as filenames.
Looping While a Condition Is True: while Statement
while lets a set of instructions be repeated as long as a condition is true.
The general form is:
while condition is true
do
commands
done
Example:
x=5
while [ $x –gt 0 ] ; do
ps –e; sleep 3
x=`expr $x – 1`
done # repeats 5 times
The continue statement lets you end one iteration of the loop and start
the next iteration immediately.
The break statement lets you completely exit the loop immediately.
Try These while Examples
Problems 1 and 2 of the In-Class Lab:
1. Modify program so that it continuously runs until the user
selects the exit option.
2. Create a shell program so that a “database” file of users
and their telephone numbers are created.
Using set to Manipulate Positional Parameters
set -- command assigns each of the items in command
a positional parameter.
Example: set -- `date` implies $1 is Mon, $2 is Nov, $3 is 10, $4
is 09:40:35 , $5 is EST, and $6 is 2010.
Let’s write a shell script, mydate.sh, that takes outputs the date in
the format:
Today is Mon, the 28th day of the month of Nov in 2011.
Try this example: mytime.sh
Write a shell program, mytime.sh, that displays the time in the format:
The time is 15 minutes past the hour of 10.
Hints:
1. Grab the time from the date command positional parameter.
2. Save the time in a file.
3. Use the cut command to get the hours and minutes of the time.
4. Format the time as above using the hours and minutes.
Using shift to Manipulate Positional Parameters
shift n transfers the contents of each positional parameter to
its immediate lower numbered one. If a number n is
present, this is done n times.
Example: set -- `date` implies $1 is Mon, $2 is Nov, $3 is 10, $4
is 09:40:35 , $5 is EST, and $6 is 2010.
shift
echo $1 $2 $3 displays $1 as Nov, $2 as 10 and $3 as
09:40:34
myln.sh
#!/bin/sh
# This program creates multiple symbolic links to a
# single target. Usage: myln.sh target list of files
#
original=$1
# target file
[ ! –f $original] && { echo “$original not found” ;
exit 1 ; }
shift
# left-most argument (target) is lost
for file in $* ; do
ln –sf $original $file
done
An Alternative to alias: Shell Functions
Shell functions execute a group of statements enclosed within {}
General form:
functionName() {
statements
return value
}
Example: ls –l files | more
# always a null parameter list
# optional number, indicating
# success or failure

myls() {
> ls –l $* | more
> }
myls *.c
Debugging Shell Scripts
set can be used to produce debugging output:
set -o verbose
set –o xtrace
verbose Echoes each command before running them to stderr
xtrace
Echoes each command after command-line processing,
after parameter and command substitution, and the other
subsequent steps.
Each line it prints starts with + which is customizable
through the built-in shell variable PS4. So you can set
PS4 to "xtrace-> "
Some Final Tips
• To use the output of a UNIX command elsewhere in script, type a $,
enclose the command within parentheses (), and store the output in an
environment variable, e.g., VAR1=$(command1|command2)
• To use a value of an environment variable, put a $ in front of the variable
name and to avoid ambiguities, enclose the variable name inside curly
braces {}, e.g., ls ${VAR1}
• See the text for more useful topics: trap, eval, exec, awk, sed
we didn’t cover in class.