Lab 3
Lab 3
Technology
Rahim Yar Khan
Department Of Computer Engineering
What is Shell?
Syntax of Shell Scripting.
What is a Shell?
For example, in an if condition, the spaces around the brackets are crucial:
“if [ condition ]; then ... fi” is correct, whereas “if[condition];then ... fi” will
produce an error.
Unlike Java, where the structure is more rigid, shell scripting demands careful
attention to spacing and formatting to ensure that commands and variables are
interpreted correctly.
Important Points while writing scripts:
Following are some important points while writing a script:
3. Parametric variables
2. Don’t put spaces on either side of the equal sign when assigning value
to variable.
or
var=""
Lab activities:
Activity 1.1:
Write a shell script that takes two numbers as input from
the user and prints the sum of those numbers.
(hint: use “read” to take user input).
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
----------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Activity 1.2:
Write a shell script that multiplies two numbers
provided by the user and displays the result.
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
-------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Syntax
if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi //ending of if.
Example
The above example can also be written using the if...else statement as
follows:
a=10
b=20
if [ $a -eq $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
If-elif… fi Statements:
Syntax
if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ] // (elif means else-if as same in Java)
then
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi //ending of if
Relational Operators
Bourne Shell supports the following relational operators that are specific
to numeric values. These operators do not work for string values unless
their value is numeric.
Activity 1.3:
Create a shell script that takes three numbers as input
and displays the largest number.
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Activity 1.4:
Write a shell script that takes input from user and check if a
number is even or odd.
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Activity 1.5:
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
-------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------