cisc3130, spring 2011 dr. zhang 1 bash programming review
TRANSCRIPT
![Page 1: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/1.jpg)
CISC3130, Spring 2011Dr. Zhang
1
Bash Programming Review
![Page 2: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/2.jpg)
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
![Page 3: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/3.jpg)
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 !
![Page 4: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/4.jpg)
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
![Page 5: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/5.jpg)
Redirection can be applied to loop
5
for i in `ls`do echo $i cat $idone > all_files
Similar for <, |
![Page 6: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/6.jpg)
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
![Page 7: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/7.jpg)
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
![Page 8: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/8.jpg)
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 $
![Page 9: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/9.jpg)
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
![Page 10: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/10.jpg)
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
![Page 11: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/11.jpg)
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 ..
![Page 12: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/12.jpg)
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
![Page 13: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/13.jpg)
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
![Page 14: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/14.jpg)
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
![Page 15: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/15.jpg)
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
….
![Page 16: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/16.jpg)
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).
![Page 17: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/17.jpg)
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
![Page 18: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/18.jpg)
Infinite loop
18
while [ 1 ]do echo -n "Enter your password" read input if [ $input = "secret" ] then break else echo -n "Try again... " fidone
![Page 19: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/19.jpg)
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 ?
![Page 20: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/20.jpg)
Next:
20
More advanced bash scripting ArrayFunctionHere document
![Page 21: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/21.jpg)
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)
![Page 22: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/22.jpg)
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
![Page 23: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/23.jpg)
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 …”
![Page 24: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/24.jpg)
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
![Page 25: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/25.jpg)
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
![Page 26: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/26.jpg)
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`
![Page 27: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/27.jpg)
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.
![Page 28: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/28.jpg)
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
![Page 29: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/29.jpg)
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.
![Page 30: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/30.jpg)
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
![Page 31: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/31.jpg)
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
![Page 32: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/32.jpg)
An example bundle file
32
Try it out: ./bundle.sh bundle.sh 411 > junk
Inside junk:
![Page 33: CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review](https://reader034.vdocuments.mx/reader034/viewer/2022051316/5697bf7b1a28abf838c83798/html5/thumbnails/33.jpg)
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