Bash Shell Programming
-
Upload
abhijeet-panwar -
Category
Documents
-
view
242 -
download
0
Transcript of Bash Shell Programming
-
7/28/2019 Bash Shell Programming
1/21
Basics of Shell Programming
-
7/28/2019 Bash Shell Programming
2/21
Whats Shell?Its acts an interface between
the user and OS (kernel).Its
known as commandinterpreter.
When you type ls :
shell finds cmd (/usr/bin).shell runs cmd.
you receive the output.
-
7/28/2019 Bash Shell Programming
3/21
Whats Shell Program?
Its collections of executables orcommands placed in a file and
executed.
It provides user an option toexecute a command based on some
condition.
It provides conditional and controlstatements.(if,for,while,switch-case
etc )
-
7/28/2019 Bash Shell Programming
4/21
Types of Shell
Bourne shell (sh)
C shell (csh)
TC shell (tcsh)
Korn shell (ksh)
Bourne Again SHell (bash)
-
7/28/2019 Bash Shell Programming
5/21
Simple Shell Programs
-
7/28/2019 Bash Shell Programming
6/21
#print date and time - today.sh
echo "Today is:"
date
Save it as today.sh
Run:
sh today.sh
-
7/28/2019 Bash Shell Programming
7/21
#sample.sh
#!/bin/sh# assign a value:
a="hello world"
# now print the content of
"a":
echo "A is:"
echo $a
-
7/28/2019 Bash Shell Programming
8/21
#ex-2 grep word in a file - grep_ex.sh
echo "Enter the search string:"
read string
echo "Enter the file name:"read filename
grep -n "$string" $filename
-
7/28/2019 Bash Shell Programming
9/21
If statement
Type 1:
if condition is truethen
do this
if
Type 2:
if condition is true
then
do thiselse
do that
if
-
7/28/2019 Bash Shell Programming
10/21
#if stmt - if_ex.sh
echo "Enter the search string:"
read string
echo "Enter the file name:"
read filename
grep -n "$string" $filename
# $? gives exit status of last command
if [ $? -eq 0 ]
then
echo " Yes,the word present in the file"
else
echo "No,word not present"
fi
-
7/28/2019 Bash Shell Programming
11/21
o/p :
[oss@pc021698 ~]$ sh if_ex.sh
Enter the search string:
date
Enter the file name:
today.sh
2:date
Yes,the word present in the file
o/p:
[oss@pc021698 ~]$ sh if_ex.sh
Enter the search string:
year
Enter the file name:
today.sh
No,word not present
-
7/28/2019 Bash Shell Programming
12/21
Passing values from command line:
$0 - Program name
$1 - First argument
$2 - Second arg.
$# - No.of arg
$? - Exit status
-
7/28/2019 Bash Shell Programming
13/21
#print date and time and cmd arg - today_1.sh
echo "Today is:"
dateecho No .of arg:$#
echo Program name:$0
o/p:[oss@pc021698 ~]$ sh today_1.sh
Today is:
Fri Mar 7 00:28:27 IST 2008
No .of arg:0Program name:today_1.sh
-
7/28/2019 Bash Shell Programming
14/21
#grep using cmd line -grep_ex2.sh
echo "Entered search string is:"$1
echo "Entered file name :"$2
grep -n "$1" $2
# $? gives exit status of last command
if [ $? -eq 0 ]
then
echo " Yes,the word present in the file"
else
echo "No,word not present"
fi
-
7/28/2019 Bash Shell Programming
15/21
o/p:
[oss@pc021698 ~]$ sh grep_ex2.sh date today.sh
Entered search string is:date
Entered file name :today.sh
2:date
Yes,the word present in the file
-
7/28/2019 Bash Shell Programming
16/21
#simple for loop
for i in 1 2 3
doecho "==>$i"
done
#for loop - for_ex.sh
for file in *.do
echo Hi
done
#simple for loop-3for (( j = 1 ; j
-
7/28/2019 Bash Shell Programming
17/21
#print files with extension .cfor file in *.c
do
echo "Filename==>$file"#place any no.of cmds
cp $file /home/oss/cprogs
done
-
7/28/2019 Bash Shell Programming
18/21
while loopsyntax
while [ condition ]
do
code block;
done
-
7/28/2019 Bash Shell Programming
19/21
#while_ex.sh
verify="n"
while [ "$verify" != y ]
do
echo "Enter option: "
read option
echo "You entered $option. Is thiscorrect? (y/n)"
read verify
done
-
7/28/2019 Bash Shell Programming
20/21
?
-
7/28/2019 Bash Shell Programming
21/21
Thank You !!!!
Happy Learning
&
Happy Coding :-)
Don't forgot to checkout
Online Linux Terminal
www.webminal.org