What Is Shell Programming PDF
What Is Shell Programming PDF
What Is Shell Programming PDF
conducted by
Mike Kupferschmid
Scientific Programming Consultant
VCC 322, x6558, kupfem@rpi.edu
What is Shell Programming?
look e
shows spelling words that begin with e
look e | more
displays the words one page at a time
unix[1] hi
Hello, world!
unix[2]
#!/bin/sh
# himike
name=Mike
echo "Hello, $name!"
exit 0
unix[3] himike
Hello, Mike!
unix[4]
#!/bin/sh
# rem
rm junk
echo "The return code from rm was $?"
exit 0
unix[16]
#!/bin/sh
# sorter
rm -f /tmp/sorted
sort $1 > /tmp/sorted
cp /tmp/sorted $1
rm -f /tmp/sorted
exit 0
unix[17] more names
Jeff
Alan
Nancy
Yossl
Scott
Harriet
Chris
unix[18] sorter names
unix[19] more names
Alan
Chris
Harriet
Jeff
Nancy
Scott
Yossl
unix[20]
#!/bin/sh
# hiyou
name=whoami
echo "Hello, $name!"
exit 0
unix[21] hiyou
Hello, kupfem!
unix[22]
#!/bin/sh
# hiyou2
echo "Hello, whoami!"
exit 0
unix[23] hiyou2
Hello, kupfem!
unix[24]
#!/bin/sh
# countem
echo "File \"$1\" contains \
exactly wc $1 | cut -c6-7 lines."
exit 0
unix[44] adder 1 2 3 4 5
The sum is 15.
unix[45]
#!/bin/sh
# fixfor
for fyle in *.for
do
new=echo $fyle | sed -e"s/\.for$/\.f/"
mv $fyle $new
done
exit 0
unix[46] ls *.for
a.for b.for pgm.for xyz.w.for
unix[47] fixfor
unix[48] ls *.f
a.f b.f pgm.f xyz.w.f
#!/bin/sh
# suffix
for fyle in *.$1
do
new=echo $fyle | sed -e"s/\.$1$/\.$2/"
mv $fyle $new
done
exit 0
unix[49] ls *.f
a.f b.f pgm.f xyz.w.f
unix[50] suffix f for
unix[51] ls *.for
a.for b.for pgm.for xyz.w.for
unix[52]
sed
reads std-in, edits line(s), writes std-out
if [ $# -eq 0 ]
then
echo "findtext word1 word2 word3 ..."
echo "lists names of files containing all given words"
exit 1
fi
for fyle in *
do
bad=0
for word in $*
do
grep $word $fyle > /dev/null 2> /dev/null
if [ $? -ne 0 ]
then
bad=1
break
fi
done
if [ $bad -eq 0 ]
then
echo $fyle
fi
done
exit 0
Gotchas
1. Write a script that counts files. (a) First make it count the
files in the current directory. (b) Now modify your script to
accept a parameter that is the name of a directory, and count
the files in that directory. Try this version on the current
directory (.) and on the /afs/rpi.edu/campus/doc directory.
(c) Further modify your script so that if it is invoked without
a parameter it prints out an explanation of how to use it.
2. If the ls command is given the name of a single extant file
it merely prints that filename back out. (a) Write a script myls
that behaves like ls except that when a single filename
parameter is supplied it produces the output that ls -l would
give for the file. (b) Revise your script so that when a single
filename parameter is given the output produced is the
filename followed by the date and time of its most recent
change and then the size of the file in bytes.
3. A script isyes is required that sets its exit code to 0 if its
parameter is some variation of y or yes, and to 1 otherwise.
(a) Assume the only acceptable parameter values meaning
yes are y, yes, Y, and YES, and solve the problem using only
shell programming features we have discussed. (b) Simplify
and generalize your script by using tr a-z A-Z, which reads
from std-in, translates to upper case, and writes to std-out.
4. Write a script that adds up the sizes reported by ls for the
files in the current directory. The script should print out only
the total number of bytes used.
References