UNIX Shell Scripts

By Abhishek
Q: What is a shell.
A: It is compiled C program that acts as an interface between the
   user and the UNIX operating system. Usually has the name /bin/sh
   on the UNIX system. It is executed automatically when you log on 
   to the UNIX system and then it interprets commands typed at the
   terminal.

Q: What is a shell script.
A: Shell scripts are ordinary files that contain UNIX commands. When 
   it is run, all the commands in it get executed. However the shell 
   script file must be made executable using the chmod command.

Q: Write a shell script that counts the number of users logged on.
A: See the following example

$ cat whoson
#----------------------------------------
# this shell script prints date and the
# number of users logged on.
# ---------------------------------------
date
echo The number of users logged on:
who | wc -l # Use the word count to count the number of users

$ whoson
Wed Aug  4 16:18:15 CDT 1999
The number of users logged on:
45           

Q: How do you pass parameter/arguments to a shell script.
A: Words appearing on the command line after the name of the shell
   script are automatically stored as the arguments in that order.
   There are some special shell variables that can be used in a 
   shell script to refer to the arguments. $1 refers to the first
   argument, $2 refers to second and so on. $* refers to all the
   arguments while $# refers to the total number of arguments passed.
   See the example below:

$ cat args
#-----------------------------------------------------------------------------
# This script accepts the argument and echoes it back.......
#-----------------------------------------------------------------------------
echo The name of the script is : $0
echo The first argument is: $1
echo The second argument is: $2
echo Total number of arguments: $#
echo All Arguments: $*

$ args asd fgh hjjk
The name of the script is : args
The first argument is: asd
The second argument is: fgh
Total number of arguments: 3
All Arguments: asd fgh hjjk 

Q: What is command substitution. What is its use.
A: It a form of shell processing in which anything 
   between the accent graves (back Quote) is processed as a 
   UNIX command. It allows the output of a command to be stored
   in a varibale. It also allows the standard output os a command
   to be used as an argument to another command. It is mostly
   used in shell scripts. EG:

$ date
Thu Aug  5 10:36:13 CDT 1999
$ value=`date`
$ echo The Date is $value
The Date is Thu Aug 5 10:35:33 CDT 1999

Q: How do you place interactive commands in a shell.
A: By using the HERE DOCUMENT. It uses << to have the input redirected
   by the shell from HERE. It needs a delimiter so the shell knows when
   the document starts and finishes. EG:


$ echo this is line 1
this is line 1
$ echo this is line 2
this is line 2
$ echo << !
> This is line 1
> This is line 2
> !
 
Q: How do you do aritmatic calculations in a shell script.
A: By using the UNIX calculater bc used in conjunction with HERE DOCUMENT
   (<<) and the command substitution (`). EG:

$ cat temp
# This shell script demostrates the use of HERE DOCUMENT
# and the UNIX calculater bc. Since the UNIX calculater
# is interactive, we will have to use << (here document)
# and ` (Command Substitution). scale=2 makes bc use 2 decimal places.    

var_result=`bc << !
2 + 5
!`
echo The result is $var_result

$ temp
The result is 7  

Q: Give an example of a shell script that uses bc, << and `.
A: EX:

$ cat pizzatime

cat << !
            MENU:
                              `date`
Select one of the following:
1) Plain Pizza
2) Mushroom Pizza
3) Pepperoni Pizza
4) White Pizza
5) The Works
!
echo Selection: "\c"
read var_choice
echo How many:  "\c"
read var_qty
cost=`bc << !
$var_choice * $var_qty
!`
echo Total cost is "$cost" for "$var_qty" selection "$var_choice" choice  
$ pizzatime
            MENU:
                              Thu Aug  5 11:29:32 CDT 1999
Select one of the following:
1) Plain Pizza
2) Mushroom Pizza
3) Pepperoni Pizza
4) White Pizza
5) The Works
Selection: 3
How many: 4
Total cost is 12 for 4 selection 3 choice       

Q: How do you direct shell to take the special characters literally.
A: By preceding the character with a backslash. The shell removes
   the backslash before execution and takes the next character literally
   even if it is a special character. EG:

$ echo $HOME
/cnir/achaudha
$ echo \$HOME
$HOME 
$ echo date is `date`
date is Thu Aug 5 12:45:28 CDT 1999
$ echo date is \`date\`
date is `date`
$ echo 'date is `date`'
date is `date`
$ echo "date is `date`"
date is Thu Aug  5 12:46:15 CDT 1999  

Q: What is the difference between single, double and back quotes.
A: Single and double quotes are same except that the single quotes
   does not allow command substitution and it turns off the meaning
   of $ so that the shell views variables as a literal.
   Back quotes are used as command substitution. EG:


$ echo home directory is $HOME
home directory is /cnir/achaudha
$ echo 'home directory is $HOME'
home directory is $HOME
$ echo "home directory is $HOME"
home directory is /cnir/achaudha
$ echo home directory is `$HOME`
ksh: /cnir/achaudha: cannot execute
home directory is
$

Q: How can you test if a command is successfull.
A: Commands and shells return an exit status that is stored in a special
   shell variable $?. A shell script can use it to test if the command
   executed successfully. EG:

$ echo $?
0
$ ls -8
ls: illegal option -- 8
usage: ls -1ARadeCxmnlogrtucpFLbqisf [files]
$ echo $?
2
 
Q: How can you debug a shell script.
A: By using 'set -vx' command. The 'v' option displays all the commands
   that are executed and 'x' displays the contents of the variables. EG:

$ cat debug
set -vx
set `date`
echo "The day   is $1 \n"
echo "The month is $2 \n"
echo "The year  is $6 \n"
$ debug
set `date`
+ date
+ set Tue Sep 7 14:44:07 CDT 1999
echo "The day   is $1 \n"
+ echo The day   is Tue \n
The day   is Tue

echo "The month is $2 \n"
+ echo The month is Sep \n
The month is Sep

echo "The year  is $6 \n"
+ echo The year  is 1999 \n
The year  is 1999
Q: What is a 'set' command.
A: 'set' is a shell built in command that does too many things:
1) With no arguments, it shows the values of variables in the environment.
2) Ordinary arguments reset the values of $1, $2 etc.
3) It can be used to debug a shell script using 'set -vx'.
EG:
$ set abhishek vivek
$ echo $#
2
$ echo $1
abhishek
$ echo $2
vivek
$ set `date`
$ echo $6
1999
$ echo $*
Tue Sep 7 14:57:11 CDT 1999
$
Q: Show how if-else-fi is used in a shell script:
A: EG:

$ cat temp
# This shell prints a message if today is friday (FRI or fri or Fri)
# The output of the date command is sent to a null_file as
# we do no want it to be printed on screen.

if date | grep -e fri -e Fri -e FRI > null_file
then
echo "Today is friday. Have a nice weekend"
else
echo "Work !! today is `date`"
fi

$ temp
Work !! today is Thu Aug  5 13:36:19 CDT 1999
$    

Q: What is test command.
A: The test command is used to evaluate one ore more conditions. It is
   used for numeric or string comparasions or to check file status. It is
   often used with if construct and does not produce standard output.

$ cat temp
# This shell prints a message if today is friday (FRI or fri or Fri)

date | grep -e fri -e Fri -e FRI > null_file
if test $? = 0
then
echo "Today is friday. Have a nice weekend"
else
echo "Work !! today is `date`"
fi

$ temp
Work !! today is Thu Aug  5 13:36:19 CDT 1999
$    

Q: Give an example of a shell script that uses while construct.
A: 

echo 'Do you want to add name or address \c'
read option
while test "$option" != name -a "$option" != address
do
 echo Incorrect response. Please type \"name\" or \"address\" '\c'
 read option
done

if test  "$option" = name
then
 echo Please write you name '\c'
 read var_name
 echo $var_name
 if test "$var_name" =  -n
 then
  echo "name should not be null"
  exit 10
 else
  echo "$var_name" >> name_file
  echo "$var_name has been added"
 then
  echo "name should not be null"
  exit 10
 else
  echo "$var_name" >> name_file
  echo "$var_name has been added"
 echo Please write your address '\c'
 read var_add
 if test "$var_add" =  -n
 then
  echo "Address should not be null"
  exit 10
 else
  echo "$var_add" >> add_file
  echo "$var_add has been added"
 fi
fi             

Q: Give an example of FOR loop.
A: See the example below. The loop goes around once for each word on 
the IN list. EX:

$ cat temp
for var_welcome
in abhishek vivek
do
        echo "Welcome $var_welcome"
done
$ temp
Welcome abhishek
Welcome vivek   

Q: What happens if IN is missing in FOR construct.
A: When the in keyword is missing, the FOR loop uses command line arguments
   (positional parameters) and executes the body of the loop once for each
   argument. If there are no arguments, the loop is not executed at all. EX:

$ cat temp
for var_welcome
do
        echo "Welcome $var_welcome"
done 
$ temp a c b n
Welcome a
Welcome c
Welcome b
Welcome n
$ temp `date`
Welcome Thu
Welcome Aug
Welcome 5
Welcome 16:55:12
Welcome CDT
Welcome 1999       


Go To Top

Send me an email