Intro. to Unix Environment Xiaolan Zhang Spring 2015 1 Outline Shell: interactive mode and batch mode Unix commands: syntax and some basic useful commands File systems and commands, emacs or vi Users and groups Environment variables and configuration files Standard input/output, redirection, pipeline grep command basics Shell scripting: control structures (branch and loop) 2 A Quick Start Terminal PuTTy, a telnet/ssh client a free and open source terminal emulator application a window in your desktop that works like old time terminal Enter your ID and password 3 Your first encounter: shell • Shell: a special-purpose program, command line interpreter, read commands typed by a user and execute programs in response to entered commands • Many different shells: Bourne Shell (sh): oldest, • • • • • 4 I/O redirection, pipelines, filename generation (globbing), variables, environment variables, command substitution, background command execution, function C Shell (csh): syntax of flow-control similar to C, command history, command-line editing, job control, aliases Korn Shell (ksh): “csh”, compatible with sh Bourne again Shell (bash): GNU’s reimplementation of Bourne shell, supports features added in C shell, and Korn shell Check/Change Login Shell To check the shell you are using echo $SHELL echo $0 login shell: default shell for a user, specified in /etc/passwd To change your login shell, use command chsh 5 Shell: interactive mode • A shell session (a dialog between user and shell) 1. 2. 3. 4. 6 Displays a prompt character, and waits for user to type in a command line Prompt depends on shell: sh, ksh, bash: $ csh: % tcsh: > May be customized (with current directory, host, ...) On input of a command line, shell extracts command name and arguments, searches for the program, and runs it. When program finishes, shell continues to step 1 The loop continues until user types “exit” or “ctrl-d” to end UNIX command line • Command name and arguments: command [ [ - ] option (s) ] [ option argument (s) ] [ command argument (s) ] – Command arguments are file or path names • cp prog1.cpp prog1.cpp.bak – Options: used to control behavior of the command • head -20 lab1.cpp • wc –w lab2.cpp // count how many words • Some options come with option argument – – 7 sort –k 1 data.txt // use the first column of data.txt as the key to sort The most important command !!! man ls • man: displaying online manuals – Press q to quit, space to scroll down, arrow keys to roll up/down 8 Correcting type mistakes • Shell starts to parse command line only when Enter key is pressed • Delete the whole line (line-kill): C-u • Erase a character: C-h or backspace key • Many more fancy functionalities: – Auto-completion: press Tab key to ask shell to auto-complete command, or path name – History (repeat command): use arrow (up and down) keys to navigate past commands –… 9 Shell: batch/scripting mode In batch mode, shell can interpret and execute shell scripts #!/bin/bash # count number of files/directories in curr. directory ls –l | wc –l Shell constructs: variables, Loop and conditional statements I/O commands (read from keyboard, write to terminal) Function, arrays … 10 Outline Shell: interactive mode and batch mode Unix commands: syntax and some basic useful commands File systems and commands, emacs or vi Users and groups Environment variables and configuration files Standard input/output, redirection, pipeline grep command basics Shell scripting: control structures (branch and loop) 11 Unix File Files: store information a sequence of 0 or more bytes containing arbitrary information What's in a filename? Case matters, no longer limited to 14 chars Special characters such as -, spaces are allowed, but you shouldn’t use them in filename Can you think of the reason ? Dot files are hidden, i.e., normally not listed by command ls To display all files, including hidden files, use ls -a 12 What’s in a file ? So far, we learnt that files are organized in a hierarchical directory structure Each file has a name, resides under a directory, is associated with some admin info (permission, owner) Contents of file: Text (ASCII) file (such as your C/C++ source code) Executable file (commands): e.g., ls, vi, … A link to other files, … Virtual file: /proc: a pseudo-filesystem, contains user-accessible objects on runtime state of kernel and executing processes To check the type of file: “file <filename>” 13 File Viewing Commands • cat: concatenate files and display on standard output (i.e., the terminal window) – cat [option] … [file] … [ ] means the argument is optional … means there can be multiple – cat proj1.cpp arguments of this type – cat proj1.cpp proj2.cpp – cat –n proj1.cpp // display the file with line # • More: file perusal filter (i.e., displaying file one screen at a time) – more proj1.cpp • head, tail: display the beginning or ending lines of a file – tail -f output // display the file, append more lines as the file grows 14 File manipulation commands • rm: remove one or multiple files or directories – rm [option] … FILE … – rm temp – rm temp1 temp2 • Wildcards (metacharacter) can be used in command line – Letter * matches with any string • rm *.o: remove all .o files – ?: match any one character – [abc]: match with letter a or b or c • rm –r: remove directories and their sub-dirs recursively • rm –i : confirm with user before removing files 15 File manipulation commands (2) cp: copy file or directory cp [OPTION] SOURCE DESTINATION To make a backup copy of your program before dramatic change cp proj1.cpp proj1.cpp.bak To make a backup copy of a whole directory cp –r lab1_dir lab1_dir_backup -R, -r, --recursive: copy directories recursively 16 File manipulation commands (3) mv: move (rename) files/directories mv [OPTION] SOURCE DEST Rename SOURCE to DEST mv proj1.cpp lab1.cpp mv [OPTION]… SOURCE… DIRECTORY Move SOURCE to DIRECTORY mv lab1.cpp lab2.cpp CISC3130 17 Hierarchical file system • Directory: a file that can hold other files Advantages of hierarchical file system: • Files can have same names, as long as they are under different directories • Easier for protection • Organized files dev cdrom tty24 / (root) bin home staff zhang 18 etc lib group passwd Absolute pathname, path / (root) dev cdrom tty24 bin home staff etc lib passwd zhang Pathname of a file/directory: location of file/directory in the file 19 system How do you tell other where your prog. Is located ? Absolute pathname: path name specified relative to root, i.e., starting with the root (/) e.g., /home/staff/zhang What’s the absolute pathname for the “passwd” file? Home directory Every user has a home directory created for him/her When you log in, you are in your home directory In home directory, a user usually has permission to create files/directories, remove files .. ~ to refer to current user’s home directory ~username to refer to username’s home directory 20 Current directory & Relative Pathname Tiring to specify absolute pathname each time To make life easier: working directory User can move around the file system, shell remembers where the user is (i.e., current directory) To check your current directory, use command: pwd 21 Getting around in the file system • To create a subdirectory: – mkdir [option]… directory… – cd – mkdir CISC3130 – cd CISC3130 – mkdir lab1 • To remove a directory: – rmdir [option]… directory… – Report failure if directory is not empty • Can use rm –rf to remove non-empty directory 22 Command for change current directory (move around) cd [directory] [zhang@storm Work]$ cd [zhang@storm ~]$ pwd /home/staff/zhang [zhang@storm ~]$ cd Work [zhang@storm Work]$ pwd /home/staff/zhang/Work [zhang@storm Work]$ cd .. [zhang@storm ~]$ pwd /home/staff/zhang [zhang@storm ~]$ 23 23 Relative pathname Absolute pathname: specified relative to root Relative pathname: specified relative to current directory . (current directory), .. (parent directory, one level up) If current directory is at /home/staff/zhang, what is the relative pathname of the file passwd? ../../../etc/passwd: go one level up, go one level up, go one level up, go to etc, passwd is there / (root) dev cdrom tty24 bin home staff 24 zhang etc lib passwd Relative pathname For all commands that take file/directory name as arguments, you can use pathnames of the file/directory Example: cd /home/staff/zhang/public_html pico CISC3130/index.html cd .. (go up one level to parent directory) cp ../prog2.cpp prog2.cpp 25 Getting around in the file system ls: list directory contents ls [OPTION] … [FILE] ls: list files/directories under current directory ls –l: long listing, [zhang@storm CISC1600]$ ls -l total 59180 -rw-r--r-- 1 zhang staff 509952 Sep 7 13:02 3_types.ppt -rw-r--r-- 1 zhang staff 593408 Sep 14 23:38 4_computation.ppt -rw-r--r-- 1 zhang staff 1297 Sep 2 12:18 account.html -rw-r--r-- 1 zhang staff 3304448 Nov 7 18:24 ArrayVector1.ppt drwxr-xr-x 2 zhang staff 4096 Dec 8 22:36 Codes 26 Long listing To get more information about each file [zhang@storm Demo]$ ls -al total 32 Total disc space taken in blocks (1024 Byte) drwxr-xr-x 5 zhang staff 4096 2008-01-16 16:01 . drwxr-xr-x 41 zhang staff 4096 2008-01-16 16:01 .. drwxr-xr-x 2 zhang staff 4096 2008-01-16 15:55 CCodes -rw-r--r-- 1 zhang staff 38 2008-01-16 16:01 .HiddenFile -rw-r--r-- 1 zhang staff 53 2008-01-16 15:57 README drwxr-xr-x 2 zhang staff 4096 2008-01-16 15:55 SampleCodes drwxr-xr-x 4 zhang staff 4096 2008-01-16 15:56 ShellScriptes d means directory 27 User name of the owner and its group Who has permission to read/write the file Long listing explained drwxr-xr-x 4 zhang staff 4096 2008-01-16 15:56 ShellScriptes field 1 1st Character: specifies the type of the file. - normal file, d directory, s socket file, l link file next 9 characters – File Permissions 28 field 2: specifies the number of links for that file field 3 : specifies owner of the file field 4: specifies the group of the file field 5 : specifies the size of file. field 6: date and time of last modification of the file field 7: File name File permissions drwxr-xr-x 4 zhang staff 4096 2008-01-16 15:56 ShellScriptes Each file is associated with permission info. Differentiate three type of users: owner user, users from same group as owner, others Three type of access: - in the field means no permission Read (r): use “cat” to open a file to read, use “ls” to list files/directories under a directory Write (w): modify the contents of the file, create/remove files from the directory Execute (x): execute the file, or “cd” or “search” the directory for file Trying to list other’s directory 29 [zhang@storm ~]$ ls ../roche/ ls: cannot open directory ../roche/: Permission denied Outline Shell: interactive mode and batch mode Unix commands: syntax and some basic useful commands File systems and commands, emacs or vi Users and groups Environment variables and configuration files Standard input/output, redirection, pipeline grep command basics Shell scripting: control structures (branch and loop) 30 User and Group Each user has unique login name (user name), and corresponding numeric ID Group id, home directory, login shell: Stored in file /etc/passwd Groups: each user can belong to multiple groups For collaboration … Group name, group id, user list info stored in /etc/group Superuser ID: 0, username: root Bypass all permission checks … 31 More to play with who: show who is logged on who am I write: write to another user’s terminal (IMS?) which: show the full path name of a command $ which bash /bin/bash How does shell find a command ? Environment variable PATH stores a list of paths to search for programs: “set | grep PATH” or “echo $PATH”, “set” to show all variable settings PATH=$PATH:$HOME/bin:. Built-in commands: history, set, echo, etc. mail: send email from command line mail –s “graded project” zhang < proj1.cpp 32 Outline Shell: interactive mode and batch mode Unix commands: syntax and some basic useful commands File systems and commands, emacs or vi Users and groups Environment variables and configuration files Standard input/output, redirection, pipeline grep command basics Shell scripting: control structures (branch and loop) 33 SHELL Variables Different types of variables Environment variables: HOME, PATH, PS1, PS2 … Parameter variables: $1, $*, … User defined variables: student, file, x, .. PATH variable: define the list of directories that shell searches for programs PATH=$PATH:.:~zhang/bin export PATH # make PATH an environment variable, which is inherited by all subprocesses 34 Variables A variable is a name that you give to a particular piece of information. Shell variable names: start with a letter or underscore, and may contain any number of following letters,digits,or underscores. Shell variables hold string values, there is no limit on length of string value variable values can be, and often are, empty—that is, they contain no characters. Empty values are referred to as null 35 Variable assignment Assign value to variable: writing variable name, immediately followed by an = character, and new value, without any intervening spaces. myvar=this_is_a_long_string_that_does_not_mean_much first=isaac middle=bashevis last=singer ##Multiple assignments allowed on one line Shell variable values are retrieved by prefixing the variable’s name with a $ character. echo $myvar ## display the value of myvar this_is_a_long_string_that_does_not_mean_much 36 Environment Variables Environment variables: a list of name-value pairs available to all programs running from the shell A child process inherits environment from parent process Variables not in environment not inherited export command: a bulit-in command Puts given variable into environment, i.e., makes the variable an environment variables Will learn how to access environment from C/C++ program When setting PATH, needs to put it into environment, unless only for current script 37 echo command echo: produce output, prompting or to generate data for further processing. printed its arguments to standard output, with each one separated from next by a space and terminated with a newline $ echo Now is the time for all good men Now is the time for all good men $ echo to come to the aid of their country. to come to the aid of their country. Example usage: to see the settings of environment variables … echo $PATH echo $HADOOP_CONF_DIR 38 Bash: startup initialization For login shell, run ~/.bash_profile For non-login shell, run ~/.bashrc Suggested way to configure your environment: Define your environment variables, and alias in ~/.bashrc Call ~/.bashrc in the ~/.bash_profile file For example: # .bash_profile 39 # Get the aliases and functions. if [ -f ~/.bashrc ]; then . ~/.bashrc //source command, or dot command fi Source command A shell builtin command Usage: . filename [arguments] source filename [arguments] Read and execute commands from filename in current shell environment, and return exit status of last command executed from filename. Demo: difference of running a script directly and source it $./CountFiles $source CountFiles Why? When running a script directly, a new shell (non-login, non-interactive shell) 40 is started to batch processing script … Outline Shell: interactive mode and batch mode Unix commands: syntax and some basic useful commands File systems and commands, emacs or vi Users and groups Environment variables and configuration files Standard input/output, redirection, pipeline grep command basics Shell scripting: control structures (branch and loop) 41 Standard I/O All programs should have a data source, a data sink (where data goes),and a place to report problems. These are standard input, standard output, standard error. • Standard input, by default is linked to keyboard • Standard output, by default is linked to terminal window • Standard error, by default linked to terminal window A program should neither know, nor care, what kind of device lies behind its input and outputs: disk files,terminals, tape drives,network connections,or even another running program! A program can expect these standard places to be already open and ready to use when it starts up. 42 Simple example A very simple C program #include <stdio.h> main() { char yourName[256]; printf ("Your name ?\n"); // Similar to cout if (fgets (yourName,256,stdin)==NULL) //similar to cin fprintf (stderr,"No input"); else printf("hello, %s\n", yourName); } 43 Input/Output Redirection On command line, one can redirect these three files To redirect standard output to a disk file: command [ [ - ] option (s) ] [ option argument (s) ] [ command argument (s) ] > FILENAME Execute the command, sending its standard output to specified file Existing content of the file is deleted E.g.: ls –lt > InfoFilelist.txt To append standard output to a file: use >> instead of > grep “tax reform” *.txt > output grep “fuel efficiency” *.txt >> output 44 Input/Output Redirection (cont’d) To redirect standard error to a file $ command [ [ - ] option (s) ] [ option argument (s) ] [ command argument (s) ] 2> ERRORMSGS Examples: [zhang@storm ~]$ ls abc ls: cannot access abc: No such file or directory [zhang@storm ~]$ ls abc 2> error [zhang@storm ~]$ more error ls: cannot access abc: No such file or directory 45 User > and 2> together To split error messages from normal output [zhang@storm ~]$ ls research.tex abc ls: cannot access abc: No such file or directory research.tex [zhang@storm ~]$ ls research.tex abc 2> error > output [zhang@storm ~]$ cat error ls: cannot access abc: No such file or directory [zhang@storm ~]$ cat output research.tex This is useful for running a command that might take long time to finish, or generates very long output … 46 More on redirection To redirect both output and error to same file: ./a.out > dd 2> dd : does not work. Error output is not captured. sort file.txt > dd 2>&1 2>&1: redirect error output to same place as standard output grep numOfStudents 2>dd >&2 >&2: redirect standard output to same place as error output To discard output, redirect it to /dev/null /dev/null: a special virtual file, “a black hole” ./a.out > /dev/null 2>&1 I don’t want to see the output or error message, nor do I want them saved to a file … 47 Input/Output Redirection (cont’d) To read standard input from a file, instead of keyboard $ command [ [ - ] option (s) ] [ option argument (s) ] [ command argument (s) ] < FILENAME Examples mail zhang –s “Question” < proj1.cpp ./a.out < values.txt //a.out is your program that reads integers from standard input and calculate the sum 48 Command line Short options (-) and long options (--) in POSIX, use two dashes (– –) to signify end of options, i.e., remaining arguments on command line that look like options are treated as arguments (for example, as filenames). To delete a file named “-l”, rm -- -l Semicolons separate multiple commands on same line. The shell executes them sequentially. ampersand (&), tell shell to run preceding command in background, which simply means that shell doesn’t wait for command to finish before continuing to next command. 49 Pipe: getting rid of temporary file Pipe: an inter-process communication mechanism provided by kernel Has a reading end and a writing end Any data write to writing end can be read back from reading end Read/write pipe is no different from read/write files, i.e., any prog. that reads from standard input can read from pipe, similarly for the standard output Writing end 50 Reading end Command Pipeline Shell set things up create a pipe, “start” two programs simultaneously, with the first program’s output redirected to writing end of pipe, second program’s input redirected to reading end of pipe individual program/command knows nothing about redirection and pipe 51 Outline Shell: interactive mode and batch mode Unix commands: syntax and some basic useful commands File systems and commands, emacs or vi Users and groups Environment variables and configuration files Standard input/output, redirection, pipeline grep command basics Shell scripting: control structures (branch and loop) 52 Filter programs Filter: program that takes input, transforms input, produces output. default: input=stdin, output=stdout e.g.: grep, sed, awk Typical use: $ program pattern_action filenames program scans files (if no file is specified, scan standard input), looking for lines matching pattern, performing action on matching lines, printing each transformed line. 53 grep/egrep/fgrep commands grep comes from ed (Unix text editor) search command “global regular expression print” or g/re/p so useful that it was written as a standalone utility two other variants grep - pattern matching using Basic Regular Expression fgrep – file (fast, fixed-string) grep, does not use regular expressions, only matches fixed strings but can get search strings from a file egrep - extended grep, uses a Extended Regular Expression (more powerful, but does not support backreferencing) 54 grep syntax Syntax grep [-hilnv] [-e expression] [filename], or grep [-hilnv] expression [filename] Options 55 -E use extended regular expression (replace egrep) -F match using fixed string (replace fgrep) -h do not display filenames -i Ignore case -l List only filenames containing matching lines -n Precede each matching line with its line number -v Negate matches -x Match whole line only (fgrep only) -e expression Specify expression as option -f filename Take regular expression (egrep) or a list of strings (fgrep) from filename What Is a Regular Expression? A regular expression (regex) describes a set of possible input strings, i.e., a pattern e.g., ls –l | grep ^d ## list only directories e.g., grep MAX_INT *.h ## where is MAX_INT defined Regular expressions are endemic to Unix vi, ed, grep, egrep, fgrep; sed emacs, awk, tcl, perl, Python more, less, page, pg Libraries for matching regular expressions: GNU C Library, and POSIX.2 interface (link) 56 Meta characters ^ (Caret) match expression at start of a line, as in ^d. $ (Dollar) match expression at end of a line, as in A$. \ (Back slash) turn off special meaning of next character, as in \^. [ ] (Brackets) match any one of the enclosed characters, as in [aeiou], use hyphen "-" for a range, as in [0-9]. [^ ] match any one character except those enclosed in [ ], as in [^0-9]. . (Period) match a single character of any value, except end of line. *(Asterisk) match zero or more of preceding character or expression. 57 Protect Metacharacters from Shell Some regex metachars have special meaning for shell: globbing and variable reference $grep e* .bash_profile ## suppose there are files email.txt, e_trace.txt # under current dir Actual command executed is: grep email.txt e_trace.txt .bash_profile $grep $PATH file ## $PATH will be replaced by value of PATH… Solution: single quote regexs so shell won’t interpret special characters grep ′e*′ .bash_profile double quotes differs from single quotes: allows for variable substitution whereas single quotes do not. 58 Escaping Special Characters \ (backslash): match special character literally, i.e., escape it E.g., to match character sequence 'a*b*‘ 'a*b*' : ## match zero or more ‘a’s followed by zero or more ## ‘b’s, not what we want 'a\*b\*' ## asterisks are treated as regular characters Hyphen when used as first char in pattern needs to be escaped ls –l | grep '\-rwxrwxrwx' # list all regular files that are readable, writable and executable to all To look for reference to shell variable PATH in a file grep '\$SHELL' file.txt 59 Regex special char: Period (.) Period . in regex matches any character. grep ′o. ′ file.txt regular expression o . For me to poop on. match 1 match 2 How to list files with filename of 5 characters ? ls | grep ′….. ′ ## actually list files with filename 5 or more chars long? Why? How to list normal files that are executable by owners? ls –l | grep ′\-..x ′ 60 This is one line of text o.*o x xyz \m ^ $ . [xy^$x] [^xy^$z] [a-z] r* r1r2 \(r\) \n \{n,m\} r+ r? r1|r2 (r1|r2)r3 (r1|r2)* 61 {n,m} Ordinary characters match themselves (NEWLINES and metacharacters excluded) Ordinary strings match themselves Matches literal character m Start of line End of line Any single character Any of x, y, ^, $, or z Any one character other than x, y, ^, $, or z Any single character in given range zero or more occurrences of regex r Matches r1 followed by r2 Tagged regular expression, matches r Set to what matched the nth tagged expression (n = 1-9) Repetition One or more occurrences of r Zero or one occurrences of r Either r1 or r2 Either r1r3 or r2r3 Zero or more occurrences of r1|r2, e.g., r1, r1r1, r2r1, r1r1r2r1,…) Repetition input line regular expression fgrep, grep, egrep grep, egrep grep egrep Quick Reference Examples Interesting examples of grep commands To search lines that have no digit character: grep -v '^[0-9]*$' filename Look for users with uid=0 (root permission) grep '^[^:]*:[^:]*:0:' /etc/passwd To search users without passwords: grep ‘^[^:]*::’ /etc/passwd To search for binary numbers To search for telephone numbers To match time of day, e.g., 12:14 am, 9:02pm, … 62 Outline Shell: interactive mode and batch mode Unix commands: syntax and some basic useful commands File systems and commands, emacs or vi Users and groups Environment variables and configuration files Standard input/output, redirection, pipeline grep command basics Shell scripting: control structures (branch and loop) 63 Control Structures & Conditions Control structures in bash if … then … fi if … then … else … fi if … then …elif … else … fi for … in … do … done while … do … done until … do … done case … in … esac break, continue Conditions (tests): used in if structures, while, until structures, similar to boolean expression in C/C++ 64 Dots shown in red are to be replaced with conditions Conditions in shell Exit status of a command, script or shell function, e.g., if diff file1 file2 >& /dev/null ## if file1 and file2 are the same … test command: used to perform a variety of test, e.g., test file attributes, compares strings and numbers. if test –e tmp.o ## if there is file named test.o … Compound condition: combine above using ! (negation), && (and), || (or) if !grep pattern myfile > /dev/null … 65 Exit status command/script/function Exit Status: every command (built-in, external, or shell function) returns a small integer value when it exits, to the program invoked it. Convention: command/program returns a zero when it succeeds and some other status when it fails How to return value from shell script? exit command, syntax exit [exit-value] Return an exit status from a shell script to its caller If exit-value is not given, exit status of last command executed will be returned. If this is what you want, do so explicitly using exit $? 66 ? A special variable stores exit status of previous command. 67 test command Used to perform a variety of test in shell scripts, e.g., test file attributes, compares strings and numbers. Provide no regular output, used exclusively for its exit status Syntax: test expression [ expression ] Note: space between [, ] and expression … 68 69 70 Numerical tests work on integers only. 71 Test status of file: file conditionals File conditionals: unary expressions examining status of a file if test –e /etc/.bashrc ## same as if [ -e /etc/.bashrc ] ## do something if /etc/.bashrc exists then ## do something else if it doesn’t fi More testing -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. -s file: true if the file has nonzero size 72 numerical comparison: example echo -n "Enter your age: " read age if [ $age -lt 18 ] then echo "You need to be 18 or older to apply for account" else echo "Choose your preferred account name" fi 73 Numeric Comparison: check arguments Often in shell script, need to check # of command line arguments: #! /bin/bash ## this script … ## check arguments … if [ $# -lt 2 ] ## less than 2 command lines arguments are specified … then echo “usage: $0 file1 file2” exit 1 ## 0 means succeed, otherwise failed fi 74 … string comparison 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. 75 Logical NOT, AND, and OR if !grep pattern myfile > /dev/null ## Negation then # pattern not here fi if grep pattern1 file && grep pattern2 file then ##contain both pattern Fi 76 if grep pattern1 file || grep pattern2 file then ## contain at least one pattern fi Roadmap Shell variables Shell Arithmetic Control Structures Test, condition, logic operations Selection: if statements Loop: while, until, for loops; break, continue Case structure 77 Selection in shell Implementing branch: change flow of control based on conditions 78 if condition then commands fi if condition then commands1 else commands2 fi if condition1 then commands1 elif condition2 commands2 else commands3 fi case … in … esac Can be repeated… if control structure Single-line Syntax if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi Multi-line Syntax if TEST-COMMANDS then CONSEQUENT-COMMANDS fi Recall: command line terminates with NEWLINE, ;, &. 79 [zhang@storm ~]$ cat checkps #!/bin/bash echo -n "Enter your password: " read password if [ $password == "secret" ] then echo "Welcome!" else echo "Go away!" fi [zhang@storm ~]$ checkps Enter your password: secret Welcome! [zhang@storm ~]$ checkps Enter your password: guess Go away! [zhang@storm ~]$ Important note: When spanning multiple lines, do not need the ; 80 if … then … elif … then …else if [[ "$op" == "+" ]] then result=$(($x + $y)) echo $x $op $y = $result elif [[ "$op" == "-" ]] then result=$(($x - $y)) echo $x $op $y = $result 81 elif [[ "$op" == "*" ]] then result=$(($x * $y)) echo $x \* $y = $result elif [[ "$op" == "/" ]] then result=$(($x / $y)) echo $x $op $y = $result else echo "Unknow operator $op" fi 82 if… statements can be nested #!/bin/bash # This script will test if we're in a leap year or not. year=`date +%Y` if [ $[$year % 400] -eq 0 ]; then echo "This is a leap year. February has 29 days." elif [ $[$year % 4] -eq 0 ]; then if [ $[$year % 100] -ne 0 ]; then echo "This is a leap year, February has 29 days." else echo "This is not a leap year. February has 28 days." fi else echo "This is not a leap year. February has 28 days." fi Roadmap Shell variables Shell Arithmetic Control Structures Test, condition, logic operations Selection: if statements Loop: while, until, for loop; break, continue case structure 83 Loop structure: while loop Multi-line Syntax: while condition do commands done Single-line Syntax (useful in interactive mode) while condition; do commands; done Note: condition and commands terminated with ; 84 while loop declare –i i=1 ## an integer variable I while [ $i -le 10 ] do echo "loop $i" i=i+1 ## can use this since i is integer done If i is not declared as integer … i=$(($i+1)) i=$[$i+1] 85 [zhang@storm ~]$ cat checkps #!/bin/bash echo -n "Enter your password: " read password if [ $password == "secret" ] then echo "Welcome!" else echo "Go away!" fi • How to modify this to allow user to try until the password matches? 86 [zhang@storm ~]$ cat checkps #!/bin/bash while test $password != "secret" ## as long as password is not same as “secret” do echo -n "Enter your password: " read password done echo "Welcome!” • What if we give the user at most 3 tries? 1. use a variable to keep track number of tries … 2. modify condition … 87 until loop Tests for a condition, and keeps looping as long as that condition is false (opposite of while loop). until condition do command(s)... done e.g.: 88 $until [ $passwd == "secret" ] ; do echo -n "Try again: "; read passwd; done Try again: guess Try again: password Try again: secret $ Wait for a user to log in # wait for a user to log in, check every 30 seconds printf “Enter username:” read user until who | grep “$user” > /dev/null do sleep 30 done Can you convert it to while loop? 89 For loops For loop: iterates over a list of objects, executing loop body 90 for each individual object in the list for variable in a_list_of_objects do ## do something on $variable commands .. done e.g., for filename in lab1.cpp lab2.cpp lab3.cpp do indent $filename done For loop: 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 How to avoid using temporary file, greeting_$student ? 91 Using for loop Use for loop to print out 2’s power Command seq: print out a sequence of number #!/bin/bash # print out 2’s powers for a in `seq 1 10` do echo 2^$a=$((2**a)) done Note: ** is the exponent operator 92 case construct: branching case construct is analogus to switch in C/C++. 93 case "$variable" in shellpattern1 ) command... ;; shellpattern2) command … ;; shell pattern n) command... ;; esac • Quoting variables is not mandatory • Each pattern can contain shell wildcard (*,?,[a-z]), ends with a ) • Each condition block ends with ;; • If a condition tests true, then associated commands execute and the case block terminates. • entire case block ends with an esac 94 #!/bin/bash OPT=$1 # option Lazy evaluation of && and || FILE=$2 # filename # test -e and -E command line args matching case $OPT in -e|-E) test if string is null echo "Editing $2 file..." # make sure filename is passed else an error displayed [ -z $FILE ] && { echo "File name missing"; exit 1; } || vi $FILE ;; -c|-C) echo "Displaying $2 file...“ [ -z $FILE ] && { echo "File name missing"; exit 1; } || cat $FILE ;; -d|-D) echo "Today is $(date)" ;; *) echo "Bad argument!" echo "Usage: $0 -ecd filename" echo " -e file : Edit file." echo " -c file : Display file." echo " -d : Display current date and time." ;; esac Case example 95 case $1 in -f) ## case for –f option ;; -d | --directory) ## -f or –directory option ;; *) echo $1: unknown option >&2 exit 1; esac