Shell_Scripting_Material
Shell_Scripting_Material
Special Files:
/dev/null
/dev/tty
[root@server shellscript]# echo "hello" >/dev/tty
hello
Variables:
1. Predifined
2. User defined
- Null Variables
[root@server shellscript]# str=""
[root@server shellscript]# echo $str
- unsetting a varaible
[root@server shellscript]# var=10
[root@server shellscript]# echo $var
10
[root@server shellscript]# unset var
[root@server shellscript]# echo $var
- Exporting Variables
var=10
export var
source ~/.bash_profile
Reading A variable:
[root@server shellscript]# cat var.sh
echo "please give a and b values"
read a b
echo "your input data is",$a,$b
- less arguments
[root@server shellscript]# bash var.sh
please give a and b values
10
your input data is,10,
- exit status ?
Success Always returns "0"
[root@server shellscript]# ls -l
total 12
-rw-r--r--. 1 root root 44 Jun 11 07:24 file
-rw-r--r--. 1 root root 0 Jun 11 07:23 file1
-rw-r--r--. 1 root root 0 Jun 11 07:23 file2
-rw-r--r--. 1 root root 0 Jun 11 07:24 file22
-rw-r--r--. 1 root root 0 Jun 11 07:23 file3
drwxr-xr-x. 3 root root 4096 Jun 9 21:52 practise
-rw-r--r--. 1 root root 75 Jun 11 07:29 var.sh
[root@server shellscript]# echo $?
0
One way :
[root@server shellscript]# bash var.sh
please give a and b values
10 20
your input data is,10,20
2nd Way:
[root@server shellscript]# ls -l var.sh
-rw-r--r--. 1 root root 75 Jun 11 07:29 var.sh
[root@server shellscript]# chmod +x var.sh
[root@server shellscript]# ls -l var.sh
-rwxr-xr-x. 1 root root 75 Jun 11 07:29 var.sh
Expressions:
Mathametical
[root@server practise]# cat mat.sh
#!/bin/bash
echo "please give a and b values"
read a
read b
echo "add of a and b is",$((a+b))
echo "sub of a and b is",$((a-b))
echo "div of a and b is",$((a/b))
echo "mul of a and b is",$(($a*$b))
echo "mod of a and b is",$((a%b))
Output:
[root@server practise]# ./mat.sh
please give a and b values
20
30
add of a and b is,50
sub of a and b is,-10
div of a and b is,0
mul of a and b is,600
mod of a and b is,20
if [[ $a -lt $b ]]
then
echo "b is big"
fi
if [[ $a -ne $b ]]
then
echo "a is not equal to b"
fi
if [[ $a -eq $b ]]
then
echo "a and b are equal"
fi
Output:
if (( a > b))
then
echo "a is big"
fi
if (( a <b ))
then
echo "b is big"
fi
if (( a == b ))
then
echo "a is equal to b"
fi
if ((a != b ))
then
echo "a and b are not equal"
fi
File expressions:
-x:
#!/bin/bash
if [[ -x file1 ]]
then
echo "has execute permission"
else
echo "has no execute permission"
fi
-d:
-f:
Logical Operations:
[root@server practise]# cat elif.sh
#!/bin/bash
echo "please give sum "
read sum
Decision Making :
if:
if (( a > b))
then
echo "a is big"
fi
if-then-else:
if ((a != b ))
then
echo "a and b are not equal"
else
echo "a and b are equal"
fi
elif-ladder:
if (( sum<=100 && sum >=80 ))
then
echo "grade A"
elif (( sum <=79 && sum >= 60))
then
echo "grade B"
elif (( sum <59 && sum>=40))
then
echo "grade C"
elif (( sum <39 && sum >=35))
then
echo "just passed"
else
echo "failed"
fi
Program:
[root@server practise]# cat parm.sh
#!/bin/bash
echo "your total input parameters are ",$#
echo "your input parameters are $1,$2,$3"
echo "your input paramaters are "
echo "Your script name is ",$0
echo "Using \$*"
for i in $*
do
echo $i
done
echo "Using \$@"
for i in $@
do
echo $i
done
echo "Using \"\$*\""
for i in "$*"
do
echo $i
done
echo "Using \"\$@\""
for i in "$@"
do
echo $i
done
Output:
Argument Validation:
[root@server practise]# cat case.sh
#!/bin/bash
if [[ $# != 3 ]]
then
echo "Usage: Please give a and b values & operation\(+,-,/,*,%\)"
fi
while loop:
Output:
[root@server practise]# ./while.sh
1
2
3
4
case:
[root@server practise]# cat case.sh
#!/bin/bash
if [[ $# != 3 ]]
then
echo "Usage: Please give a and b values & operation\(+,-,/,*,%\)"
fi
case $3 in
"+") echo "add of a and b is",$(($1+$2));;
esac
Output:
[root@server practise]# ./case.sh 10 20 +
add of a and b is,30
until loop:
[root@server practise]# cat until.sh
#!/bin/bash
count=10
until ((count <1))
do
echo $count
((count=count-1))
done
Output:
[root@server practise]# ./until.sh
10
9
8
7
6
5
4
3
2
1
for loop:
[root@server practise]# cat for.sh
#!/bin/bash
for i in {1..9}
do
echo $i
done
Output:
[root@server practise]# ./for.sh
1
2
3
4
5
6
7
8
9
select loop:
[root@server practise]# cat select.sh
#!/bin/bash
select i in + - / % \*
do
echo "please give a and b values"
read a
read b
case $i in
"+") echo "add of a and b is ",$((a+b));;
"/") echo "div of a and b is",$((a/b));;
"%") echo "mod of a and b is",$((a%b));;
"*") echo "mul of a and b is", $((a*b));;
"-") echo "sub of a and b is",$((a-b));;
*) echo "Usage: Please give proper arthimetic operator";;
esac
done
Output:
[root@server practise]# ./select.sh
1) +
2) -
3) /
4) %
5) *
#? 1
please give a and b values
10
20
add of a and b is ,30
#? 2
please give a and b values
10
20
sub of a and b is,-10
Loop Direction:
Input Redirection:
while
[root@server shellscript]# cat file
hello this is shahan
this is my first class
[root@server shellscript]# while read data
> do
> echo $data
> done<file
hello this is shahan
this is my first class
output Redirection:
while:
[root@server shellscript]# cat opred.sh
#!/bin/bash
count=1
while ((count <5))
do
echo $count
((count=count+1))
done>/tmp/test
Output:
[root@server shellscript]# ./opred.sh
[root@server shellscript]# cat /tmp/test
1
2
3
4
for:
for i in {1..5}; do echo $i; done>/tmp/test
[root@server shellscript]# cat /tmp/test
1
2
3
4
5
Input Piping:
while:
[root@server shellscript]# cat file
hello this is shahan
this is my first class
[root@server shellscript]# cat file|while read data
> do
> echo $data
> done
hello this is shahan
this is my first class
output piping:
while:
[root@server shellscript]# cat file
hello this is shahan
this is my first class
[root@server shellscript]# cat file|while read data; do echo $data; done|wc -l
2
continue:
sleep:
[root@server shellscript]# for i in {1..9}
> do
> echo $i
> sleep 2
> done
1
2
3
4
5
6
7
8
9
Debuggin scripts:
One Method:
ksh -o xtrace *.scr
2nd Way:
simple add "set -x " after "#!/bin/bash" and execute the script
function add()
{
return $((a+b))
}
function sub()
{
sum=$((a-b))
}
function div()
{
return $((a/b))
}
function mul()
{
return $((a*b))
}
function mod()
{
return $((a%b))
}
case $opr in
"+") add
echo "add of a and b is",$?;;
"-") sub
echo "sub of a and b is",$sum;;
"/") div
echo "div of a and b is",$?;;
"*") mul
echo "mul of a and b is",$?;;
"%") mod
echo "mod of a and b is",$?;;
*) echo "Usage: Please choose proper operator";;
esac
Output: