0% found this document useful (0 votes)
52 views25 pages

Shell Scripting: Presented by - Mukesh Halwan Premkanth Mengani

The document discusses shell scripting in Linux. It covers various shells like Bourne shell, C shell, Korn shell, and Bash shell. It then discusses how to create basic shell scripts using Bash, including writing "Hello World" scripts. It also covers variables, single and double quotes, export command, read command, arithmetic evaluation, conditional statements, expressions, case statement, iteration statements like for loop and while loop, and functions in shell scripts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views25 pages

Shell Scripting: Presented by - Mukesh Halwan Premkanth Mengani

The document discusses shell scripting in Linux. It covers various shells like Bourne shell, C shell, Korn shell, and Bash shell. It then discusses how to create basic shell scripts using Bash, including writing "Hello World" scripts. It also covers variables, single and double quotes, export command, read command, arithmetic evaluation, conditional statements, expressions, case statement, iteration statements like for loop and while loop, and functions in shell scripts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

SHELL SCRIPTING

PRESENTED BY –
MUKESH HALWAN
PREMKANTH MENGANI
SHELL SCRIPTING
 Linux has a variety of different shells:
 Bourne shell (sh), C shell (csh), Korn
shell (ksh), TC shell (tcsh), Bourne
Again shell (bash).
 Certainly the most popular shell is
“bash”. Bash is an sh-compatible shell
that incorporates useful features from
the Korn shell (ksh) and C shell (csh).
 It offers functional improvements over
sh for both programming and
 To make a file
 Syntax -> vi filename.sh

 Change permission
 Syntax -> chmod u+x filename.sh

 Run the file


 Syntax -> ./filename.sh
THE FIRST BASH PROGRAM
 There are two major text editors in Linux:
 vi and emacs(or xemacs).
 So fire up a text editor; for example: vi
hello.sh
 and type the following inside it:
#!/bin/bash
echo “Hello World”
 The first line tells Linux to use the bash
interpreter to run this script. We call it
hello.sh. Then, make the script executable:
 $ chmod 700 hello.sh
 $ ./hello.sh
 Output : Hello World
VARIABLES
 We can use variables as in any programming
languages. Their values are always stored as
strings, but there are mathematical operators in the
shell language that will convert variables to
numbers for calculations.
 We have no need to declare a variable, just
assigning a value to its reference will create it.
 Example
#!/bin/bash
STR=“Hello World!”
echo $STR
 Line 2 creates a variable called STR and assigns
the string "Hello World!" to it. Then the value of this
variable is retrieved by putting the '$' in at the
SINGLE AND DOUBLE QUOTE

 When assigning character data containing spaces or special characters,


the data must be enclosed in either single or double quotes.

 Using double quotes to show a string of characters will allow any


variables in the quotes to be resolved

$ var=“test string”
$ newvar=“Value of var is $var”
$ echo $newvar
Value of var is test string

 Using single quotes to show a string of characters will not allow variable
resolution

$ var=’test string’
$ newvar=’Value of var is $var’
$ echo $newvar
Value of var is $var
THE EXPORT COMMAND
 The export command puts a variable into the environment so it will be accessible to
child processes. For instance:

$ x=hello
$ bash # Run a child shell.
$ echo $x # Nothing in x.
$ exit # Return to parent.
$ export x
$ bash
$ echo $x
hello # It's there.

 If the child modifies x, it will not modify the parent’s original value. Verify this by
changing x in the following way:
$ x=ciao
$ exit
$ echo $x
hello
READ COMMAND
 The read command allows you to prompt for input and store it
in a variable.

 Example:

#!/bin/bash
echo -n “Enter name of file to delete: ”
read file
echo “Type 'y' to remove it, 'n' to change your mind ... ”
rm -i $file
echo "That was YOUR decision!”

 Line 2 prompts for a string that is read in line 3. Line 4 uses


the interactive remove (rm -i) to ask the user for confirmation.
ARITHMETIC EVALUATION
 The let statement can be used to do mathematical functions:

$ let X=10+2*7
$ echo $X
24
$ let Y=X+2*4
$ echo $Y
32

 An arithmetic expression can be evaluated by $[expression] or


$((expression))

$ echo “$((123+20))”
143
$ VALORE=$[123+20]
$ echo “$[123*$VALORE]”
17589
ARITHMETIC EVALUATION
 Available operators: +, -, /, *, %

 Example

$ cat arithmetic.sh
#!/bin/bash
echo -n “Enter the first number: ”; read x
echo -n “Enter the second number: ”; read y
add=$(($x + $y))
sub=$(($x - $y))
mul=$(($x * $y))
div=$(($x / $y))
mod=$(($x % $y))
# print out the answers:
echo “Sum: $add”
echo “Difference: $sub”
echo “Product: $mul”
echo “Quotient: $div”
echo “Remainder: $mod”
CONDITIONAL STATEMENTS
 Conditionals let us decide whether to perform an action or not, this
decision is taken by evaluating an expression. The most basic form is:

if [ expression ];
then
statements
elif [ expression ];
then
statements
else
statements
fi

 the elif (else if) and else sections are optional

 Put spaces after [ and before ], and around the operators and operands.
EXPRESSIONS
 An expression can be: String comparison, Numeric comparison, File
operators and Logical operators and it is represented by [expression]:

 String Comparisons:

= compare if two strings are equal


!= compare if two strings are not equal
-n evaluate if string length is greater than zero
-z evaluate if string length is equal to zero

 Examples:

[ s1 = s2 ] (true if s1 same as s2, else false)


[ s1 != s2 ] (true if s1 not same as s2, else false)
[ s1 ] (true if s1 is not empty, else false)
[ -n s1 ] (true if s1 has a length greater then 0, else false)
[ -z s2 ] (true if s2 has a length of 0, otherwise false)
EXPRESSIONS
 Number Comparisons:

-eq compare if two numbers are equal


-ge compare if one number is greater than or equal to a number
-le compare if one number is less than or equal to a number
-ne compare if two numbers are not equal
-gt compare if one number is greater than another number
-lt compare if one number is less than another number

 Examples:

[ n1 -eq n2 ] (true if n1 same as n2, else false)


[ n1 -ge n2 ] (true if n1greater then or equal to n2, else false)
[ n1 -le n2 ] (true if n1 less then or equal to n2, else false)
[ n1 -ne n2 ] (true if n1 is not same as n2, else false)
[ n1 -gt n2 ] (true if n1 greater then n2, else false)
[ n1 -lt n2 ] (true if n1 less then n2, else false)
EXAMPLES
$ cat user.sh
#!/bin/bash
echo “Enter your login name: "
read name
if [ $name = $USER ];
then
echo “Hello, $name. How are you today ?”
else
echo “You are not $USER, so who are you ?”
fi
EXPRESSIONS
 Files operators:

-d check if path given is a directory


-f check if path given is a file
-e check if file name exists
-r check if read permission is set for file or directory
-s check if a file has a length greater than 0
-w check if write permission is set for a file or directory
-x check if execute permission is set for a file or directory

 Examples:

[ -d fname ] (true if fname is a directory, otherwise false)


[ -f fname ] (true if fname is a file, otherwise false)
[ -e fname ] (true if fname exists, otherwise false)
[ -s fname ] (true if fname length is greater then 0, else false)
[ -r fname ] (true if fname has the read permission, else false)
[ -w fname ] (true if fname has the write permission, else false)
[ -x fname ] (true if fname has the execute permission, else false)
EXAMPLE
#!/bin/bash
if [ -f /etc/fstab ];
then
cp /etc/fstab .
echo “Done.”
else
echo “This file does not exist.”
exit 1
fi
EXPRESSIONS
 Logical operators:

! negate (NOT) a logical expression


-a logically AND two logical expressions
-o logically OR two logical expressions

Example:

#!/bin/bash
echo “Enter a number 1 < x < 10:”
read num
if [ $num -gt 1 –a $num -lt 10 ];
then
echo “$num*$num=$(($num*$num))”
else
echo “Wrong insertion !”
fi
EXPRESSIONS
 Logical operators:

&& logically AND two logical expressions


|| logically OR two logical expressions

Example:

#!/bin/bash
echo "Enter a number 1 < x < 10: "
read num
if [ $number -gt 1 ] && [ $number -lt 10 ];
then
echo “$num*$num=$(($num*$num))”
else
echo “Wrong insertion !”
fi
CASE STATEMENT
 Used to execute statements based on specific values. Often used
in place of an if statement if there are a large number of
conditions.

 Value used can be an expression


 each set of statements must be ended by a pair of semicolons;
 a *) is used to accept any value not matched with list of values

case $var in
val1)
statements;;
val2)
statements;;
*)
statements;;
esac
EXAMPLE CASE.SH

 #!/bin/bash
 echo “Enter a number 1 < x < 10: ”
 read x
 case $x in
 1) echo “Value of x is 1.”;;
 2) echo “Value of x is 2.”;;
 3) echo “Value of x is 3.”;;
 4) echo “Value of x is 4.”;;
 5) echo “Value of x is 5.”;;
 6) echo “Value of x is 6.”;;
 7) echo “Value of x is 7.”;;
 8) echo “Value of x is 8.”;;
 9) echo “Value of x is 9.”;;
 0 | 10) echo “wrong number.”;;
 *) echo “Unrecognized value.”;;
 esac
ITERATION STATEMENTS & FOR LOOP
 The for structure is used when you are looping through a range of variables.

for var in list


do
statements
done

 statements are executed with var set to each value in the list.

 Example

#!/bin/bash
let sum=0
for num in 1 2 3 4 5
do
let “sum = $sum + $num”
done
echo $sum
WHILE LOOP
 syntax for bash shell

while [ condition ]
do
command1
command2
commandN
done
BASH WHILE LOOP EXAMPLE

 #!/bin/bash
 c=1

 while [ $c -le 5 ]

 do

 echo "Welcone $c times"


 (( c++ ))
 done
FUNCTIONS
 Functions make scripts easier to maintain. Basically it breaks up
the program into smaller pieces. A function performs an action
defined by you, and it can return a value if you wish.

#!/bin/bash
hello()
{
echo “You are in function hello()”
}

echo “Calling function hello()…”


hello
echo “You are now out of function hello()”

 In the above, we called the hello() function by name by using the


line: hello . When this line is executed, bash searches the script
for the line hello(). It finds it right at the top, and executes its
contents.
FUNCTIONS
$ cat function.sh
#!/bin/bash
function check() {
if [ -e "/home/$1" ]
then
return 0
else
return 1
fi
}
echo “Enter the name of the file: ” ; read x
if check $x
then
echo “$x exists !”
else
echo “$x does not exists !”
fi.

You might also like