KEMBAR78
Module 03 Programming on Linux | PDF
1
Module-3 Basic Shell Scripting
Tushar B Kute
tushar.kute@gmail.com
http://snashlug.org
contact@snashlug.org
2
Why Shell?
 The commercial UNIX used Korn Shell
 For Linux, the Bash is the default
 Why Shell?
 For routing jobs, such as system administration,
without writing programs
 However, the shell script is not efficient,
therefore, can be used for prototyping the ideas
 For example,
% ls –al | more (better format of listing
directory)
% man bash | col –b | lpr (print man page of man)
3
What is Shell?
 Shell is the interface between end user and the Linux
system, similar to the commands in Windows
 Bash is installed as in /bin/sh
 Check the version
% /bin/sh --version
Kernel
Other
programs
X window
bash
csh
4
Shell as a Language
 We can write a script containing many shell commands
 Interactive Program:
 grep files with POSIX string and print it
% for file in *
> do
> if grep –l POSIX $file
> then
> more $file
 fi
 done
Posix
There is a file with POSIX in it
 ‘*’ is wildcard
% more `grep –l POSIX *`
% more $(grep –l POSIX *)
% more –l POSIX * | more
5
Writing a Script
 Use text editor to generate the “first” file
#!/bin/sh
# first
# this file looks for the files containing POSIX
# and print it
for file in *
do
if grep –q POSIX $file
then
echo $file
fi
done
exit 0
% /bin/sh first
% chmod +x first
%./first (make sure . is include in PATH parameter)
exit code, 0 means successful
6
Syntax
 Variables
 Conditions
 Control
 Lists
 Functions
 Shell Commands
 Result
 Document
7
Variables
 Variables needed to be declared, note it is
case-sensitive (e.g. foo, FOO, Foo)
 Add ‘$’ for storing values
% salutation=Hello
% echo $salutation
Hello
% salutation=7+5
% echo $salutation
7+5
% salutation=“yes sir”
% echo $salutation
yes sir
% read salutation
Hello!
% echo $salutation
Hello!
8
Quoting
 Edit a “vartest.sh” file
#!/bin/sh
myvar=“Hi there”
echo $myvar
echo “$myvar”
echo `$myvar`
echo $myvar
echo Enter some text
read myvar
echo ‘$myvar’ now equals $myvar
exit 0
Output
Hi there
Hi there
$myvar
$myvar
Enter some text
Hello world
$myvar now equals Hello world
9
Environment Variables
 $HOME home directory
 $PATH path
 $$ process id of the script
 $# number of input parameters
 $0 name of the script file
 $IFS separation character (white
space)
10
Condition
 test or ‘ [ ‘
if test –f fred.c
then
...
fi
If [ -f
fred.c ]
then
...
fi
if [ -f fred.c ];then
...
fi
expression1 –eq expression2
expression1 –ne expression2
expression1 –gt expression2
expression1 –ge expression2
expression1 -lt expression2
expression1 –le expression2
!expression
-d file if directory
-e file if exist
-f file if file
-g file if set-group-id
-r file if readable
-s file if size >0
-u file if set-user-id
-w file if writable
-x file if executableString1 = string2
String1 != string 2
-n string (if not empty string)
-z string (if empty string)
need space !
11
Control Structure
Syntax
if condition
then
statement
else
statement
fi
#!/bin/sh
echo “Is it morning? Please answer yes or no”
read timeofday
if [ $timeofday = “yes” ]; then
echo “Good morning”
else
echo “Good afternoon”
fi
exit 0
Is it morning? Please answer yes or no
yes
Good morning
12
Condition Structure
#!/bin/sh
echo “Is it morning? Please answer yes or no”
read timeofday
if [ $timeofday = “yes” ]; then
echo “Good morning”
elif [ $timeofday = “no” ]; then
echo “Good afternoon”
else
echo “Sorry, $timeofday not recongnized. Enter yes or no”
exit 1
fi
exit 0
13
Condition Structure
#!/bin/sh
echo “Is it morning? Please answer yes or no”
read timeofday
if [ “$timeofday” = “yes” ]; then
echo “Good morning”
elif [ $timeofday = “no” ]; then
echo “Good afternoon”
else
echo “Sorry, $timeofday not recongnized. Enter yes or no”
exit 1
fi
exit 0
If input “enter” still returns Good morning
14
Loop Structure
Syntax
for variable
do
statement
done
#!/bin/sh
for foo in bar fud 43
do
echo $foo
done
exit 0
bar
fud
43
How to output as bar fud 43?
Try change for foo in “bar fud 43”
This is to have space in variable
15
Loop Structure
 Use wildcard ‘*’
#!/bin/sh
for file in $(ls f*.sh); do
lpr $file
done
exit 0
Print all f*.sh files
16
Loop Structure
Syntax
while condition
do
statement
done
#!/bin/sh
for foo in 1 2 3 4 5 6 7 8 9 10
do
echo “here we go again”
done
exit 0
#!/bin/sh
foo = 1
while [ “$foo” –le 10 ]
do
echo “here we go again”
foo = $foo(($foo+1))
done
exit 0
Syntax
until condition
do
statement
done
Note: condition is
Reverse to while
How to re-write
previous sample?
17
Case Statement
Syntax
case variable in
pattern [ | pattern ] …) statement;;
pattern [ | pattern ] …) statement;;
…
esac
#!/bin/sh
echo “Is it morning? Please answer yes or no”
read timeofday
case “$timeofday” in
yes) echo “Good Morning”;;
y) echo “Good Morning”;;
no) echo “Good Afternoon”;;
n) echo “Good Afternoon”;;
* ) echo “Sorry, answer not recongnized”;;
esac
exit 0
18
Case Statement
 A much “cleaner” version
#!/bin/sh
echo “Is it morning? Please answer yes or no”
read timeofday
case “$timeofday” in
yes | y | Yes | YES ) echo “Good Morning”;;
n* | N* ) echo “Good Afternoon”;;
* ) echo “Sorry, answer not recongnized”;;
esac
exit 0
But this has a problem, if we enter ‘never’ which obeys n*
case and prints “Good Afternoon”
19
Command
 expr evaluate expressions
%x=`expr $x + 1` (Assign result value expr $x+1 to x)
Also can be written as
%x=$(expr $x + 1)
Expr1 | expr2 (or) expr1 != expr2
Expr1 & expr2 (and) expr1 + expr2
Expr1 = expr2 expr1 – expr2
Expr1 > expr2 expr1 * expr2
Expr1 >= expr2 expr1 / expr2
Expr1 < expr2 expr1 % expr2 (module)
Expr1 <= expr2
20
Command
 printf format and print data
 Escape sequence
  backslash
 a beep sound
 b backspace
 f form feed
 n newline
 r carriage return
 t tab
 v vertical tab
 Conversion specifier
 %d decimal
 %c character
 %s string
 %% print %
% printf “%sn” hello
Hello
% printf “%s %dt%s” “Hi
There” 15 people
Hi There 15 people
21
Command
 return return a value
 set set parameter variable
#!/bin/sh
echo the date is $(date)
set $(date)
echo The month is $2
exit 0
22
Pattern Matching
operator
! -not test reverse
-a -and test and
-o -or test or
action
-exec command execute command
-ok command confirm and exectute command
-print print
-ls ls –dils
Find files newer than while2 then print
% find . –newer while2 -print
23
Pattern Matching
Find files newer than while2 then print only files
% find . –newer while2 –type f –print
Find files either newer than while2, start with ‘_’
% find . ( -name “_*” –or –newer while2 ) –type f
–print
Find files newer than while2 then list files
% find . –newer while2 –type f –exec ls –l {} ;
24
Pattern Matching
 grep print lines matching a pattern
(General Regular Expression Parser)
grep [options] PATTERN [FILES]
option
-c print number of output context
-E Interpret PATTERN as an extended regular expression
-h Supress the prefixing of filenames
-i ignore case
-l surpress normal output
-v invert the sense of matching
% grep in words.txt
% grep –c in words.txt words2.txt
% grep –c –v in words.txt words2.txt
25
References
 Bash Beginners Guide
(http://tldp.org/LDP/Bash-Beginners-Guide/)

Module 03 Programming on Linux

  • 1.
    1 Module-3 Basic ShellScripting Tushar B Kute tushar.kute@gmail.com http://snashlug.org contact@snashlug.org
  • 2.
    2 Why Shell?  Thecommercial UNIX used Korn Shell  For Linux, the Bash is the default  Why Shell?  For routing jobs, such as system administration, without writing programs  However, the shell script is not efficient, therefore, can be used for prototyping the ideas  For example, % ls –al | more (better format of listing directory) % man bash | col –b | lpr (print man page of man)
  • 3.
    3 What is Shell? Shell is the interface between end user and the Linux system, similar to the commands in Windows  Bash is installed as in /bin/sh  Check the version % /bin/sh --version Kernel Other programs X window bash csh
  • 4.
    4 Shell as aLanguage  We can write a script containing many shell commands  Interactive Program:  grep files with POSIX string and print it % for file in * > do > if grep –l POSIX $file > then > more $file  fi  done Posix There is a file with POSIX in it  ‘*’ is wildcard % more `grep –l POSIX *` % more $(grep –l POSIX *) % more –l POSIX * | more
  • 5.
    5 Writing a Script Use text editor to generate the “first” file #!/bin/sh # first # this file looks for the files containing POSIX # and print it for file in * do if grep –q POSIX $file then echo $file fi done exit 0 % /bin/sh first % chmod +x first %./first (make sure . is include in PATH parameter) exit code, 0 means successful
  • 6.
    6 Syntax  Variables  Conditions Control  Lists  Functions  Shell Commands  Result  Document
  • 7.
    7 Variables  Variables neededto be declared, note it is case-sensitive (e.g. foo, FOO, Foo)  Add ‘$’ for storing values % salutation=Hello % echo $salutation Hello % salutation=7+5 % echo $salutation 7+5 % salutation=“yes sir” % echo $salutation yes sir % read salutation Hello! % echo $salutation Hello!
  • 8.
    8 Quoting  Edit a“vartest.sh” file #!/bin/sh myvar=“Hi there” echo $myvar echo “$myvar” echo `$myvar` echo $myvar echo Enter some text read myvar echo ‘$myvar’ now equals $myvar exit 0 Output Hi there Hi there $myvar $myvar Enter some text Hello world $myvar now equals Hello world
  • 9.
    9 Environment Variables  $HOMEhome directory  $PATH path  $$ process id of the script  $# number of input parameters  $0 name of the script file  $IFS separation character (white space)
  • 10.
    10 Condition  test or‘ [ ‘ if test –f fred.c then ... fi If [ -f fred.c ] then ... fi if [ -f fred.c ];then ... fi expression1 –eq expression2 expression1 –ne expression2 expression1 –gt expression2 expression1 –ge expression2 expression1 -lt expression2 expression1 –le expression2 !expression -d file if directory -e file if exist -f file if file -g file if set-group-id -r file if readable -s file if size >0 -u file if set-user-id -w file if writable -x file if executableString1 = string2 String1 != string 2 -n string (if not empty string) -z string (if empty string) need space !
  • 11.
    11 Control Structure Syntax if condition then statement else statement fi #!/bin/sh echo“Is it morning? Please answer yes or no” read timeofday if [ $timeofday = “yes” ]; then echo “Good morning” else echo “Good afternoon” fi exit 0 Is it morning? Please answer yes or no yes Good morning
  • 12.
    12 Condition Structure #!/bin/sh echo “Isit morning? Please answer yes or no” read timeofday if [ $timeofday = “yes” ]; then echo “Good morning” elif [ $timeofday = “no” ]; then echo “Good afternoon” else echo “Sorry, $timeofday not recongnized. Enter yes or no” exit 1 fi exit 0
  • 13.
    13 Condition Structure #!/bin/sh echo “Isit morning? Please answer yes or no” read timeofday if [ “$timeofday” = “yes” ]; then echo “Good morning” elif [ $timeofday = “no” ]; then echo “Good afternoon” else echo “Sorry, $timeofday not recongnized. Enter yes or no” exit 1 fi exit 0 If input “enter” still returns Good morning
  • 14.
    14 Loop Structure Syntax for variable do statement done #!/bin/sh forfoo in bar fud 43 do echo $foo done exit 0 bar fud 43 How to output as bar fud 43? Try change for foo in “bar fud 43” This is to have space in variable
  • 15.
    15 Loop Structure  Usewildcard ‘*’ #!/bin/sh for file in $(ls f*.sh); do lpr $file done exit 0 Print all f*.sh files
  • 16.
    16 Loop Structure Syntax while condition do statement done #!/bin/sh forfoo in 1 2 3 4 5 6 7 8 9 10 do echo “here we go again” done exit 0 #!/bin/sh foo = 1 while [ “$foo” –le 10 ] do echo “here we go again” foo = $foo(($foo+1)) done exit 0 Syntax until condition do statement done Note: condition is Reverse to while How to re-write previous sample?
  • 17.
    17 Case Statement Syntax case variablein pattern [ | pattern ] …) statement;; pattern [ | pattern ] …) statement;; … esac #!/bin/sh echo “Is it morning? Please answer yes or no” read timeofday case “$timeofday” in yes) echo “Good Morning”;; y) echo “Good Morning”;; no) echo “Good Afternoon”;; n) echo “Good Afternoon”;; * ) echo “Sorry, answer not recongnized”;; esac exit 0
  • 18.
    18 Case Statement  Amuch “cleaner” version #!/bin/sh echo “Is it morning? Please answer yes or no” read timeofday case “$timeofday” in yes | y | Yes | YES ) echo “Good Morning”;; n* | N* ) echo “Good Afternoon”;; * ) echo “Sorry, answer not recongnized”;; esac exit 0 But this has a problem, if we enter ‘never’ which obeys n* case and prints “Good Afternoon”
  • 19.
    19 Command  expr evaluateexpressions %x=`expr $x + 1` (Assign result value expr $x+1 to x) Also can be written as %x=$(expr $x + 1) Expr1 | expr2 (or) expr1 != expr2 Expr1 & expr2 (and) expr1 + expr2 Expr1 = expr2 expr1 – expr2 Expr1 > expr2 expr1 * expr2 Expr1 >= expr2 expr1 / expr2 Expr1 < expr2 expr1 % expr2 (module) Expr1 <= expr2
  • 20.
    20 Command  printf formatand print data  Escape sequence  backslash  a beep sound  b backspace  f form feed  n newline  r carriage return  t tab  v vertical tab  Conversion specifier  %d decimal  %c character  %s string  %% print % % printf “%sn” hello Hello % printf “%s %dt%s” “Hi There” 15 people Hi There 15 people
  • 21.
    21 Command  return returna value  set set parameter variable #!/bin/sh echo the date is $(date) set $(date) echo The month is $2 exit 0
  • 22.
    22 Pattern Matching operator ! -nottest reverse -a -and test and -o -or test or action -exec command execute command -ok command confirm and exectute command -print print -ls ls –dils Find files newer than while2 then print % find . –newer while2 -print
  • 23.
    23 Pattern Matching Find filesnewer than while2 then print only files % find . –newer while2 –type f –print Find files either newer than while2, start with ‘_’ % find . ( -name “_*” –or –newer while2 ) –type f –print Find files newer than while2 then list files % find . –newer while2 –type f –exec ls –l {} ;
  • 24.
    24 Pattern Matching  grepprint lines matching a pattern (General Regular Expression Parser) grep [options] PATTERN [FILES] option -c print number of output context -E Interpret PATTERN as an extended regular expression -h Supress the prefixing of filenames -i ignore case -l surpress normal output -v invert the sense of matching % grep in words.txt % grep –c in words.txt words2.txt % grep –c –v in words.txt words2.txt
  • 25.
    25 References  Bash BeginnersGuide (http://tldp.org/LDP/Bash-Beginners-Guide/)