Introduction to Bash Programming, part 3 Ellen Zhang
Outline A review about what we learnt about bash Customize your bash environment list10 script Bash variable
Shell 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)
Bash 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 *, ?, [,], |, >, <, >> Quotations: single quote, double quote
Shell 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 $@: a list of all parameters, seperated by space Also called positional parameter
Our first shell script: list10 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
Bash 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
Specifying conditions Shell’s 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
Conditions 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
Conditions that one can “test”(cont’d) 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
Special bash scripts
Shell 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
Customize 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
To 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
Generate 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
Even 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.
To 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
Outline A review about what we learnt about BASH Customize your bash environment list10 script Bash variables
Script: list10 #!/bin/bash #echo $# #echo $1 #echo $0 ls -Rl $* | grep ^- | sort -k 5 -nr | head -10
Handling 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 20
Coming 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
Our 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
Bash 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” > greeting_$student write $student < greeting_$student done
Final list10 script if [[ "$1" == -[0-9]* ]] then for path in $* do if [ $path != $1 ] 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
Another example … Save student account name in a file, all.txt for student in `cat all.txt` do echo “Hello, $student” > greeting_$student grep $student student_rec.txt >>greeting_$student write $student < greeting_$student rm greeting_$student done exit How to avoid using the temporary file, greeting_$student ?
Outline A review about what we learnt about BASH Customize your bash environment list10 script Bash variables
SHELL 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 $x=1 # variable x is set to 1 Note: no spaces before and after “=“
SHELL Variables Access variable by preceding it with $ $ echo $x Need quote marks if there are spaces $ greeting=“Hello world” # need to quotation $ echo $greeting Hello world
Read variable value from input $ read timeofday Morning $ echo Good $timeofday! Good Morning! $ read greeting Good morning # don’t need to quote $ echo $greeting $ Good morning $ echo “$greeting” is \$greeting. What will be the output ?
Variable’s value type: string Variables values are stored as strings $ number=7+5 $ echo $number 7+5 $ x=2; y=3 $ z1=x+y; z2=$x+$y $ echo $z1 $z2 # What will be the output?
Arithmetic Evaluation To evaluate an arithmetic expression Use expr command $ x=1 $ x=`expr $x + 1` # increment x by 1 Or $ x=$(expr $x+1) Or $ x=$(( $x+1)) Other operations: Comparison: =, <, >, >=, <=, != Arithmetic: +, -, *, /, %
A bash based calculator First, let’s implement addition echo “calculate x+y” echo –n “x=“ read x echo –n “y=“ read y echo “x+y=“ `expr $x + $y `
Exercises: work on the following problems, finish them up at your own time and submit by Monday 8pm.
Compare 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 it’s the same as your secret password “secret”; if yes, print out “welcome!”; print out “Go away” if not.
Compare 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
Exercise 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 # What’s next ? Hint, case construct Evaluate multiple expressions, until user select to exit
Exercise4: 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.
Next two classes here document More advanced shell scripts Regular expression Sed, grep, awk, …