Shell Scripting Lab 1
Shell Scripting Lab 1
SHELL PROGRAMMING 1
EVEN OR ODD
• AIM:
To write a program to find whether a number is even or odd .
ALGORITHM:
• STEP 1: Read the input number.
• STEP 2: Perform modular division on input number by 2.
• STEP 3: If remainder is 0 print the number is even.
• STEP 4: Else print number is odd.
• STEP 5: Stop the program.
Lab3
PROGRAM
$ echo "enter the number"
$ read num
$ if [ `expr $num % 2` -eq 0 ]
$ then
$ echo "number is even"
$ else
$ echo "number is odd"
$ fi
Lab3
OUTPUT
• enter the number: 5
• the number is odd.
RESULT:
• Thus the program has been executed successfully.
• Note: before running the program make sure that you have the permission
to execute.
• Run the program by using the command
• $ ./ filename
Lab 3
SHELL PROGRAMMING 2
• BIGGEST OF TWO NUMBERS
• AIM :
• To write a program to find biggest in two numbers.
• ALGORITHM :
• STEP 1: Read The Two Numbers.
• STEP 2: If Value Of A Is Greater Than B Is Big.
• STEP 3: Else Print B Is Big.
• STEP 4: Stop The Program.
PROGRAM
$ echo "enter the number"
$ read a b
$ if [ $a -gt $b ]
$ then
$ echo "A is bigger than B"
$ else
$ echo "B is bigger than A"
$ fi
Lab 3
OUTPUT:
• Enter The Two Number:
• 23 67
• B is Big.
• RESULT:
• Thus the program has been executed successfully.
Lab 4
LAB 4
• Shell Comments:
• You can put your comments in your script as follows:
• Read-only Variables:
• The shell provides a way to mark variables as read-only by using the read-only
command. After a variable is marked read-only, its value cannot be changed.
• For example, following script would give error while trying to change the value of
NAME:
• #!/bin/sh
• NAME="Zara Ali"
• readonly NAME
• NAME="Qadiri"
• This would produce following result:
• /bin/sh: NAME: This variable is read only.
LAB 4
• Unsetting Variables:
• Unsetting or deleting a variable tells the shell to remove the variable from the list
of variables that it tracks. Once
• you unset a variable, you would not be able to access stored value in the variable.
• Following is the syntax to unset a defined variable using the unset command:
• unset variable_name
• Above command would unset the value of a defined variable. Here is a simple
example:
• #!/bin/sh
• NAME="Zara Ali“
• unset NAME
• echo $NAME
• Above example would not print anything. You cannot use the unset command to
unset variables that are marked readonly.
LAB 4
Variable Types
When a shell is running, three main types of variables are present:
• Local Variables: A local variable is a variable that is present within the
current instance of the shell. It is not available to programs that are started
by the shell. They are set at command prompt.
• Environment Variables: An environment variable is a variable that is
available to any child process of the shell. Some programs need
environment variables in order to function correctly. Usually a shell script
defines only those environment variables that are needed by the programs
that it runs.
• Shell Variables: A shell variable is a special variable that is set by the shell
and is required by the shell in order to function correctly. Some of these
variables are environment variables whereas others are local variables.
LAB 4
Unix-Special Variables
• Previous tutorials warned about using certain non-alphanumeric
characters in your variable names. This is because those characters
are used in the names of special Unix variables. These variables are
reserved for specific functions.
• For example, the $ character represents the process ID number, or
PID, of the current shell:
• $echo $$
• Above command would write PID of the current shell:
• 29949
• The following table shows a number of special variables that you can
use in your shell scripts:
LAB 4
• Variable Description
• $0 The filename of the current script.
• $n These variables correspond to the arguments with which a script was invoked.
Here n is a positive decimal number corresponding to the position of an
argument (the first argument is $1, the second argument is $2, and so on).
• $# The number of arguments supplied to a script.
• $* All the arguments are double quoted. If a script receives two arguments, $* is
equivalent to $1 $2.
• $@ All the arguments are individually double quoted. If a script receives two
arguments, $@ is equivalent to $1 $2.
• $? The exit status of the last command executed.
• $$ The process number of the current shell. For shell scripts, this is the process ID
under which they are executing.
• $! The process number of the last background command.
LAB 4
• Command-Line Arguments:
• The command-line arguments $1, $2, $3,...$9 are positional parameters, with $0 pointing to the actual
command, program, shell script, or function and $1, $2, $3, ...$9 as the arguments to the command.
• Following script uses various special variables related to command line:
• #!/bin/sh
• echo "File Name: $0"
• echo "First Parameter : $1"
• echo "First Parameter : $2"
• echo "Quoted Values: $@"
• echo "Quoted Values: $*"
• echo "Total Number of Parameters : $#"
• Here is a sample run for the above script:
• $./test.sh Zara Ali
• File Name : ./test.sh
• First Parameter : Zara
• First Parameter : Ali
• Quoted Values: Zara Ali
• Quoted Values: Zara Ali
• Total Number of Parameters : 2
• Special Parameters $* and $@:
LAB 4
• There are special parameters that allow accessing all of the command-line arguments at once. $* and $@
both will act the same unless they are enclosed in double quotes, "". Both the parameter specifies all
command-line arguments but the "$*" special parameter takes the entire list as one argument with spaces
between and the "$@" special parameter takes the entire list and separates it into separate arguments. We
can write the shell script shown below to process an unknown number of command-line arguments with
either the $* or $@ special parameters:
• #!/bin/sh
• for TOKEN in $*
• do
• echo $TOKEN
• done
• There is one sample run for the above script:
• $./test.sh Zara Ali 10 Years Old
• Zara
• Ali
• 10
• Years
• Old
• Note: Here do...done is a kind of loop which we would cover in subsequent tutorial.
LAB 4
• Exit Status: The $? variable represents the exit status of the previous command.
• Exit status is a numerical value returned by every command upon its completion. As a rule, most commands
return an exit status of 0 if they were successful, and 1 if they were unsuccessful. Some commands return
additional exit statuses for particular reasons. For example, some commands differentiate between kinds of
errors and will return various exit values depending on the specific type of failure. Following is the example of
successful command:
• $./test.sh Zara Ali
• File Name : ./test.sh
• First Parameter : Zara
• First Parameter : Ali
• Quoted Values: Zara Ali
• Quoted Values: Zara Ali
• Total Number of Parameters : 2
• $echo $?
• 0
• $
LAB 4
Unix – Using Arrays
• A shell variable is capable enough to hold a single value. This type of
variables are called scalar variables.
• Shell supports a different type of variable called an array variable that
can hold multiple values at the same time.
• Arrays provide a method of grouping a set of variables. Instead of
creating a new name for each variable that is required, you can use a
single array variable that stores all the other variables.
• All the naming rules discussed for Shell Variables would be applicable
while naming arrays.
LAB 4
• Defining Array Values:
• The difference between an array variable and a scalar variable can be explained as follows.
• Say that you are trying to represent the names of various students as a set of variables. Each of
the individual
• variables is a scalar variable as follows:
• NAME01="Zara"
• NAME02="Qadir"
• NAME03="Mahnaz"
• NAME04="Ayan"
• NAME05="Daisy"
• We can use a single array to store all the above mentioned names. Following is the simplest
method of creating an array variable is to assign a value to one of its indices. This is expressed as
follows:
• array_name[index]=value
• Here array_name is the name of the array, index is the index of the item in the array that you
want to set, and value is the value you want to set for that item.
LAB 4
• As an example, the following commands:
• NAME[0]="Zara"
• NAME[1]="Qadir"
• NAME[2]="Mahnaz"
• NAME[3]="Ayan"
• NAME[4]="Daisy"
LAB 4
• Accessing Array Values:
• After you have set any array variable, you access it as follows:
• ${array_name[index]}
• Here array_name is the name of the array, and index is the index of the value to be accessed.
Following is the simplest example:
• NAME[0]="Zara"
• NAME[1]="Qadir"
• NAME[2]="Mahnaz"
• NAME[3]="Ayan"
• NAME[4]="Daisy"
• echo "First Index: ${NAME[0]}"
• echo "Second Index: ${NAME[1]}"
• This would produce following result:
• $./test.sh
• First Index: Zara
• Second Index: Qadir
LAB 4
• You can access all the items in an array in one of the following ways:
• ${array_name[*]}
• ${array_name[@]}
• Here array_name is the name of the array you are interested in. Following is the simplest example:
• NAME[0]="Zara"
• NAME[1]="Qadir"
• NAME[2]="Mahnaz"
• NAME[3]="Ayan"
• NAME[4]="Daisy"
• echo "First Method: ${NAME[*]}"
• echo "Second Method: ${NAME[@]}"
• This would produce following result:
• $./test.sh
• First Method: Zara Qadir Mahnaz Ayan Daisy
• Second Method: Zara Qadir Mahnaz Ayan Daisy
LAB 4
Unix - Basic Operators
• There are various operators supported by each shell. Our tutorial is based on default shell
(Bourne) so we are going to cover all the important Bourne Shell operators in the tutorial.
• There are following operators which we are going to discuss:
• Arithmetic Operators.
• Relational Operators.
• Boolean Operators.
• String Operators.
• File Test Operators.
• Here is simple example to add two numbers:
• val=`expr 2 + 2`
• echo "Total value : $val"
• This would produce following result:
• Total value : 4
• There are following points to not
LAB 4
• There are following points to note down:
• There must be spaces between operators and expressions for
example 2+2 is not correct, where as it should be written as 2 + 2.
• Complete expression should be enclosed between ``, called inverted
commas.
LAB 4
a=10
b=20
+ Arithmetic Operators Addition - Adds values on either side of the operator
`expr $a + $b` will give 30
• - Subtraction - Subtracts right hand operand from left hand operand `expr $a - $b` will give -10
* Multiplication - Multiplies values on either side of the operator `expr $a * $b` will give 200
/ Division - Divides left hand operand by right hand operand `expr $b / $a` will give 2
% Modulus - Divides left hand operand by right hand operand and
returns remainder `expr $b % $a` will give 0
= Assignment - Assign right operand in left operand a=$b would assign value
of b into a
== Equality - Compares two numbers, if both are same then returns true. [ $a == $b ]
would return false.
!= Not Equality - Compares two numbers, if both are different then returns true.
[ $a != $b ] would return true.
LAB 4
• It is very important to note here that all the conditional expressions would be put
inside square braces with one spaces around them, for example [ $a == $b ] is
correct where as [$a==$b] is incorrect.
• All the arithmetical calculations are done using long integers.
• Example: Here is an example which uses all the arithmetic operators:
• #!/bin/sh
• a=10
• b=20
• val=`expr $a + $b`
• echo "a + b : $val"
• val=`expr $a - $b`
• echo "a - b : $val"
• val=`expr $a \* $b`
• echo "a * b : $val"
LAB 4
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
• This would produce following result:
• a + b : 30
• a - b : -10
• a * b : 200
• b/a:2
• b%a:0
• a is not equal to b
LAB 4
Relational Operators:
• Bourne Shell supports following relational operators which are specific to numeric
values. These operators would not work for string values unless their value is numeric.
• For example, following operators would work to check a relation between 10 and 20 as
well as in between "10“ and "20" but not in between "ten" and "twenty".
• Assume variable a holds 10 and variable b holds 20 then:
• Operator Description Example
• -eq Checks if the value of two operands are equal or not, if yes then condition becomes
true.
[ $a –eq $b ] is not true.
• -ne Checks if the value of two operands are equal or not, if values are not equal then
condition becomes true.
[ $a –ne $b ] is true
LAB 4
-gt Checks if the value of left operand is greater than the value of right operand, if yes
then condition becomes true.
[ $a –gt $b ] is not true.
-lt Checks if the value of left operand is less than the value of right operand, if yes
then condition becomes true.
[ $a –lt $b ] is true.
-ge Checks if the value of left operand is greater than or equal to the value of right
operand, if yes then condition becomes true.
[ $a –ge $b ] is not true.
-le Checks if the value of left operand is less than or equal to the value of right
operand, if yes then condition becomes true.
[ $a –le $b ] is true.
It is very important to note here that all the conditional expressions would be put inside
square braces with one spaces around them, for example [ $a <= $b ] is correct where as
[$a <= $b] is incorrect.
LAB 4
if [ $a -ge $b ]
then
echo "$a -ge $b: a is greater or equal to b“
else
echo "$a -ge $b: a is not greater or equal to b"
fi
if [ $a -le $b ]
then
echo "$a -le $b: a is less or equal to b"
else
echo "$a -le $b: a is not less or equal to b"
fi
• This would produce following result:
10 -eq 20: a is not equal to b
10 -ne 20: a is not equal to b
10 -gt 20: a is not greater than b
10 -lt 20: a is less than b
10 -ge 20: a is not greater or equal to b
10 -le 20: a is less or equal to b
LAB 4
Boolean Operators
• There are following Boolean operators supported by Bourne Shell.
• Assume variable a holds 10 and variable b holds 20 then:
• Operator Description Example
•! This is logical negation. This inverts a true condition into false
and vice versa. [ ! false ] is true.
• -o This is logical OR. If one of the operands is true then condition
would be true.
[ $a –lt 20 -o $b -gt 100 ] is true.
• -a This is logical AND. If both the operands are true then condition
would be true otherwise it would be false.
[ $a –lt 20 -a $b -gt 100 ] is false.
LAB 4
• Example:
• Here is an example which uses all the boolean operators:
LAB 4
EXAMPLE
a=10
b=20
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
if [ $a -lt 100 -a $b -gt 15 ]
then
echo "$a -lt 100 -a $b -gt 15 : returns true"
Example Cont.
else
echo "$a -lt 100 -a $b -gt 15 : returns false"
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi
if [ $a -lt 5 -o $b -gt 100 ]
then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi
• This would produce following result:
10 != 20 : a is not equal to b
10 -lt 100 -a 20 -gt 15 : returns true
10 -lt 100 -o 20 -gt 100 : returns true
10 -lt 5 -o 20 -gt 100 : returns false
String Operators:
• There are following string operators supported by Bourne Shell.
• Assume variable a holds "abc" and variable b holds "efg" then:
• Operator Description Example
= Checks if the value of two operands are equal or not, if yes then
condition becomes true.
[ $a = $b ] is not true.
!= Checks if the value of two operands are equal or not, if values are
not equal then condition becomes true.
[ $a != $b ] is true.
-z Checks if the given string operand size is zero. If it is zero length
then it returns true.
[ -z $a ] is not true.
-n Checks if the given string operand size is non-zero. If it is non-zero
length then it returns true.
[ -z $a ] is not false.
Str Check if str is not the empty string. If it is empty then it returns
false. [ $a ] is not false.
• Example:
• Here is an example which uses all the string operators:
#!/bin/sh
a="abc"
b="efg"
if [ $a = $b ]
then
echo "$a = $b : a is equal to b"
Cont.
else
echo "$a = $b: a is not equal to b"
fi
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
Cont.
• if [ -z $a ]
• then
• echo "-z $a : string length is zero"
• else
• echo "-z $a : string length is not zero"
• fi
• if [ -n $a ]
• then
• echo "-n $a : string length is not zero"
Cont.
else
echo "-n $a : string length is zero"
fi
if [ $a ]
then
echo "$a : string is not empty"
else
echo "$a : string is empty"
fi
result