Shell Scripting Notes
Shell Scripting Notes
Shell Scripting Notes
At this point you are probably wondering how you can set
your own priority levels on processes. To change the priority
when issuing a new command you do nice -n [nice value]
[command]:
nice -n 10 apt-get upgrade
This will increment the default nice value by a positive 10
for the command, apt-get upgrade This is often useful for
times when you want to upgrade apps but dont want the
extra process burden at the given time. Remember a
positive number is gives less priority for a process.
Shell scripting:
The shell provides you with an interface to the UNIX
system.
It gathers input from you and executes programs
based on that input.
A shell is an environment in which we can run our
commands, programs, and shell scripts. There are
different flavors of shells.
Shell prompt:
The prompt, $, which is called command prompt, is
issued by the shell. While the prompt is displayed,
you can type a command.
For eg: $ date
pattern1)
Statement(s) to be executed if pattern1 matches ;;
pattern2)
Statement(s) to be executed if pattern2 matches;;
pattern3)
Statement(s) to be executed if pattern3 matches;;
Esac
Example 1:
echo Enter any value between 1 to 5
read num
case $num in
1)echo ONE ;;
2)echo TWO;;
3)echo THREE;;
4)echo FOUR;;
5)echo FIVE;;
*)echo INVALID VALUE;;
esac
Note: * indicates default value.
Ifelse:
Comparisons:
-eq equal to
-ne not equal to
-lt less than
-le less than or equal to
-gt greater than
-ge greater than or equal to
If-else program:
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
Example 2:
valid_password=welcome
Example 3:
Echo Enter the number
Read num
If test $num le 30
Then
echo Number is less than equal to 30
else
echo Number is greater than 30
fi
if then elif:
Syntax:
if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
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
Example 4:
echo enter the value of a
read a
echo enter the value of b
read b
if [ $a = $b ]
then
echo a is equal to b
elif [ $a gt $b ]
then
echo a is greater than b
elif [ $a lt $b ]
then
echo a is less than b
else
echo INVALID VALUE
fi
While loop:
Syntax:
while command
do
Statement(s)
done
Example 5:
For loop:
Syntax:
for (( EXP1; EXP2; EXP3 ))
do
command1
command2
command3
done
Example 6:
for((c=1;c<=5;c++))
do
echo Welcome to STC
done
Until loop:
Syntax:
until command
do
Statement(s)
Done
Example 7:
a=1
until [ $a ge 5 ]
do
echo $a .Welcome to STC
a=$(($a + 1))
done
PASSING PARAMETERS:
Example 8:
(inside a shell program)
Echo First value is $1
Syntax:
Break
Example 9:
a=0
while [ $a lt 10 ]
do
echo $a
if [ $a eq 5 ]
then
break
fi
a=`expr $a + 1`
done
Syntax:
Continue
Example 10:
NUMS="1 2 3 4 5 6 7"
for NUM in $NUMS
do
Q=`expr $NUM % 2`
if [ $Q -eq 0 ]
then
echo "Number is an even number!!"
continue
fi
echo "Found odd number"
done
LOGICAL OPERATORS:
&& -------- >
-------- >
Boolean Operators:
There are following boolean operators supported by Bourne
Shell.
Assume variable a holds 10 and variable b holds 20 then:
Operator
!
-o
-a
Description
This is logical negation.
This inverts a true
condition into false and
vice versa.
This is logical OR. If one of
the operands is true then
condition would be true.
This is logical AND. If both
Example
[ ! false ] is true.
[ $a -lt 20 -o $b -gt
100 ] is true.
[ $a -lt 20 -a $b -gt
N : True if the file exists and has been modified since it was
last read.
S : True if the file exists and is a socket.
Example 13:
File=directory1
If [ -d $file ]
Then
Echo Directory is present
Else
Echo Directory is not present
fi
How to execute:
Bash x filename.sh
Bash v filename.sh