Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Bash Programming, part 3

Similar presentations


Presentation on theme: "Introduction to Bash Programming, part 3"— Presentation transcript:

1 Introduction to Bash Programming, part 3
Ellen Zhang

2 Outline A review about what we learnt about bash
Customize your bash environment list10 script Bash variable

3 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)

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 Special bash scripts

12 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

13 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

14 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

15 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

16 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.

17 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

18 Outline A review about what we learnt about BASH
Customize your bash environment list10 script Bash variables

19 Script: list10 #!/bin/bash #echo $# #echo $1 #echo $0 ls -Rl $* | grep ^- | sort -k 5 -nr | head -10

20 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 ~zhang/documents If the number argument (starting with -) is not given, then list 10 largest files 20 20

21 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

22 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

23 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

24 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

25 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 ?

26 Outline A review about what we learnt about BASH
Customize your bash environment list10 script Bash variables

27 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 “=“

28 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

29 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 ?

30 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 $z # What will be the output?

31 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: +, -, *, /, %

32 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 `

33 Exercises: work on the following problems, finish them up at your own time and submit by Monday 8pm.

34 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.

35 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

36 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

37 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.

38 Next two classes here document More advanced shell scripts
Regular expression Sed, grep, awk, …


Download ppt "Introduction to Bash Programming, part 3"

Similar presentations


Ads by Google