Upload
mohitohri
View
237
Download
0
Embed Size (px)
Citation preview
8/13/2019 Bash Shell Scripting Linux
1/19
Bash Shell Scripting
Shell is just the platform where your comment gets compiled and get executed, which has thecapacity to convert the binary codes int machinery language and return output. There are somany shell platform like /bin/bash, /bin/tsh, /usr/bin/python, /usr/bin/perl etc..
Shell scripting (Automation): A shell scriptis ascriptwritten for theshell,orcommand lineinterpreter,of anoperating system.It is often considered a simpledomain-specific programminglanguage.Typical operations performed by shell scripts include file manipulation, programexecution, and printing text.
Ok,Shell Scripting is just a text file which contains of sequences of commands put together lineby line to perform a customized task as per your requirement to be executed. They are used toautomate commonly used commands, as System Administration Tools, to develop SimpleProgram and Manipulation of text or files.
Arithmetic Operators
-eq = equals to
-ne = not equal to
-gt = greater than
-ge = greater than or equals to
-lt = less than
-le = less than or equals to
-a = and
Conditional Execution
|| = logical or
&& = logical and
=======================
[root@station1]# vi script1
a=10b=5a=`expr $a7`echo a is equal to $a
http://en.wikipedia.org/wiki/Scripting_languagehttp://en.wikipedia.org/wiki/Scripting_languagehttp://en.wikipedia.org/wiki/Scripting_languagehttp://en.wikipedia.org/wiki/Shell_%28computing%29http://en.wikipedia.org/wiki/Shell_%28computing%29http://en.wikipedia.org/wiki/Shell_%28computing%29http://en.wikipedia.org/wiki/Command_line_interpreterhttp://en.wikipedia.org/wiki/Command_line_interpreterhttp://en.wikipedia.org/wiki/Command_line_interpreterhttp://en.wikipedia.org/wiki/Command_line_interpreterhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Domain-specific_programming_languagehttp://en.wikipedia.org/wiki/Domain-specific_programming_languagehttp://en.wikipedia.org/wiki/Domain-specific_programming_languagehttp://en.wikipedia.org/wiki/Domain-specific_programming_languagehttp://en.wikipedia.org/wiki/Domain-specific_programming_languagehttp://en.wikipedia.org/wiki/Domain-specific_programming_languagehttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Command_line_interpreterhttp://en.wikipedia.org/wiki/Command_line_interpreterhttp://en.wikipedia.org/wiki/Shell_%28computing%29http://en.wikipedia.org/wiki/Scripting_language8/13/2019 Bash Shell Scripting Linux
2/19
a=`expr $a \* $b`echo a is equal to $aa=`expr $a / 3`echo a is equal to $a==============
Shift + ; -> :
:wq > Save and Quit
-
Making the script executable:
#chmod +x script1 > you have to save all the script and make executable before runningthem.
Running the shell script. (U can run the script with two ways,)
1) run the script from the current location and like (./script1)
2) make a bin directory inside the users home directory (like #mkdir /home/abhandari/bin,you dont have to worry about the path of the script i:e u can run it from any location just typing# script1 like this)
# ./script1
or
# mkdir /root/bin
# cd /root/bin
# script1
==================
Example2
==================
[root@station1]# vi script2
echo Enter the value for a:
read a
8/13/2019 Bash Shell Scripting Linux
3/19
echo Enter the value for b:
read b
a=`expr $a7`
echo a is equal to $a
a=`expr $a \* $b`
echo a is equal to $a
a=`expr $a / 3`
echo a is eual to $a
:wq
[root@station1]# bash script2
==================
If Construct / Condition
==================
Example3
==================
[root@station1]#vi ifscript1
echon Enter your no.:
read N
if [ $Nge 1a $Nlt 100 ]
then echo Your input No. is between 1 and 100.
else echo Number is greater than 100
fi
:wq
8/13/2019 Bash Shell Scripting Linux
4/19
[root@station1]# bash ifscript1
==================
Example4
==================
[root@station1]# vi ifscript2
echo -n Enter your no.:
read N
if [ $N -lt 50 ]
then echo The No. you entered is less than 50
elif [ $N -ge 50 -a $N -lt 60 ]
then
echo The No. you entered is between 50 and 60
elif [ $N -ge 60 -a $N -le 100 ]
then
echo The No. you entered is between 60 and 100
else
echo The No.you entered wrong entry
fi
:wq
[root@station1]# bash ifscript2
Exit Command
==================
Example5
8/13/2019 Bash Shell Scripting Linux
5/19
==================
[root@station1]# vi exit
echo Do you like to exit..?
read ans
if [ $ans = "y" ]
then exit
fi
:wq
[root@station1]# bash exit
While Construct
==================
Example6
==================
[root@station1]# vi while
while true
do
echon Do you wish to continue..?
read ans
if test $ans = n
then exit
fi
done
:wq
8/13/2019 Bash Shell Scripting Linux
6/19
[root@station1]# bash while
==================
Example7
==================
[root@station1]# vi reply
ans=y
#while test $reply !=n
while test $ans!=n
do
echo -n Enter your desired number:
read N
if [ $N -ge 1 -a $N -lt 100 ]
then echo The number you entered is between 1 and 100
else echo The number you entered is greater than 100
fi
echo -n -e Would you like to continue..? \n\t[Enter to Continue]\n\t [Press Ctrl + C to quit]:
read ans
done
:wq
[root@station1]# bash reply
==================
Example8
==================
8/13/2019 Bash Shell Scripting Linux
7/19
[root@station1]# vi reply1
reply = y
while test $reply ! = n
do
echon Enter the filename:
read filename
cat ${filename}
echon Would you like to enter another filename?
read reply
done
:wq
[root@station1]# bash reply1
==================
Example9
==================
[root@station1]#vi reply3
while:
do
read-p "Enter two numnbers ( - 1 to quit ) : " a b
if[$a -eq -1 ]
then
break
fi
ans=$((a + b ))
echo$ans
done
[root@station1]# bash reply3
For Construct
==================
8/13/2019 Bash Shell Scripting Linux
8/19
Example10
==================
[root@station1]# vi forin
for name in zeshan rabi sandeep
do
echo ${name}
done
:wq
[root@mycentos script]# bash forin
==================
Example11
==================
[root@mycentos script]# vi forloop
#!/bin/bash
for i in {1..5}
do
echo Welcome $i times
done
===
[root@mycentos script]# ./forloop
Welcome 1 times
Welcome 2 times
Welcome 3 times
8/13/2019 Bash Shell Scripting Linux
9/19
Welcome 4 times
Welcome 5 times
==================
Example12
==================
[root@mycentos script]# vi forloopwith_seq
#!/bin/bash
for i in $(seq 1 2 20)
do
echo Welcome $i times
done
[root@mycentos script]# ./forloopwith_seq
Welcome 1 times
Welcome 3 times
Welcome 5 times
Welcome 7 times
Welcome 9 times
Welcome 11 times
Welcome 13 times
Welcome 15 times
Welcome 17 times
8/13/2019 Bash Shell Scripting Linux
10/19
Welcome 19 times
==================
Example13
==================
[root@station1]# vi for2
for NAMEFILE in `ls /home/`
do
echo File listing are ${NAMEFILE}
cat /home/${NAMEFILE}
done
:wq
[root@station1]# bash for2
Case
==================
Example14
==================
[root@station1]# vi case
echo Enter the choice to run command
echo 1) date 2) who
echo 3) ls 4) pwd
read choice
case $choice in
1)
8/13/2019 Bash Shell Scripting Linux
11/19
date;;
2)
who;;
3)
ls;;
4)
pwd;;
*)
echo There is no such choice for this No.
esac
:wq
[root@station1]# bash case
==================
Example15
==================
[root@station1]# vi case2
while true
do
echo Enter the choice to run command.
Echo press q to quit.
echo 1) date 2) who
echo 3) ls 4) pwd
read choice
8/13/2019 Bash Shell Scripting Linux
12/19
case $choice in
1)
date;;
2)
who;;
3)
ls;;
4)
pwd;;
q)
break;;
*)
echo There is no such choice for this No.
esac
done
:wq
==================
Example16
==================
# cat > menuui
## Script to create simple menus and take action according to that selected# menu item#while :doclearecho "-------------------------------------"echo " Main Menu "
8/13/2019 Bash Shell Scripting Linux
13/19
echo "-------------------------------------"echo "[1] Show Todays date/time"echo "[2] Show files in current directory"echo "[3] Show calendar"echo "[4] Start editor to write letters"echo "[5] Exit/Stop"echo "======================="echo -n "Enter your menu choice [1-5]: "read yourchcase $yourch in1) echo "Today is `date` , press a key. . ." ; read ;; 2) echo "Files in `pwd`" ; ls -l ; echo "Press a key. . ." ; read ;; 3) cal ; echo "Press a key. . ." ; read ;; 4) vi ;;5) exit 0 ;;*) echo "Opps!!! Please select choice 1,2,3,4, or 5";echo "Press a key. . ." ; read ;; esacdone
[root@station1]# bash menuui
[root@station1]#
Test command with if
==================
Example17
==================
[root@station1]# vi testwithif
echon Enter your filename
read fname
if testf $fname
then echo $fname is an ordinary file.
elif testd $fname
then echo $fname is a directory.
elif tests $fname
then echo $fname is not an emply file.
8/13/2019 Bash Shell Scripting Linux
14/19
elif testr $fname
then echo $fname exists & is readable.
elif test !r $fname
then echo $fname is a not readable
elif testL $fname
then echo $fname is a symbolic link file.
fi
:wq
[root@station1]# bash testwithif
====================
-dtrue if file is a directory
-ftrue if file exists
-htrue if file is a slymbolic link
-Ltrue if file is a symbolic link
-rtrue if file is readable by you
-strue if file exists and is not empty
-e = check if the file exists or not.
-w = writeable
-x = executable
-b = block device file
-O = owned by current user or not
-G = owned by group of current user or not
String Operators:
8/13/2019 Bash Shell Scripting Linux
15/19
-x stringtrue if string is empty
-n string true if string is not empty
String1 = string2true if the strings are equal
String
Further help of this type run help test
Expression
==================
Example19
==================
[root@station1]# vi expr
A = 1
while test ${A}lt 100
do
echo `expr ${A} \* ${A}`
A = `expr ${A} + 1`
done
:wq
[root@station1]# bash expr
Input from file
==================
Example20
==================
[root@station1]# vi inputfile
8/13/2019 Bash Shell Scripting Linux
16/19
while read infile
do
echo $infile
done < /etc/passwd
:wq
[root@station1]# bash inputfile
==================
Example21
==================
[root@station1]# vi inputfile2
while read finput
do
login = $(echo $finput | cutd : f1)
directory = $(echo $finput | cut d : f6-8)
echo login = $login and details = $directoru
done < /etc/passwd
exit()
:wq
[root@station1]# bash inputfile2
==================
Example22
==================
[root@station1]# vi inputfile3
8/13/2019 Bash Shell Scripting Linux
17/19
lsla > ./result
while read finput
do
filename = $(echo $finput | cut d f9)
permission = $(echo $finput | cut d f1)
echo filename = $filename and permission = $permission
done < ./result
:wq
==================
Example-23
Shell Script to add user and password input from file (need 2 files if best)
==================
Create Users using file containing List of Users/Password/Description with commaseparated===========There are two files with a shell script and users (users.sh ) list.
[root@station1 test2]# vi users.txt
=======================user100,password100,User1_Staffuser200,password200,User2_Staffuser300,password300,User3_Staff=========================
[root@station1 test2]# cat users.sh#!/bin/sh
#for line in `more sortdetails.txt`for line in `more users.txt`# while read linedousername=`echo $line | cut -d, -f1`password=`echo $line | cut -d, -f2`name=`echo $line | cut -d, -f3`#echo $username $password $name
8/13/2019 Bash Shell Scripting Linux
18/19
/usr/sbin/useradd -c $name -p $password $usernameecho $password > pwcat pw | passwdstdin $usernamedoneexit 1
==================root@station1test2]# chmod a+x users.sh[root@vmail01 test2]# sh users.sh
Example 24
==================
Script with argument passing on command line
[root@r6 bin]# cat plus
a=$1b=$2c=$3z=`expr $b$c`echo value $zecho addition $0echo addition $1echo addition $2echo addition $3[root@r6 bin]# ./plus 2 3 5value -2
addition ./plusaddition 2addition 3addition 5[root@r6 bin]#
==================
Example 25
=============
[root@r6 bin]# cat plusa=$1b=$2c=$3z=`expr $b$c`echo Substracted value from second arg to third: $zecho dollor $@
mailto:root@vmail01mailto:root@vmail01mailto:root@vmail018/13/2019 Bash Shell Scripting Linux
19/19
echo Total arguments value $#echo Command run successfully or not {zero=successful} $?echo First value is command: $0echo First argument value passed: $1echo Second argument value passed: $2
echo Third argument value passed: $3[root@r6 bin]# ./plus 2 3 5Substracted value from second arg to third: -2dollor 2 3 5Total arguments value 3Command run successfully or not {zero=successful} 0First value is command: ./plusFirst argument value passed: 2Second argument value passed: 3Third argument value passed: 5[root@r6 bin]#
================
Automation is a vast topic. This is a tips only. So you guys really got Enjoy with thisi know.Stay tuned this will be further updated as i got the time. Thanks for Visiting this site.