Title: Introduction to Bash Programming, part 4
1Introduction to Bash Programming, part 4
2Use Arsc
- Type in user name and password once, clear
Terminal button to open PuTTy window - Open one terminal first
- I will write a script to that terminal
- Open another terminal
3Write the script to all students
- !/bin/bash
- Usage write given file to all students listed
in a file - write cat /CSRU3130/submissions/student_accounts
.txt lt 1
Example at /Demo/Examples/loop.sh
4Outline
- Review
- Execution of shell script
- Bash variables
- Conditions
- Control Structure
- Arithmetic, working with numbers
- Function
- Here document
5Execution of shell script
- !/bin/bash
- an endless loop, to illustrate how script is
run. - cd oddvariable1
- echo "PWD is set to PWD"
- echo "oddvariableoddvariable
- echo -n press any key to continue
- read x
- while 1 Endless loop.
- do
- echo endless loop, type Ctrl-c to abort. . ."
- done
- )
- Script will run forever,
- or at least until terminated by a Ctl-C.
6Execution of shell script
- bash ./myscript or ./myscript
- a subshell is started to interpret and run script
- Type command ps -ef grep USER grep bash
- to check your own bash processes
- Current shells environment is not altered,
including current directory, variables - pwd echo PWD echo oddvariable
7Execution of shell script
- Run a script from current shell source myscript,
or . myscript - Any changes to environment is made at current
shell - Try running myscript using above commands, use
- ps ef grep bash grep USER to check of
bash processes - Notice current directory is changed, and
oddvariable remains set
8SHELL Variables (1)
- Different types of variables, set
- Environment variables HOME, PATH, PS1, PS2
- Parameter variables ,0, 1, ,_at_
- User defined variables student,
- Declare variables by using them, e.g.,
- x1 variable x is set to 1
- Note no spaces before and after
9Shell variables (2)
- Do not need to declare a variable
- All variables are string (stored as strings)
- Set a variables value
- X3 messagehello world
- To read from standard input a string, and set it
to a variable - echo Please input value for x echo n x
- read x
10Shell variables (3)
- Refer to a variables value add a before
variable name - echo The value of x is x
- If x is not defined, x returns a empty string
- Unset a variable, unset command
- unset x
11Shell variables (4)
- Shell variables created in a shell is not
available in subshells (including interactive
shell and shell running scripts) - x123
- bash
- echo x
- To make a variable available to all subshells
- export xabc strHello
- export PATHPATH/bin
12Outline
- Review
- Execution of shell script
- Bash variables
- Conditions
- Control Structure
- Exercises problem from last class
- Some useful bash built-in commands
- export, break, , , etc.
- Here document
13Bash programming Control Structures
- Control structures in bash
- if then else fi
- if then elif else fi
- for in do done
- while do done
- until do done
- case in esac
14If/then/else/fi Check password input
- echo n "Enter your password "
- read input
- if input "secret"
- then
- echo "welcomed --
- echo to the secret world!
- else
- echo "go away!"
- fi
Please pay attention to the style. use
indention use comments
15Specifying conditions
- We use test command, or to specify conditions
- if test input secret or
input "secret" - then
- echo "welcomed --
- echo to the secret world!
- else
- echo "go away!"
- fi
Important note the spaces before and after
, To put if, then in one line if -f list10
then
16Check password script2
- !/bin/bash
- while "input" ! "secret"
- do
- echo "Enter your password"
- read input
- done
- echo "welcome!"
What will happen if no quotation on input ?
17Alternative way
- while 1
- do
- echo -n "Enter your password"
- read input
- if input "secret"
- then
- break
- else
- echo -n "Try again... "
- fi
- done
18Check password script2 use until
- !/bin/bash
- until "input "secret"
- do
- echo -n"Enter your password "
- read input
- done
- echo "welcome!"
19Conditions that one can test
- String comparison
- string1 string2 strings are equal
- -n string string is not null
- -z string string is null
- Arithmetic comparison
- exp1 eq exp2 true if two expressions are equal
- exp1 ne exp2 true if two expressions are not
equal - Others gt, -ge, -lt, -le
- ! exp1 true if exp1 if false
20String pattern matching
- Double brackets allow for pattern matching
- To test if first argument is followed by a
number - if "1" -0-9
- then
- .
- To check if input is abc followed by numbers
- if "input" abc0-9
- then
- .
21test about files
- File conditionals
- -d file true if the file is a directory
- -e file true if the file exists
- -f file true if the file is a regular file
- -r file true if the file is readable -x,-w
- -s file true if the file has nonzero size
- more . Read bash tutorial manuals
22list10 script
- if "1" -0-9
- then
- for path in
- do
- if path ! 1
- then
- path_list"path_list
path" - fi
- done
- ls -Rl path_list sort -k 5 -nr head
1 - else
- ls -Rl grep - sort -k 5 -nr
head -10 - fi
23Outline
- Review
- Execution of shell script
- Bash variables
- Conditions
- Control Structure
- Arithmetic, working with numbers
- Some useful bash built-in commands
- export, break, , , etc.
- Here document
24Variables are strings
- Variables values are stored as strings
- number75
- echo number
- 75
- x2 y3
- z1xy z2xy
- echo z1 z2 What will be the output?
- xy 23
25Arithmetic Evaluation
- To evaluate an arithmetic expression use expr
command - x1
- expr x 100 Need space before/after
- 101
- xexpr x 1 need space before/after
- x(expr x 1) need space before/after
- x((x1)) dont need space
- Other operations supported , -, , /,
26Exercise4 loop10
- count1
- limit10
- while count -lt limit
- do
- sleep 5 echo Get up and do some exercise
- countexpr count 1
- done
27Another solution
- for (( i0ilt10i ))
- do
- sleep 5 echo "Get up and get some
excercise!" - done
Courtesy of Chris Belsole
28Exercise 3 A Simple Calculator
- echo "evaluate binary operation on x,y"
- echo -n "x"
- read x
- echo -n "op (, -, , /, )"
- read op
- echo -n "y"
- read y
29First attempt
- if op ""
- then
- echo ((xy))
- elif op "-"
- then
- echo ((x-y))
- elif op "/"
- then
- echo ((x/y))
- elif op ""
- then
- echo ((xy))
- else
- echo ((xy))
- fi
30Exercise 3 A Simple Calculator
variable
- case op in
- ) echo "xyexpr x y"
- -) echo "x-yexpr x - y"
- "") echo "xyexpr x "" y"
- /) echo "x/yexpr x / y"
- ) echo "xyexpr x y"
- ) echo "Not recognized operation"
- esac
pattern
- On first matching pattern, execute corresponding
statement. - Last (wildcards) match the all the rest input
what happen if put it first? - Note that we need to quote on 3rd line.
31Putting multiple patterns together
- echo is it morning ? (yes or no)
- read timeofday
- case timeofday in
- Yes y yes ) echo Good morning
- N n ) echo Good afternoon
- ) echo Sorry, invalid answer
- esac
The means or.
32Another solution
- while 1
- do
-
- ans(( xopy ))
- echo "xopyans"
- echo "Are you finished (y/n)"
- read choice
- if choice y
- then
- exit
- fi
- echo ""
- Done
- Courtesy of Chris Belsole
33Outline
- Review
- Execution of shell script
- Bash variables
- Conditions
- Control Structure
- Arithmetic, working with numbers
- Something new
- Lists, break, , , etc.
- Here document
34Check password maximum 5 tries
- tries0 limit5
- while "input" ! "secret tries lt
limit - do
- echo "Enter your password"
- read input tries((tries1))
- done
- if input secret
- then
- echo "welcome!
- else
- echo retry limit (5) reached
- fi
35AND list construct
- We used to connect two test commands together
- Generally,
- 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)
36OR list construct
- Generally,
- 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). - Note the statement can be any command, such as
echo (which always return true)
37Functions
- 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
- foo
- echo end script
38About functions
- Need to be defined first, and then can be called
- Parameter passing
- ! /bin/bash
- calsum()
- echo expr 1 2
-
- x1y2
- calsum x y
39About functions
- Result returning
- Through setting a variable
- Use return command
- Use echo command
- ! /bin/bash
- calsum()
- echo expr 1 2
-
- x1y2
- calsum x y
- zcalsum x y
calsum() zexpr 1 2 x1y2 cals
um x y echo zz
40About functions
- Local variable its scope is within the function
- ! /bin/bash
- calsumsqr()
- local sumexpr 1 2
- echo expr sum sum
-
- x1y2
- calsum x y
- zcalsum x y
41Here document
- A special way to pass input to a command here
document, i.e., from the shell script itself - !/bin/bash
- cat ltlt!FUNKY!
- Hello
- This is a here
- Document
- !FUNKY!
Here document starts with ltlt, 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.
42Here document2
- Benefits store codes and data together, easier
to maintain - Example 411 script
- grep ltlt End
- Dial-a-joke 212-976-3838
- Dial-a-prayer 212-246-4200
- Dial santa 212-976-141
- End
/Demo/Examples/411
43A case study bundle program (P 97)
- Suppose a friend asks for copies of shell files
in your bin directory - cd /user/you/bin
- for i in .sh
- gt do
- gt echo This is file i
- gt cat i
- gt done mail yourfriend_at_hotmail.com
Pipeline input/output redirection can be
applied to a for, while, until loop.
44Make it better ?
- 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 themselves - Use here document mechanism
- Generate this shell script using another script
45Bundle script
- !/bin/bash
- echo To unbundle, bash this file
- for i
- do
- echo echo i 1gt2
- echo cat gti ltltEnd of i
- cat i
- echo End of i
- done
/Examples/writescript bundle.sh
46An example bundle file
- Try it out
- ./bundle.sh bundle.sh 411 gt junk
- Inside junk
47Inside junk file
end of bundle.sh echo 411 1gt2 cat gt411 ltlt'End of
411' grep "" ltltEnd dial-a-joke
212-976-3838 dial-a-prayer 000000000 dial santa
8900999 today is date end end of 411
- To unbundle, bash this file
- echo bundle.sh 1gt2
- cat gtbundle.sh ltlt'End of bundle.sh'
- !/bin/bash
- echo 'To unbundle, bash this file'
- for i in _at_ or for i
- do
- echo "echo i 1gt2"
- echo "cat gti ltlt'End of i'"
- cat i
- echo "End of i"
- done
48Summary
- We so far covered basic features of shell
variables, functions, conditions and constructs. - Next class
- Some powerful commands for searching, stream
processing text files grep, sed, awk - Regular expressions