0% found this document useful (0 votes)
32 views48 pages

Shell

The document discusses different types of shells including Bourne, Korn, C, Bash and Z shells. It covers shell variables including user defined, system defined and environment variables. It also covers shell programming concepts like conditions, loops, functions and case statements.

Uploaded by

captprice586
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views48 pages

Shell

The document discusses different types of shells including Bourne, Korn, C, Bash and Z shells. It covers shell variables including user defined, system defined and environment variables. It also covers shell programming concepts like conditions, loops, functions and case statements.

Uploaded by

captprice586
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Shell

What are shells?

A shell is an interpreter where we can provide


commands and get a response. Shells can perform
powerful and uncountable number of actions based on
the commands,which we provided. Basically, a Shell is
a program which runs other programs.

# cp f1 f2
Type of shells
Type of shells
● Bourne shell $ sh
● Korn shell $ ksh
● C shell $ csh
● Bash shell $ bash
● Z shell $ zsh
Variables
Variables:
● it is data name
● It is a temporary storage location
● It’s value can be change at run time
● In shell there is no data types and each one is treated as
string
● Variables are two types:
Variables are two types:

1 user defined variables


2 system defined variables
1) user defined variables
1.1 local variables
1.2 constant variables
1.3 global variables
1.1 local variables
This variables are available only particular shell(maha),but not available globally.
Example:
#a=10
#b=2.0
#name=ram
#comp=”abc tech”
# echo $a
# c=$a
# k=ls
# echo $k : ls
# k=’ls’
1.2 constant variables:
These are all readonly variables

Example:

#x=100
#readonly x
#x=25 : it raises error
1.3 Global variable:
it is available for entire session. If you want, this variables as parminent then set
in .bash_profile or /etc/profile

# export x=100
# env
# : input from user

read
2) System defined & Environment variables:
#set : display all env variables and with values
#PATH
Standard variables
0 :standard input
1 :standard output
2 :standard error
Example:
# sh sample.sh 1>stdout.txt 2>stderr.txt
Operators:
Arithmetic : * / - + %
Relational numeric : -lt -le -gt -ge -eq -ne
Relational string comp : < > == !=
Logical : -a -o ! || &&
Assignment : =
`
Examples:
echo "enter a value :"
read a
echo "enter b value :"
read b
echo `expr $a + $b`
echo `expr $a \* $b`
echo `expr $a / $b`
echo `expr $a % $b`

``
Input arguments to the script
$0 :denotes name of the executing script
$n :denotes nth argument
$* :denotes all input arguments entire string a$s single arg
$$ :denotes PID of the executing script
$# :no.of args count
$@ :denotes all input arguments each word as single arg
Flow control
Conditions
string comparison: if true
-n string string is not an empty string, true
-z string string is an empty string, true
string1 == string2 strings are same true
string1 != string2 strings are not same true
conditions
Arithmetic comparison: if true
exp1 –eq exp2 expressions equal
exp1 –ne exp2 expressions not equal
exp1 –gt exp2 exp1 greater than exp2
exp1 –lt exp2 exp1 less than exp2
! expression expression is false
file conditionals:if
-f : true,if it is Regular file.
-d : true,if it is Directory file.
-l : true,if it is link file.
-b : true,if it is block specific file.
-c : true,if it is character specific file.
-e : true,if it is exists file.
-s : true,if it is file is not empty.
-r : true,if it is file has read permissions.
-w: true,if it is file has write permissions
-x : true,if it is file has execute permissions
If condition:

if [ condition ]; then
smt...
fi

If [ condition ]; then smt1 else smt2 fi

If [ condition1 ]; then smt1 elif [ condition 2 ]; then smt2 else smt3 fi

if [ condition1 ]; then if[ condition2 ]; then smt else smt fi else smt fi
Example:

if [ $# -gt 0 ]; then

echo "input argument has given"

else

echo "no input argument"

fi
For Loop:Iteration
for variable in val1 val2 val3 ...val nth
do
Smt1
Smt2
Smt3
Smt4 exit
Smt5
Smt6

done

● Break : terminate the loop


● Continue : it skips current iteration and takes next iteration
Example:

#!/bin/sh
for i in 1 2 3 4 5
do
echo "Looping ... number $i"
done
While loop:

while [ condition ]do


smt1 …
done
Example
#!/bin/sh

a=0

while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Case:

case variable in
pattern1) smt1
Smt2
;;
pattern2) smt1
Smt2
;;
esac
read input
case $input in
hello)
echo "Hello yourself!"
;;
bye)
echo "See you again!"
;;
*)
echo "Sorry, I don't understand"
;;
esac
Functions
● To reduce length of code
● Easy to develop
● Easy to maintain
● Support re-usability

function_name()
{
List of commands
}
#!/bin/sh
myfunc()
{
echo "I was called as : $@"
x=2
}
### Main script starts here
echo "Script was called with $@"
x=1
echo "x is $x"
myfunc 1 2 3
echo "x is $x"
#!/bin/sh
myadd()
{
a=$1
b=$2
c=`expr $a + $b`
echo “ total value is : “ $c
}
myadd 20 30
myadd 10 20
myadd 5 5
myadd 1 1
Exp 0:
Substitution: Sum of 456

n=456
echo “ n variable value is :” $n
a=`echo $n |cut -c 1`
b=`echo $n |cut -c 2`
c=`echo $n |cut -c 3`
d=`expr $a + $b + $c`
echo “sum of 456 is :” $d
1)program for addition and subtraction
echo "Enter a:"
read a
echo "Enter b:"
read b
sum=`expr $a + $b`
diff=`expr $a - $b`
prod=`expr $a \* $b`
quo=`expr $a / $b`
echo $sum
echo $diff
echo $prod
2 multiple conditions in for loop
for g in 1 2 3
do
for c in 123 456 789
do
if [[ ( "$g" -eq 1 && "$c" = "123" ) || ( "$g" -eq 2 && "$c" = "456" ) ]];
then
echo "g = $g; c = $c; true"
else
echo "g = $g; c = $c; false"
fi
done
3 program on while

a=0;
while [ $a -lt 5 ];
do
date
a=`expr $a + 1`
done
4 Program on for loop

i=1
for day in Mon Tue Wed Thu Fri
do
echo "Weekday $((i++)) : $day"
done
5 shell script with C snippet

#!/bin/bash
for (( c=1; c<=5; c++ ))
do
echo "Welcome $c times..."
done
6 shell script to execute to identify the count of word(user) in passwd
file
#!/bin/bash
for f in /etc/*
do
if [ "${f}" == "/etc/passwd" ]; then
c=$(grep -c User /etc/passwd)
echo "Total number of user word count ${c} defined in ${f}"
break
fi
done
7 Shell script to identify file or folder
#!/bin/bash
FILES="$@"
for f in $FILES
do

if [ -f $f ]; then
echo "It is a file"
continue # read next file
fi
done
9 shell script for function to add numbers

#!/bin/bash
#function to add two numbers
add()
{
x=$1
y=$2
echo -e "Number entered by u are: $x and $y"
echo "sum of $1 and $2 is `expr $x + $y` "
}
# main script
echo "enter first number"
read first
echo "enter second number"
read sec
#calling function
add $first $sec
echo "end of the script"
10 Case syntax in shell script
#!/bin/bash
for filename in $(ls)
do
# Take extension available in a filename
ext=${filename##*\.}
case "$ext" in
c) echo "$filename : C source file"
;;
o) echo "$filename : Object file"
;;
sh) echo "$filename : Shell script"
;;
txt) echo "$filename : Text file"
;;
*) echo " $filename : Not processed"
;;
esac
done
11 Case with user inputs

#!/bin/bash
echo -n "Do you agree with this? [yes or no]: "
read yno
case $yno in
[yY] | [yY][Ee][Ss] )
echo "Agreed"
;;

[nN] | [n|N][O|o] )
echo "Not agreed, you can't proceed the installation";
exit 1
;;
*) echo "Invalid input"
;;
esac
12 Case with starting the application

#!/bin/bash

case "$1" in
'start')
echo "Starting application"
/usr/bin/startpc
;;
'stop')
echo "Stopping application"
/usr/bin/stoppc
;;
'restart')
echo "Usage: $0 [start|stop]"
;;
esac

You might also like