Title: Introduction to Bash Programming, part 3
1Introduction to Bash Programming, part 3
2Outline
- A review about what we learnt about bash
- Customize your bash environment
- list10 script
- Bash variable
3Shell command line
- Shell command line syntax
- ls R l
- Command name and options separated by space
- Command ends with newline, and
Command name
argument
Options (arguments)
4Bash special characters
- Globbing, filename expansion
- ls , rm , etc.
- Shell expands filename patterns or templates
containing special characters - Can be turned off set f, shopt
- Shell special characters
- , ?, ,, , gt, lt, gtgt
- Quotations single quote, double quote
5Shell parameter variables
- If your script is invoked with parameters, shell
sets parameter variables - the number of parameters
- 0 the command/script name
- 1 the first parameter given to the script
- 2 the second parameter
- a list of all parameters, seperated by first
char in IFS - _at_ a list of all parameters, seperated by space
- Also called positional parameter
6Our first shell script list10
- Our first shell script
- ls -Rl grep - sort -k 5 -nr head -10
- To make it accept path_name
- ls -Rl 1 grep - sort -k 5 -nr head -10
- To make it accept multiple path names
- ls -Rl grep - sort -k 5 -nr head -10
7Bash programming Control Structures
- Similar to other programming language
- Differs in syntax
- 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
8Specifying conditions
- Shells boolean check command test, or
- if test f list10 if -f list10
- then
- ..
- fi
Important note the spaces before and after
, To put if, then in one line if -f list10
then
9Conditions that one can test
- 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
10Conditions that one can test(contd)
- 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
11Special bash scripts
12Shell startup file
- Profile files executed for login shell
- /etc/profile system wide default, setting
environment for all shell. - HOME/.bash_profile user-specific bash
environment default settings - Initialization files executed for login and
interactive shell - /etc/bashrc system wide function and aliases for
bash - HOME/.bashrc user-specific initialization files
13Customize your bash environment
- Edit the /.bash_profile file
- Print a greeting message with your account name,
home directory, date - echo n Hello
- who am i
- echo n your home is
- pwd
- Echo n today is
- date
14To generate nicer output
- Command substitution substitute output of a
command (sequence) into another context - Syntax enclose using backquote, or ()
- As argument for another command
- rm ls .o
- To set a variable
- time1(date) echo times1
- To be used in for construct
- for file in ls do
-
- done
15Generate adapted welcome msg
- Examples
- echo Welcome, who am i! Your home is pwd
- echo The time is (date)
- You can also use
- echo Welcome, USER
16Even nicer output
- set command, a shell builtin command
- display current variables, set
- set shell options, set f, set n ..
- set position parameters,
- set Hello world echo 1, 2
- Combine command substitution, set
- set who am i
- echo Welcome, 1! You logged in from 5.
17To test your settings
- Reloggin
- Type bash to create an another sub-shell
- Current shell is not altered
- Run a script from current shell to change
settings of current shell - source .bashrc , or . .bashrc
18Outline
- A review about what we learnt about BASH
- Customize your bash environment
- list10 script
- Bash variables
19Script list10
- !/bin/bash
- echo
- echo 1
- echo 0
- ls -Rl grep - sort -k 5 -nr head -10
20Handling command line option
- To accept an optional arguments
- list_n largest_num path1 path2 path3
- display 20 largest files under a directory
- list_n -20 zhang/documents
- If the number argument (starting with -) is not
given, then list 10 largest files
20
21Coming back to list10
- To test if first argument is followed by a
number - if "1" -0-9
- then
- echo "list10 with number argument "
- else
- echo "list10 without number argument"
- fi
- Double brackets allow for pattern matching
22Our list10 script
- if "1" -0-9
- then
- list10 with number argument
- how to implement this ?
- else
- list10 withoutnot number argument
- all arguments are paths
- ls Rl grep - sort -k 5 nr head
-10 - fi
23Bash Programming Loop Structure
- for construct to loop through a range of values,
which can be any set of strings - for student in jack, john, alice
- do
- echo Hello, student gt greeting_student
- write student lt greeting_student
- done
24Final list10 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
25Another example
- Save student account name in a file, all.txt
- for student in cat all.txt
- do
- echo Hello, student gt greeting_student
- grep student student_rec.txt
gtgtgreeting_student - write student lt greeting_student
- rm greeting_student
- done
- exit
How to avoid using the temporary file,
greeting_student ?
26Outline
- A review about what we learnt about BASH
- Customize your bash environment
- list10 script
- Bash variables
27SHELL Variables
- Different types of variables, set
- Environment variables HOME, PATH, PS1, PS2
- Parameter variables 1, ,
- User defined variables student,
- Declare variables by using them, e.g.,
- for path in
- Or
- x1 variable x is set to 1
- Note no spaces before and after
28SHELL Variables
- Access variable by preceding it with
- echo x
- Need quote marks if there are spaces
- greetingHello world need to quotation
- echo greeting
- Hello world
-
29Read variable value from input
- read timeofday
- Morning
- echo Good timeofday!
- Good Morning!
- read greeting
- Good morning dont need to quote
- echo greeting
- Good morning
- echo greeting is \greeting.
What will be the output ?
30Variables value type string
- Variables values are stored as strings
- number75
- echo number
- 75
- x2 y3
- z1xy z2xy
- echo z1 z2 What will be the output?
31Arithmetic Evaluation
- To evaluate an arithmetic expression
- Use expr command
- x1
- xexpr x 1 increment x by 1
- Or x(expr x1)
- Or x(( x1))
- Other operations
- Comparison , lt, gt, gt, lt, !
- Arithmetic , -, , /,
32A bash based calculator
- First, lets implement addition
- echo calculate xy
- echo n x
- read x
- echo n y
- read y
- echo xy expr x y
33Exerciseswork on the following problems,
finish them up at your own time and submit by
Monday 8pm.
34Compare string variable
- Remember the conditionals for testing strings
, !, -n , -z - Exercise 1
- Write a script that read from standard input a
string, and check if its the same as your secret
password secret if yes, print out welcome!
print out Go away if not.
35Compare string variable
- Exercise 2
- Use while construct to rewrite the script, so
that user can keep on trying until getting it
right. - while condition do
- statements
- done
36Exercise 3 A Simple Calculator
- To perform addition, subtraction,
- echo "evaluate binary operation on x,y"
- echo -n "x"
- read x
- echo -n "op (, -, , /, )"
- read op
- echo -n "y"
- read y
- Whats next ? Hint, case construct
- Evaluate multiple expressions, until user select
to exit
37Exercise4 loop10
- Write a script that wake up every 5 seconds,
print out a message Get up and do some
exercise. - Extend the above script so that it loops for 10
times.
38Next two classes
- here document
- More advanced shell scripts
- Regular expression
- Sed, grep, awk,