Example of using an if construct

To carry out a conditional action:

   if who | grep -s keith > /dev/null
   then
   echo keith is logged in
   else
   echo keith not available
   fi

This lists who is currently logged on to the sytem and pipes the output through grep to search for the username keith.

The -s option causes grep to work silently and any error messages are directed to the file /dev/null instead of the standard output.

If the command is succesful i.e. the username keith is found in the list of users currently logged in then the message

   keith is logged on

is displayed, otherwise the second message is displayed.


[Home] [Search] [Index] This site maintained by system@gantep.edu.tr