KEMBAR78
Shell scripting | PDF
Shell Scripting
Ashrith Mekala
Commands (Recap)
• passwd
• pwd
• cd
• ls
• file
• cat/pr
• page/more/less
• clear
• cp/copy
• chown
• chgrp
• chmod
• rm
• mv
• mkdir
• rm
• grep
• egrep
• find
Commands (Recap) 2
• >>
• >
• |
• ||
• &
• &&
• date
• echo
• sleep
• wc
• head
• tail
• diff/sdiff
• cal
• who/w/whoami/fi
nger/id
• df/du
• ps
• netstat
• vmstat
Commands (Recap) 3
• iostat
• mpstat
• uname
• which
• basename
• man
• su
• cut
• sed/awk
• vi/emacs
• wget
• rpm
• yum
• ln
• service
• ssh
• scp
• rsync
• mail/mailx/send
mail
• Shell is command interpreter
• Shell supports scripting
• Used to automate repetitive admin tasks as well as
solve some problems.
• Shell forks a sub-shell to execute commands and
scripts
What is Shell ?
What is Shell Script ?
• A small program that is executed by the shell
• The script can be a text file which contains:
• shell commands
• shell flow control structures
• variables
Your First Shell Script
#!/usr/bin/env bash
echo "Hello World!"
Running script
• bash  scriptname
• chmod  +x  scriptname  &&  ./scriptname
Shell Customization
• Every shell supports some customization
• Customization takes place in startup files
• Shell Flavors:
• Bourne -- sh, ksh, bash, zsh (fast)
• C -- csh, tcsh (better for customization)
• Check Shell: echo  $SHELL
• Switch Shell: exec  <shellname>
• Change Shell: chsh  -­‐s  <path_to_shell>
Set Command
• Set command is a shell built in
• Display the names and values of shell
• Allow to change values of shell options
• some options:
• noclobber  (prevent  exit  of  shell)
• ignoreeof  (prevent  overwriting  files)
• debugging
• -­‐x
• -­‐v
Startup Scripts
• /etc/profile
• all shells & all users
• /etc/profile.d
• application specific startup files
• ~/.profile
• read in the absence of .bash_profile
• ~/.bash_profile
• only for login shells
• ~/.bashrc
• only if called from .bash_profile
Global Variables
• PWD
• PATH
• HOME
• MAIL
• TERM
• HISTFILE
Setting Shell Variables
• Using assignment command
HOME="/etc"
PATH=/usr/bin:/usr/etc:/sbin
NEWVAR="this  is  a  new  var"
Environment Variables
• export
• exporting a variable makes it available to sub-
shells
• sub-shells inherit parents shell exported variables
Customizing Shell Interactions
• PS1 - default interaction prompt
• PS2 - continuation interactive prompt
• PS3 - prompt used by ‘select’ inside shell script
• PS4 - Used by ‘set -x’ to prefix tracing output
• PROMPT_COMMAND - executes just before displaying the PS1
export PS1="([033[01;32m]T[033[00m])
[033[01;34m]u[033[00m]@[033[0;35m]mac[
033[00m]:wn[e[00m][033[0;32m]$(parse_git
_branch_and_add_brackets)[033[0m]$"
MetaCharacters
• some of the shell meta-characters are:
• command redirection >  >>  <  |
• File specification *  []  ?  {}
• command sequencing ;  ||  &&  ()
• Background &
• Grouping Text “    ‘
• commenting #
• backslash 
• redirection &>,  &>>
Quoting
• Strong quoting
• Weak quoting
Command Substitution
• `command`
• $(command)
Wild Cards
• * (match any string)
• ? (match any single char)
• [set] - match chars listed in set
• m[a,o,u]m  -­‐ mam,  mom,  mum
• { } - multiple alternatives
• cp  {*.doc,*.pdf}  ~
Combined Command Execution
• sequenced commands on same line separated by ;
• Grouped Commands
• (cmd1;cmd2;cmd3)
• {cmd1;cmd2;cmd3}
• Conditional Commands
• &&
• ||
Shell Script IO
• Input
• read
• Output
• printf  /  echo
printf "What is your name? -> "
read NAME
echo "Hello, $NAME. Nice to meet you."
IFS (Internal Field
Seperator)
• Change the behavior of:
• read command
• parameter expansions
• command substitution
#!/bin/bash
printf "Type three numbers separated by 'q'. -> "
IFS="q"
read NUMBER1 NUMBER2 NUMBER3
echo "You said: $NUMBER1, $NUMBER2, $NUMBER3"
Variable Expansion and IFS
• Operations effected by IFS
• read
• variable expansion
#!/bin/bash
IFS=":"
LIST="a:b:c d"
for i in $LIST ; do
echo $i
done
Pipes & Redirection
• File descriptors
• stdin (0)
• stdout (1)
• stderr (2)
• Pipe (|)
• Redirection (>,>>,<)
stdout & stderr
• stdout is designated by the file descriptor 1
• stderr is designated by 2
• To redirect stderr use 2>
• redirect both stdout and stderr 2>&1
• ls  nofile  >  listing  2>&1
• ls  nofile  &>  listing
Conditional Expressions
• test or []
• [ expression ] returns an exit status of 0 (success) or 1
(failure)
• string comparisons ==,  !=,  <,  >,  >=,  <=
• arithmetic operators -­‐lt,-­‐gt,-­‐le,-­‐ge,-­‐eq,-­‐ne
• Logical expressions !,-­‐a,-­‐o
• [[ test ]] -> more c like usage (extended test command)
• supports &&,  ||,  =~ (complete list: http://goo.gl/DiHrPH)
[ -e ~/.bashrc -a ! -d ~/.bashrc ] && echo true
[[ -e ~/.bashrc && ! -d ~/.bashrc ]]
Arithmetic Expressions
• bash treats variables as strings
• by using arithmetic expression syntax (( exp )) or
let - bash treats them as numericals
• x=1
((  x=x+1  ))
Conditional Stmts - IF
# always execute
if true; then
ls
else
echo "true is false."
fi
# never execute
if false; then
ls
fi
read A
if [ "$A" == "foo" ] ; then
echo "Foo"
elif [ "$A" == "bar" ] ; then
echo "Bar"
else
echo "Other"
fi
Conditional Stmts - CASE
case $opt in
a ) echo "option a";;
b ) echo "option b";;
c ) echo "option c";;
? ) echo 
'usage: alice [-a] [-b] [-c] args...'
exit 1;;
esac
Loops - For
for i in *.JPG ; do
mv "$i" "$(echo $i | sed 's/.JPG$/.x/')"
done
for i in a b c d ; do
echo $i
done
#extended for loop
for (( i = 1 ; i <= $1 ; i++ )) ; do
echo "I is $i"
done
Loops - While
#!/bin/sh
COUNTER=0
while [ $COUNTER -lt 10 ] ; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
COUNTER=0
while (( COUNTER < 10 )) ; do
echo The counter is $COUNTER
(( COUNTER = COUNTER+1 ))€šÃ„è
done
Loops - Until
COUNTER=20
until [ $COUNTER -lt 10 ]
do
echo COUNTER $COUNTER
let COUNTER-=1
done
Example
#!/bin/sh
LOOP=0
while [ $LOOP -lt 20 ] ; do
# The next line is explained in the
# math chapter.
VAL=`expr $LOOP % 10`
case "$VAL" in
( 0 ) echo "ZERO" ;;
( 1 ) echo "ONE" ;;
( 2 ) echo "TWO" ;;
( 3 ) echo "THREE" ;;
( 4 ) echo "FOUR" ;;
( 5 ) echo "FIVE" ;;
( 6 ) echo "SIX" ;;
( 7 ) echo "SEVEN" ;;
( 8 ) echo "EIGHT" ;;
( 9 ) echo "NINE" ;;
( * ) echo "This shouldn't happen." ;;
esac
LOOP=$(($LOOP + 1))
Loop Controls
• break - terminates the loop
• continue - causes a jump to the next iteration of
the loop
Result Codes
• Result codes, also known as return values, exit status
• Very critical aspect of shell scripting
• Possible range will be 0-255
• Ways to use exit statues
• using test with if
• using $?
• and (&&) operator
Result Codes - Ex
if ls mysillyfilename ; then
echo "File exists."
fi
ls mysillyfilename
if [ $? -eq 0 ] ; then
echo "File exists."
fi
ls mysillyfilename && echo "File exists."
Special Variables
• $0 - name of the command being executed
• $1-­‐$9 - positional parameters
• ${10} … ${N} - more positional parameters
• $# - no. of args
• $* - all args
• $@ - all args
• $? - return value of last command
• $$ - process id of shell
• $! - pid of last bg process
Handle Command Line Args
#!/usr/bin/env bash
for i in "$@" ; do
echo ARG $i
done
#!/usr/bin/env bash
echo "first parameter is $1"
echo "Second parameter is $2"
echo "Third parameter is $3"
exit 0
Handle Command Line Args - Shift
#!/bin/bash
while [[ $# -gt 0 ]]; do
case $1 in
-p)
pages=$2
shift
;;
-l)
length=$2
shift
;;
-t)
time=$2
shift
;;
*)
echo >&2 "Invalid argument: $1"
;;
esac
shift
done
Handle Command Line Args - getopts
#!/bin/bash
while getopts p:l:t: opt; do
case $opt in
p)
pages=$OPTARG
;;
l)
length=$OPTARG
;;
t)
time=$OPTARG
;;
esac
done
Subroutines
#!/bin/sh
mysub()
{
echo "Arg 1: $1"
}
mysub "This is an arg"
Subroutines (2)
#!/bin/sh
mysub()
{
local MYVAR
MYVAR=3
echo "SUBROUTINE: MYVAR IS $MYVAR";
}
MYVAR=4
echo "MYVAR INITIALLY $MYVAR"
mysub "This is an arg"
echo "MYVAR STILL $MYVAR"
Sourcing
#!/bin/sh
MYVAR=4
# The next line sources the external script.
. /path/to/mysub.sh
echo "MYVAR INITIALLY $MYVAR"
mysub "This is an arg"
echo "MYVAR STILL $MYVAR"
Regular Expressions
• Powerful mechanism for text processing
• Uses:
• search for a pattern within a block of text
• replace bits of text
• manipulate strings
• Using external tools: grep, awk, sed, perl.
Using Regex - Basics
#replace occurrence of a to A
echo "This is a test, this is only a test" | sed 's/a/A/g'
#search for bar in the file foo.txt
grep "bar" foo.txt
cat foo.txt | grep "bar"
Regex - Examples
# Expression: /test/
grep 'test' poem.txt
# Expression: /^Mary/
grep "^Mary" < poem.txt
# Expression: /fox$/
grep "fox$" < poem.txt
# Expression: s/Mary/Joe/
sed "s/Mary/Joe/" < poem.txt
# Expression s/Mary/Joe/g
sed "s/Mary/Joe/g" < poem.txt
# Expression: /wa./
grep 'wa.' poem.txt
# Expression: /Mary.*lamb/
grep "Mary.*lamb" poem.txt
Regex - Modifiers
• overall behavior of a regular expression can be
tuned using a number of modifiers
• /i - case insensitive
• /g - replace globally
• /x - ignore space globally
AWK
• Processing structured data records containing text
• AWK centers around dividing the input text into
records
AWK - Examples
#!/bin/bash
awk '{ print }' /etc/passwd
awk '{ print $0 }' /etc/passwd
#Multiple Fields
awk -F":" '{ print $1 }' /etc/passwd # print out a list of all user accounts
awk -F":" '{ print $1 $3 }' /etc/passwd # prints in separate lines
awk -F":" '{ print $1 " " $3 }' /etc/passwd # concatenates $1 and $3
awk -F":" '{ print "username: " $1 "ttuid:" $3 }' /etc/passwd # labels
awk -F":" '{print $1,$NF;}' /etc/passwd
#Cool stuff
awk -F":" 'BEGIN {print "UsernametAccountTypetShellType";} 
{print $1,"t",$5,"t",$NF;} 
END{print "End n--------------"; 
}' /etc/passwd
awk -F":" '$3 >200' /etc/passwd #UID greater than 200
awk -F":" '$5 ~/System/' /etc/passwd #system users
awk -F":" 'BEGIN { count=0;} 
$5 ~/System/ { count++; } 
END { print "Number of System Users = "count;}' /etc/passwd #count system users
Demo
• Demo User Management System
Assignment
• Build a Contact Management System
More !!!
• http://tldp.org/LDP/abs/html/
• http://bash.cyberciti.biz/guide/Main_Page
End Scripting

Shell scripting

  • 1.
  • 2.
    Commands (Recap) • passwd •pwd • cd • ls • file • cat/pr • page/more/less • clear • cp/copy • chown • chgrp • chmod • rm • mv • mkdir • rm • grep • egrep • find
  • 3.
    Commands (Recap) 2 •>> • > • | • || • & • && • date • echo • sleep • wc • head • tail • diff/sdiff • cal • who/w/whoami/fi nger/id • df/du • ps • netstat • vmstat
  • 4.
    Commands (Recap) 3 •iostat • mpstat • uname • which • basename • man • su • cut • sed/awk • vi/emacs • wget • rpm • yum • ln • service • ssh • scp • rsync • mail/mailx/send mail
  • 5.
    • Shell iscommand interpreter • Shell supports scripting • Used to automate repetitive admin tasks as well as solve some problems. • Shell forks a sub-shell to execute commands and scripts What is Shell ?
  • 6.
    What is ShellScript ? • A small program that is executed by the shell • The script can be a text file which contains: • shell commands • shell flow control structures • variables
  • 7.
    Your First ShellScript #!/usr/bin/env bash echo "Hello World!"
  • 8.
    Running script • bash scriptname • chmod  +x  scriptname  &&  ./scriptname
  • 9.
    Shell Customization • Everyshell supports some customization • Customization takes place in startup files • Shell Flavors: • Bourne -- sh, ksh, bash, zsh (fast) • C -- csh, tcsh (better for customization) • Check Shell: echo  $SHELL • Switch Shell: exec  <shellname> • Change Shell: chsh  -­‐s  <path_to_shell>
  • 10.
    Set Command • Setcommand is a shell built in • Display the names and values of shell • Allow to change values of shell options • some options: • noclobber  (prevent  exit  of  shell) • ignoreeof  (prevent  overwriting  files) • debugging • -­‐x • -­‐v
  • 11.
    Startup Scripts • /etc/profile •all shells & all users • /etc/profile.d • application specific startup files • ~/.profile • read in the absence of .bash_profile • ~/.bash_profile • only for login shells • ~/.bashrc • only if called from .bash_profile
  • 12.
    Global Variables • PWD •PATH • HOME • MAIL • TERM • HISTFILE
  • 13.
    Setting Shell Variables •Using assignment command HOME="/etc" PATH=/usr/bin:/usr/etc:/sbin NEWVAR="this  is  a  new  var"
  • 14.
    Environment Variables • export •exporting a variable makes it available to sub- shells • sub-shells inherit parents shell exported variables
  • 15.
    Customizing Shell Interactions •PS1 - default interaction prompt • PS2 - continuation interactive prompt • PS3 - prompt used by ‘select’ inside shell script • PS4 - Used by ‘set -x’ to prefix tracing output • PROMPT_COMMAND - executes just before displaying the PS1 export PS1="([033[01;32m]T[033[00m]) [033[01;34m]u[033[00m]@[033[0;35m]mac[ 033[00m]:wn[e[00m][033[0;32m]$(parse_git _branch_and_add_brackets)[033[0m]$"
  • 16.
    MetaCharacters • some ofthe shell meta-characters are: • command redirection >  >>  <  | • File specification *  []  ?  {} • command sequencing ;  ||  &&  () • Background & • Grouping Text “    ‘ • commenting # • backslash • redirection &>,  &>>
  • 17.
  • 18.
  • 19.
    Wild Cards • *(match any string) • ? (match any single char) • [set] - match chars listed in set • m[a,o,u]m  -­‐ mam,  mom,  mum • { } - multiple alternatives • cp  {*.doc,*.pdf}  ~
  • 20.
    Combined Command Execution •sequenced commands on same line separated by ; • Grouped Commands • (cmd1;cmd2;cmd3) • {cmd1;cmd2;cmd3} • Conditional Commands • && • ||
  • 21.
    Shell Script IO •Input • read • Output • printf  /  echo printf "What is your name? -> " read NAME echo "Hello, $NAME. Nice to meet you."
  • 22.
    IFS (Internal Field Seperator) •Change the behavior of: • read command • parameter expansions • command substitution #!/bin/bash printf "Type three numbers separated by 'q'. -> " IFS="q" read NUMBER1 NUMBER2 NUMBER3 echo "You said: $NUMBER1, $NUMBER2, $NUMBER3"
  • 23.
    Variable Expansion andIFS • Operations effected by IFS • read • variable expansion #!/bin/bash IFS=":" LIST="a:b:c d" for i in $LIST ; do echo $i done
  • 24.
    Pipes & Redirection •File descriptors • stdin (0) • stdout (1) • stderr (2) • Pipe (|) • Redirection (>,>>,<)
  • 25.
    stdout & stderr •stdout is designated by the file descriptor 1 • stderr is designated by 2 • To redirect stderr use 2> • redirect both stdout and stderr 2>&1 • ls  nofile  >  listing  2>&1 • ls  nofile  &>  listing
  • 26.
    Conditional Expressions • testor [] • [ expression ] returns an exit status of 0 (success) or 1 (failure) • string comparisons ==,  !=,  <,  >,  >=,  <= • arithmetic operators -­‐lt,-­‐gt,-­‐le,-­‐ge,-­‐eq,-­‐ne • Logical expressions !,-­‐a,-­‐o • [[ test ]] -> more c like usage (extended test command) • supports &&,  ||,  =~ (complete list: http://goo.gl/DiHrPH) [ -e ~/.bashrc -a ! -d ~/.bashrc ] && echo true [[ -e ~/.bashrc && ! -d ~/.bashrc ]]
  • 27.
    Arithmetic Expressions • bashtreats variables as strings • by using arithmetic expression syntax (( exp )) or let - bash treats them as numericals • x=1 ((  x=x+1  ))
  • 28.
    Conditional Stmts -IF # always execute if true; then ls else echo "true is false." fi # never execute if false; then ls fi read A if [ "$A" == "foo" ] ; then echo "Foo" elif [ "$A" == "bar" ] ; then echo "Bar" else echo "Other" fi
  • 29.
    Conditional Stmts -CASE case $opt in a ) echo "option a";; b ) echo "option b";; c ) echo "option c";; ? ) echo 'usage: alice [-a] [-b] [-c] args...' exit 1;; esac
  • 30.
    Loops - For fori in *.JPG ; do mv "$i" "$(echo $i | sed 's/.JPG$/.x/')" done for i in a b c d ; do echo $i done #extended for loop for (( i = 1 ; i <= $1 ; i++ )) ; do echo "I is $i" done
  • 31.
    Loops - While #!/bin/sh COUNTER=0 while[ $COUNTER -lt 10 ] ; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done COUNTER=0 while (( COUNTER < 10 )) ; do echo The counter is $COUNTER (( COUNTER = COUNTER+1 ))€šÃ„è done
  • 32.
    Loops - Until COUNTER=20 until[ $COUNTER -lt 10 ] do echo COUNTER $COUNTER let COUNTER-=1 done
  • 33.
    Example #!/bin/sh LOOP=0 while [ $LOOP-lt 20 ] ; do # The next line is explained in the # math chapter. VAL=`expr $LOOP % 10` case "$VAL" in ( 0 ) echo "ZERO" ;; ( 1 ) echo "ONE" ;; ( 2 ) echo "TWO" ;; ( 3 ) echo "THREE" ;; ( 4 ) echo "FOUR" ;; ( 5 ) echo "FIVE" ;; ( 6 ) echo "SIX" ;; ( 7 ) echo "SEVEN" ;; ( 8 ) echo "EIGHT" ;; ( 9 ) echo "NINE" ;; ( * ) echo "This shouldn't happen." ;; esac LOOP=$(($LOOP + 1))
  • 34.
    Loop Controls • break- terminates the loop • continue - causes a jump to the next iteration of the loop
  • 35.
    Result Codes • Resultcodes, also known as return values, exit status • Very critical aspect of shell scripting • Possible range will be 0-255 • Ways to use exit statues • using test with if • using $? • and (&&) operator
  • 36.
    Result Codes -Ex if ls mysillyfilename ; then echo "File exists." fi ls mysillyfilename if [ $? -eq 0 ] ; then echo "File exists." fi ls mysillyfilename && echo "File exists."
  • 37.
    Special Variables • $0- name of the command being executed • $1-­‐$9 - positional parameters • ${10} … ${N} - more positional parameters • $# - no. of args • $* - all args • $@ - all args • $? - return value of last command • $$ - process id of shell • $! - pid of last bg process
  • 38.
    Handle Command LineArgs #!/usr/bin/env bash for i in "$@" ; do echo ARG $i done #!/usr/bin/env bash echo "first parameter is $1" echo "Second parameter is $2" echo "Third parameter is $3" exit 0
  • 39.
    Handle Command LineArgs - Shift #!/bin/bash while [[ $# -gt 0 ]]; do case $1 in -p) pages=$2 shift ;; -l) length=$2 shift ;; -t) time=$2 shift ;; *) echo >&2 "Invalid argument: $1" ;; esac shift done
  • 40.
    Handle Command LineArgs - getopts #!/bin/bash while getopts p:l:t: opt; do case $opt in p) pages=$OPTARG ;; l) length=$OPTARG ;; t) time=$OPTARG ;; esac done
  • 41.
  • 42.
    Subroutines (2) #!/bin/sh mysub() { local MYVAR MYVAR=3 echo"SUBROUTINE: MYVAR IS $MYVAR"; } MYVAR=4 echo "MYVAR INITIALLY $MYVAR" mysub "This is an arg" echo "MYVAR STILL $MYVAR"
  • 43.
    Sourcing #!/bin/sh MYVAR=4 # The nextline sources the external script. . /path/to/mysub.sh echo "MYVAR INITIALLY $MYVAR" mysub "This is an arg" echo "MYVAR STILL $MYVAR"
  • 44.
    Regular Expressions • Powerfulmechanism for text processing • Uses: • search for a pattern within a block of text • replace bits of text • manipulate strings • Using external tools: grep, awk, sed, perl.
  • 45.
    Using Regex -Basics #replace occurrence of a to A echo "This is a test, this is only a test" | sed 's/a/A/g' #search for bar in the file foo.txt grep "bar" foo.txt cat foo.txt | grep "bar"
  • 46.
    Regex - Examples #Expression: /test/ grep 'test' poem.txt # Expression: /^Mary/ grep "^Mary" < poem.txt # Expression: /fox$/ grep "fox$" < poem.txt # Expression: s/Mary/Joe/ sed "s/Mary/Joe/" < poem.txt # Expression s/Mary/Joe/g sed "s/Mary/Joe/g" < poem.txt # Expression: /wa./ grep 'wa.' poem.txt # Expression: /Mary.*lamb/ grep "Mary.*lamb" poem.txt
  • 47.
    Regex - Modifiers •overall behavior of a regular expression can be tuned using a number of modifiers • /i - case insensitive • /g - replace globally • /x - ignore space globally
  • 48.
    AWK • Processing structureddata records containing text • AWK centers around dividing the input text into records
  • 49.
    AWK - Examples #!/bin/bash awk'{ print }' /etc/passwd awk '{ print $0 }' /etc/passwd #Multiple Fields awk -F":" '{ print $1 }' /etc/passwd # print out a list of all user accounts awk -F":" '{ print $1 $3 }' /etc/passwd # prints in separate lines awk -F":" '{ print $1 " " $3 }' /etc/passwd # concatenates $1 and $3 awk -F":" '{ print "username: " $1 "ttuid:" $3 }' /etc/passwd # labels awk -F":" '{print $1,$NF;}' /etc/passwd #Cool stuff awk -F":" 'BEGIN {print "UsernametAccountTypetShellType";} {print $1,"t",$5,"t",$NF;} END{print "End n--------------"; }' /etc/passwd awk -F":" '$3 >200' /etc/passwd #UID greater than 200 awk -F":" '$5 ~/System/' /etc/passwd #system users awk -F":" 'BEGIN { count=0;} $5 ~/System/ { count++; } END { print "Number of System Users = "count;}' /etc/passwd #count system users
  • 50.
    Demo • Demo UserManagement System
  • 51.
    Assignment • Build aContact Management System
  • 52.
    More !!! • http://tldp.org/LDP/abs/html/ •http://bash.cyberciti.biz/guide/Main_Page
  • 53.