Shell Programming

 

 

Examples of simple shell scripts:  (/home/cs476/shells/BourneShell/)

 

 

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

 

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

 

 

 

Example:  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

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

 

expr: Evaluates arithmetic expression

         example:    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

example:  readexample

 

while read line

do

   echo $line

done

 

                 % readexample < readexample

 

echo: Writes to the standard output

    example:  echo  "the line read is:"   $var
   writes:  the line read is:  <the content of  var>

 

 

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

    example:  set hello world
                   echo $2 

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


IFS: Internal Field Separators

     example:  IFS=@
                      set hello@world
                      echo $2  

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


eval: Executes a command

     example:  cmd="ls -l"
                       eval  $cmd

 

Examples of  Bourne Shell Scripts

The examples are available at: /home/cs476/public_html/shell/BourneShell

 

ê    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

 

Ø 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

 

 

 

ê    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

 

Examples:

% 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’.

 

 

Ø mail-to-group-csh : (csh version ) 

 

       This is the same as the previous example but uses csh instead of Bourne shell.

 

#! /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
 

ê    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
 

Example:

% when-login  wahab

Will  return if wahab is login.

 

 

 

ê    who-login:

 

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

 

#! /bin/sh
echo ‘Usage: who-login’

who | sortout  
 

sortout

 

#! /bin/sh
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

 

ê    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$$ 
 

Example:

% count-files  /home/cs476

 

ê    scan-dir :

 

This program recursively scans a directory

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

 

#! /bin/sh
echo ‘Usage: scan-dir [path]’ 
 
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

 

Example:

 

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