CISC3130, Spring 2011Dr. Zhang
1
Bash Programming Review
Outline
2
Coding standard: how to get good grades in lab assignment
Review of standard input/output/errorHow to redirect them ? Pipeline
Review of bash scripting
Standard input/output/error
3
By default, link to keyboard and terminal window respectively
Can be redirected to files Can be redirected to pipeline
input can be redirected to reading end of a pipeoutput and error can be redirected to writing end of a
pipeWhen a bash script’s input/output/error is
redirected: E.g., headtail 3 10 .bash_profile > outputls –l | headtail 10 24 | wc –l input/output/error for every command in the script
are redirected !
Save standard input if necessary
4
cat > stdinput ## save standard input to a file
## so that we can use as many times as we want
wc –l stdinput grep PHONE stdinput
Redirection can be applied to loop
5
for i in `ls`do echo $i cat $idone > all_files
Similar for <, |
Outline
6
Coding standard: how to get good grades in lab assignment
Review of standard input/output/errorHow to redirect them ? Pipeline
Review of bash scripting
Bash scripting: general hints
7
Use echo command to trace (like cout, printf in C/C++)
Sometimes there are alternatives ways to do things, choose one and remember it: $(( …)), and $[ … ] [[ ]] for test
Be careful about typo, shell wont complain variable not declared/assigned … The price of freedom
A walk-through of basic bash scripting
Bash Scripting
8
VariablesEnvironment variable: affect behavior of shellUser defined variable: default type is string, can
declare it to be other typePositional variables: used to pass command line
arguments to bash scriptVariable assignment:
x=10 ## assign value 10 to variable x, no space around =
x=$x+1 ## add 1 to x’s value and assign to xPATH=$PATH:.:~/bin
To refer to a variable’s value, precede variable name with $
A script that display positional variable
9
echo All arguments: $*echo Number of arguments: $#echo Script name: $0echo argument 1: $1echo argument 2: $2
for arg in $*do echo argument $argdone
demo
10
[zhang@storm CISC3130]$ ./test 10 20 tmpfile
All arguments: 10 20 tmpfileNumber of arguments: 3Script name: ./testargument 1: 10argument 2: 20argument 10argument 20argument tmpfile
arithmetic operation
11
As variable’s default type is string, to perform arithmetic operation, use the following syntax $[$x+1] or $(($x+1))
For simpler syntax: declare variable to be numerical
declare –i x x=$x*10+2Above are for integer arithmetic operations
only ..
Command bc
12
An arbitrary precision calculator $ bc3.14159*10^2 314.15900130^216900sqrt(1000)31scale=4sqrt(1000)31.6277quit
An interactive calculator: * user input shown in normal font, * result shown in italics font
Internal variable scale: * control the number of decimal points after decimalpoint
bc in command line/script
13
To evaluate an expression, simply send it using pipe to bcecho "56.8 + 77.7" | bc
Write a script that read a Fahrenheit degree from standard input, convert it to Celsius degree (up to 2 digits after decimal point):
C=(F-32)*5/9Base conversion, from base 10 (decimal) to
base 16 (hexadecimal)echo "obase=16; ibase=10; 56" | bc
Test/Conditions
14
Any command or script, if it return 0, then test is successful
if rm tmp.txt then echo file tmp.txt has been deleted else echo fail to remove file tmp.txtFiUse ! to negate
Test Condition
15
Numerical comparisons-lt, -ne, -ge, …
String comparisionPattern matching: using double brackets
To test if first argument is “–” followed by a number:
if [[ "$1" == -[0-9]* ]]then
….
AND/OR list construct
16
Statement1 && statement2 && statement3 && …Each statement is executed; if it returns true, next
statement is executed…. Until finding first statement that returns false, or all statements return true
Note: the statement can be any command, such as echo (which always return true)
Statement1 || statement2 || statement3 || …Each statement is executed; if it returns false, next
statement is executed…. Until finding first statement that returns true (the construct then return true), or all statements return false (the construct then returns false).
Check password: maximum 5 tries
17
tries=0; limit=5while [ "$input" != "secret“ ] && [ $tries –lt $limit ]do echo "Enter your password" read input; tries=$(($tries+1))doneif [ $input= “secret” ]then
echo "welcome!“else
echo “retry limit (5) reached”fi
Infinite loop
18
while [ 1 ]do echo -n "Enter your password" read input if [ $input = "secret" ] then break else echo -n "Try again... " fidone
Useful command : select loop
19
#!/bin/bash
OPTIONS="Hello Quit“
select opt in $OPTIONS; do
if [ "$opt" = "Quit" ]
then
echo done
exit
elif [ "$opt" = "Hello" ]
then
echo Hello World
else
echo bad option
fi
done Recall pick command ?
Next:
20
More advanced bash scripting ArrayFunctionHere document
Array
21
Bash provides one-dimensional array variables. There is no maximum limit on the size of an
array, nor any requirement that members be indexed or assigned contiguously.
Arrays are indexed using integers and are zero-based.
Assign values to array: array=( one two three )files=( "/etc/passwd" "/etc/group" "/etc/hosts" )limits=( 10, 20, 26, 39, 48)
To Iterate Through Array Values
22
for i in "${arrayName[@]}“ do # do whatever on $i done -----------------------------------------------------------------------#!/bin/bash # declare an array called array and define 3 vales array=( one two three ) for i in "${array[@]}" do echo $i done
Functions
23
One can define functions to increase modularity and readability of shell scripts
More efficient than breaking large scripts into many smaller ones… Why ? foo() {
Echo “in function foo”
}echo “start script…”fooecho “end script …”
About functions
24
Need to be defined first, and then can be called
Parameter passing#! /bin/bashcalsum(){ echo `expr $1 + $2`}x=1;y=2;calsum $x $y
About functions
25
Result returningThrough setting a variableUse return commandUse echo command#! /bin/bashcalsum(){ echo `expr $1 + $2`}x=1;y=2;calsum $x $yz=`calsum $x $y`
calsum(){ z=`expr $1 + $2`}x=1;y=2;calsum $x $yecho z=$z
About functions
26
Local variable: its scope is within the function#! /bin/bashcalsumsqr(){
local sum=`expr $1 + $2`; echo `expr $sum * $sum`}x=1;y=2;calsum $x $yz=`calsum $x $y`
Here document
27
A special way to pass input to a command: here document, i.e., from the shell script itself#!/bin/bashcat <<!FUNKY!HelloThis is a hereDocument!FUNKY!
Here document starts with <<,followed by a special string which is repeated at the end of the document.
Note: the special string should be chosen to be rare one.
Here document:2
28
Benefits: store codes and data together, easier to maintain
Example: 411 scriptgrep “$*” << EndDial-a-joke 212-976-3838Dial-a-prayer 212-246-4200Dial santa 212-976-141End
~/Demo/Examples/411
A case study: bundle program (P 97)
29
Suppose a friend asks for copies of shell files in your bin directory
$ cd /user/you/bin$ for i in *.sh> do> echo ===== This is file $i
===============> cat $i> done | mail [email protected]
Pipeline & input/output redirection can be applied to for, while, until loop.
Make it better ?
30
Construct a mail message that could automatically unpack itself, i.e., to generate the original files packed inside
A shell script contains instructions for unpacking, and files themselvesUse here document mechanism
Generate this shell script using another script
Bundle script
31
#!/bin/bashecho ‘# To unbundle, bash this file’for ido echo “echo $i 1>&2” echo “cat >$i <<‘End of $i’” cat $i echo “End of $i”done
~/Examples/writescript bundle.sh
An example bundle file
32
Try it out: ./bundle.sh bundle.sh 411 > junk
Inside junk:
Inside junk file
33
#To unbundle, bash this fileecho bundle.sh 1>&2cat >bundle.sh <<'End of
bundle.sh'#!/bin/bashecho '#To unbundle, bash this
file'for i in $@ ## or for ido echo "echo $i 1>&2" echo "cat >$i <<'End of $i'" cat $i echo "End of $i"done
end of bundle.shecho 411 1>&2cat >411 <<'End of 411'grep "$*" <<Enddial-a-joke 212-976-3838dial-a-prayer 000000000dial santa 8900999today is `date`endend of 411