10/2/2007
Net212
SHELL PROGRAMMING WITH BASH
Intro The difference between tcsh and bash is not
so noticeable à It is apparent when writing shell scripts
What is a shell script? Wh i h ll i ? à Set of commands that are executed from a file à Flow of execution can be controlled
Shell script anatomy Beginning à #!/bin/bash
Body à #this is a remark or comment à grep $1 /etc/passwd
File attributes à ‐rwxrwxr‐x 1 student student 31 Sept 21 18:15
printinfo.sh
1
10/2/2007
Making a script 1. Open new file à
vi printinfo.sh
2. Put in beginning (specify shell) à
#!/bin/bash / /
3. Put in body (commands and structures) à
grep $1 /etc/passwd
4. Save file à
Esc , :wq
5. Make it executable à
chmod a+x printinfo.sh
Executing it If it’s not in your path à /path/to/script/printinfo.sh student
If it’s in your path à printinfo.sh student
If it’s not in your path and in your current
folder à ./printinfo.sh student
Script arguments grep $1 /etc/passwd à $1 stands for first argument à $2 would be the second argument and so on… à $0 is the script name
Expand last example à grep $1 /etc/passwd à grep $1 /etc/passwd | cut –f $2 –d “:” à echo thank you for using script $0
Use it à à à à
./printinfo.sh student 3 student:x:500:500:student:/home/student:/bin/bash 500 Thank you for using script ./printinfo.sh
2
10/2/2007
Another example Last example printed username from passwd
file and its uid How about an account creation script à #!/bin/sh à useradd $1 à passwd $1
Save it as mkuser.sh and run it à ./ ./mkuser mkuser linuxguy à Changing password for linuxguy
Another example Save process list à #!/bin/bash à ps –U $USER > processes.txt à chmod h d a+x listprocess.sh li t h à ./listprocess.sh
Control the execution flow Use if statements à If (condition is true) à Statements
Use while loops hl l à While(condition is true) à Statements
3
10/2/2007
If statements Does a file exist? à if [ ‐f /etc/inittab ] #if [condition] à then echo yes #statements h # t t t à fi #end if
Run it à chmod a+x checkinittab.sh à ./checkinittab.sh à yes
Variables Argument variables à $0, $1, $2,…
Internal variables à myvar=“/bin/bash” à avalue=`grep student /etc/passwd | cut –f 7 –d “:”`
Back quotes are for command results!!! à cnt=`ls | wc –l` Å same key as tilde! à cnt=‘ls | wc –l’ <‐ Same key as double quote
Variables with if Check if anything in current folder à cnt=`ls | wc –l` à if [ $cnt –gt 0] #is count greater than zero? à then #yes it is h # i i à echo there are files here à else #no it isn’t à echo no files here à fi
4
10/2/2007
Conditional operators Operators for numbers à ‐gt Greater than à ‐eq Equal to à ‐lt l less than l h à ‐ne Not equal to
Operators for words and sentences (strings) à = equal à != not equal
Another if example Check if .bash_profile exists à bashfile=“.bash_profile” #a string variable à if [ `ls –a .bash_profile` = “$bashfile” ] à then h à echo it exists à else à echo it’s not there à fi
Sum up variable usage Numbers do not use double quotes à à à à
Mynum=10 if [ $Mynum –lt 11 ] Echo $Mynum is less than 11 Fi
Strings do use double quotes à à à à
Mystr=“david” if [ “$Mystr” != “david” ] echo This is not david fi
5
10/2/2007
Sum up if statements For comparing values à à à à
if [ value1 operator value2 ] then statements fi
For checking the exit code à à à à
if some command here then #do this if exit code is 0 statements fi
Exit codes Also known as an exit status code à type echo $? echo $? To see the last exit code
Status code 0 means success à ls /etc/issue à echo $? à 0
Status code 1 means failure à ls /etc/issues à echo $? à 1
While statements Do something until condition is false à Uses same conditional expressions as if
Ex: Create 10 files à à à à à à à
Stoppoint=11 i=1 while [ $i ‐lt $Stoppoint ] do touch myfile$i i=$[$i+1] done
6
10/2/2007
Another while example Read input from a pipe à ls | while read file à do à echo $USER has file $file h USER h fil fil à done
Results à student has file myfile1 à student has file myfile2 à ….
7