Linux - Shell - Script
Linux - Shell - Script
Shell script can take input from user, file and output
them on screen.
Useful to create own commands.
Save lots of time.
To automate some task of day today life.
System administration part can be also automated.
How to write and execute ?
#!/bin/bash
echo "Hello User"
echo "See the files in current directory"
ls
Variables
In Linux (Shell), there are two types of variable:
System variables ‐ created and maintained by Linux
itself.
echo $USER
echo $PATH
User defined variables ‐ created and maintained by
user.
All variables are considered and stored as strings, even
when they are assigned numeric values.
Variables are case sensitive.
Variables
myvar=”Hello”
echo $myvar [ Hello ]
echo “$myvar” [ Hello ]
echo ‘$myvar’ [ $myvar ]
echo \$myvar [ $myvar ]
Read
#!/bin/bash
echo ‐n "Enter your name:”
read name
echo ‐n "Enter your student no:”
read stdno
echo "Your Name:$name”
echo "Your Age:$stdno”
Shell Arithmetic
#!/bin/bash
expr 1 + 1
expr 1 ‐ 1
expr 1 \* 1
expr 1 / 1
va r=`expr 1 + 1`
x=1
x=`expr $x + 1`
Shell Arithmetic
If‐Else
if [ conditiong1 ]; then
statement1
elif [ condition2 ]; then
statement2
else
statement3
fi
It is must to put spaces between the [ braces and the
condition being checked.
If you prefer putting then on the same line as if, you
must add a semicolon to separate the test from the
then.
If‐Else
If‐Else
If‐Else
#!/bin/bash
echo "Enter first number "
read num1
echo "Enter second number"
read num2
if [ $num1 ‐gt $num2 ] ; then
echo "$num1 is greater than $num2"
elif [ $num1 ‐lt $num2 ] ; then
echo "$num1 is less than $num2"
else
echo "$num1 and $num2 are equal"
fi
Case
case $var in
condition1) statement ;;
condition2) statement ;;
*) statement3
esac
#!/bin/sh
echo “Is it morning? Please answer yes or no”
read timeofday
case “$timeofday” in
yes) echo “Good Morning”;;
no ) echo “Good Afternoon”;;
y ) echo “Good Morning”;;
n ) echo “Good Afternoon”;;
* ) echo “Sorry, answer not recognized”;;
esac
Case
#!/bin/sh
echo “Is it morning? Please answer yes or no”
read timeofday
case “$timeofday” in
yes | y | Yes | YES ) echo “Good Morning”;;
n* | N* ) echo “Good Afternoon”;;
*) echo “Sorry, answer not recognized”;;
esac
Command Line arguments
[1] [2]
#!/bin/bash #!/bin/bash
echo "the number of args is for i in `ls`
$#" do
a=1 echo $i
for i in $* done
do
echo "The $a No arg is $i" [3]
a=`expr $a + 1` for(( i=0;i<=50;i++))
done do
echo $i
done
While
#!/bin/sh
foo() {
echo “Function foo is executing”
}
echo “script starting”
foo
echo “script ending”
output
script starting
Function foo is executing
script ending
Functions
#!/bin/bash
When a function is invoked, showarg()
the parameters to the {
script [$*, $#, $1, $2] and a=1
so on are replaced by the for i in $*
parameters to the function. do
When the function finishes, echo "The $a No arg is $i"
they are restored to their a=`expr $a + 1`
previous values. done
}
echo "Listing start"
showarg $*
echo "Total:$#"
echo "Listing End"
Functions
Functions can return numeric values using the return
command.
Functions can also return strings by the following ways.
[1]
f(){ var="123“; }
f
echo $var
[2]
f(){ echo "123"; }
result="$(f)"
Functions
Be careful :
Function calling can be recursive.
f()
{
statements
f
}
f
The parameter must be passed every time a function
is invoked either from main or from any other
functions.
Thanks