Shell Programming

Examples of simple shell scripts:

 

(/home/cs476/shells/BourneShell/) &

(/home/cs476/shells/BourneShell/linux)

 

Example1:  man2pdf  converts a man page to pdf

 

#! /bin/sh

troff -man  /usr/share/man/man1/$1.1 > t2
cat t2 | dpost > t3
ps2pdf t3  $1.pdf


Example2:  man2pdfs  simple version of man2pdf

 

#! /bin/sh

troff -man /usr/share/man/man1/$1.1 | dpost | ps2pdf - $1.pdf 
 

Note that man2pdfs does not use temporary files.

(thus you do not have to worry about permissions to create such files and deleting them at the end)

 

Introduction to Bourne Shell:

 

for more details see man page for sh (% man sh)  (sh.pdf).

 

Loop Statements:

 

while  list        until   list      for  name  [ in word ... ]
do                    do                 do

    list                   list                  list

done             done        done

 

forexample

for i

do

     echo $i

done

 

% forexample 1 2 a b

1

2

a

b

 

Conditional Statements:

 
if  list                       case  word   in
then                             pattern)     list ;;
      list                         pattern)     list ;;
[ elif   list                          .........
  then                       esac
       list  ] ...
[ else
       list  ]
fi  

 

Misc. Statements:

 

test:  Evaluates conditional expressions

e.g.:    test $i -le 2
returns 0 (true) if  $i  is less than or equal to 2.

 

expr: Evaluates arithmetic expression

e.g.:    i=`expr $i + 1`
increments i by 1 (like i++ in C).

 

read: Reads one line from standard input

read  var  
reads a line from stdin and assign it to var

e.g.:  readexample

while read line

do

   echo $line

done

% readexample < readexample

 

echo: Writes to the standard output

e.g.,:  echo  "the line read is:" $line
writes:  the line read is:  <the content of  var>

 

set: Assigns values to positional parameters $1, $2, ...

e.g.:  set hello world
                echo $2 

                world   (the value is of $2 is: world)

 

IFS: Internal Field Separators

e.g.:   IFS=@
    set hello@world
    echo $2  

         world   (the value is of $2 is: world)

 

eval: Executes a command

e.g.:  cmd="ls -l"
    eval  $cmd

 

Example 3:  man-to-pdf converts a man page to pdf

 

if  test $# -eq 0

then

    echo "usage man-to-pdf <cmd>"

    echo "e.g., man-to-pdf touch"

    exit

fi

if whereis $1 | grep man > /dev/null

then

    filepath=` whereis $1 | tr ' ' '\n' |

               grep man | head -1 `

    filename=` basename $filepath`

else

   echo "man page for $1 is not found"

    exit

fi

troff -man $filepath  > /tmp/t1$$

echo ..done troff

cat /tmp/t1$$ | dpost > /tmp/t2$$

echo ..done dpost

ps2pdf /tmp/t2$$  $HOME/$filename.pdf

echo ..done ps2pdf

echo "the pdf file is:"

ls -lt $HOME/$filename.pdf

rm /tmp/t1$$ /tmp/t2$$

echo DONE

 

Example 4:  man-to-pdfs  simple version of  man-to-pdf

                            that does not use tmp files:

 

troff -man $filepath  | dpost |  ps2pdf - $HOME/$filename.pdf

 

Replaces:

 

troff -man $filepath  > /tmp/t1$$

cat /tmp/t1$$ | dpost > /tmp/t2$$

ps2pdf /tmp/t2$$  $HOME/$filename.pdf

 

 

Example 5:  mail-to-group

 

This program mails a file to a group of users.

The first attribute is the file name, followed by the recipients’ email.

 

echo ‘Usage : mail-to-group letter u1 u2 ....
//send letter to users u1 u2 ..’
letter=$1
shift
for i  
do
    Mail -s "shell example" $i < $letter
    echo Mailed $letter to $i
done

 

E.g.:

% mail-to-group  mail-to-group    wahab    cs476

Will mail the “mail-to-group” file to both wahab and cs476.

 

% mail-to-group  mail-to-group  `cat list`

Will  mail  the file to each person listed on ‘list’.

 

Example 6:  mail-to-group-csh  (csh version ) 

 

This is the same as the previous example but uses csh.

 
#! /bin/csh
set letter=$1
set total=$#argv
set index=2
while ($index <= $total)
 Mail –s "shell example" $argv[$index] < $letter
 echo mailed $letter to $argv[$index]
 @ index++
end
 

Example 7:  when-login

This program informs you when a specific user login to a machine.

It has one argument, the user to watch for.

 
until who | grep $1 
do
   sleep 3
done
 

E.g.:

% when-login  wahab

Will  return if wahab is login.

 

Example 8:  who-login

This programs gives a list of users currently login, sorted by login time.

 

who | sortout  

 

sortout 
    
while :
    do
if read line
then
set `echo $line`
echo $3 $4 $1 >> /tmp/t$$  
   /* $3 day $4 time $1 user*/ 
      else
    sort /tmp/t$$
    rm /tmp/t$$
    exit
 fi
done

 

Example 9:  count-files

This program counts the number of files under a  subtree.

The only argument is the root of the subtree.

 

echo ‘Usage: count-files <path>’
touch /tmp/t$$
find $1 -type f -exec /usr/bin/echo "1\c" >> /tmp/t$$ \;
wc -c < /tmp/t$$ 
rm /tmp/t$$ 
 

E.g.:

% count-files   /home/cs476

 

Example 10:  scan-dir  

This program recursively scans a directory and prompts the users

for a command to execute on each file under the directory.

 

case $# in
   0) dir=. ;;
   1) dir=$1 ;;
esac
echo .... scanning directory $dir
cd $dir

for i in *
do
       if test -d $dir/$i
     then
         $0   $dir/$i
     else
       ls -l $i
       echo -n type any command: 
       read command
       eval $command
       fi
done

 

E.g.:

% scan-dir /home/cs476/public_html/shells/BourneShell/test-scan-dir