Experiment No. 09: Echo "Enter Some Text " Read Text Echo "You Entered: $text"
Experiment No. 09: Echo "Enter Some Text " Read Text Echo "You Entered: $text"
Experiment No. 09: Echo "Enter Some Text " Read Text Echo "You Entered: $text"
09
THEORY:
A shell program (shell script) is a text file that contains standard UNIX and shell
commands. Each line in a shell program contains a single UNIX command exactly as if you
had typed them in yourself. The difference is that you can execute all the commands in a
shell program simply by running the shell program rather than typing all the commands
within.
READ STATEMENT:
To get input from the keyboard, the read command is used. The read command takes input
from the keyboard and assigns it to a variable.
EXAMPLE:
echo "Enter some text > "
read text
echo "You entered: $text"
OUTPUT:
Enter some text > operating system
You entered: operating system
POSITIONAL PARAMETERS:
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.
Page 1 of 11
Variable Meaning
$0 the name of the shell program
$1 to $9 the first to ninth parameter
$# the number of parameters
$* all the parameters passed represented as a single word with
individual parameters separated
$@ all the parameters passed with each parameter as a separate word
$? hold the exit status of the previous command
EXAMPLE:
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"
OUTPUT:
$sh file1.sh abc xyz
First Parameter : abc
Second Parameter : xyz
Quoted Values: abc xyz
Quoted Values: abc xyz
Total Number of Parameters : 2
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.
OPERATORS:
Arithmetic Operators.
Relational Operators.
Boolean Operators.
String Operators.
File Test Operators.
ARITHMETIC OPERATORS
Assume variable a holds 10 and variable b holds 20 then,
Page 2 of 11
Operator Description Example
EXAMPLE:
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"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
Page 3 of 11
echo "b % a : $val"
OUTPUT:
a + b : 30
a - b : -10
a * b : 200
b/a:2
b%a:0
RELATIONAL OPERATORS:
Assume variable a holds 10 and variable b holds 20 then,
-eq Checks if the value of two operands are equal [ $a -eq $b ] is not true.
or not, if yes then condition becomes true.
-ne Checks if the value of two operands are equal [ $a -ne $b ] is true.
or not, if values are not equal then condition
becomes true.
-gt Checks if the value of left operand is greater [ $a -gt $b ] is not true.
than the value of right operand, if yes then
condition becomes true.
-lt Checks if the value of left operand is less than [ $a -lt $b ] is true.
the value of right operand, if yes then
condition becomes true.
-ge Checks if the value of left operand is greater [ $a -ge $b ] is not true.
than or equal to the value of right operand, if
yes then condition becomes true.
-le Checks if the value of left operand is less than [ $a -le $b ] is true.
or equal to the value of right operand, if yes
then condition becomes true.
EXAMPLE:
a=10
b=20
if [ $a -eq $b ]
then
Page 4 of 11
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
fi
if [ $a -ne $b ]
then
echo "$a -ne $b: a is not equal to b"
else
echo "$a -ne $b : a is equal to b"
fi
if [ $a -gt $b ]
then
echo "$a -gt $b: a is greater than b"
else
echo "$a -gt $b: a is not greater than b"
fi
if [ $a -lt $b ]
then
echo "$a -lt $b: a is less than b"
else
echo "$a -lt $b: a is not less than b"
fi
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
OUTPUT:
10 -eq 20: a is not equal to b
10 -ne 20: a is not equal to b
Page 5 of 11
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
BOOLEAN OPERATORS:
Assume variable a holds 10 and variable b holds 20 then,
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
OUTPUT:
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:
Assume variable a holds "abc" and variable b holds "efg" then,
EXAMPLE:
a="abc"
b="efg"
Page 7 of 11
if [ $a = $b ]
then
echo "$a = $b : a is equal to b"
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
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"
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
OUTPUT:
abc = efg: a is not equal to b
abc != efg : a is not equal to b
-z abc : string length is not zero
-n abc : string length is not zero
abc : string is not empty
Page 8 of 11
FILE TEST OPERATORS:
There are following operators to test various properties associated with a file. Assume a
variable file holds an existing file name "test" whose size is 100 bytes and has read, write
and execute permission on,
Operator Description Example
-s file Check if file has size greater than 0 if yes then [ -s $file ] is true.
condition becomes true.
EXAMPLE:
if [ -r $file ]
then
echo "File has read access"
else
echo "File does not have read access"
fi
if [ -w $file ]
then
echo "File has write permission"
else
echo "File does not have write permission"
fi
if [ -x $file ]
Page 9 of 11
then
echo "File has execute permission"
else
echo "File does not have execute permission"
fi
if [ -f $file ]
then
echo "File is an ordinary file"
else
echo "This is sepcial file"
fi
if [ -d $file ]
then
echo "File is a directory"
else
echo "This is not a directory"
fi
if [ -s $file ]
then
echo "File size is zero"
else
echo "File size is not zero"
fi
if [ -e $file ]
then
echo "File exists"
else
echo "File does not exist"
fi
OUTPUT:
File has read access
File has write permission
File has execute permission
File is an ordinary file
This is not a directory
File size is zero
File exists
Page 10 of 11
EXPR:
The shell does not have any computing features at all, it has to rely on the EXPR command
for that purpose. It can perform arithmetic operations on integers and also manipulates
strings to a limited extent.
$ z=`expr $z + 1`
$ echo $z
6
Page 11 of 11