Lecture 4 Log into Linux Reminders: Homework 1 due today; Homework 2 due on Thursday Questions? Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 1
Outline Exercise from last lecture More BASH programming Command Substitution Arithmetic Substitution Additional Useful Commands Grouping Exercises Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 2
Command Substitution Command substitution allows the standard output of a command to replace the command name. There are two forms: `COMMAND` or $ (COMMAND). rm `cat filelist` # rm files in a list txtfiles=$(ls *.txt) # capture ls display userpass=$(grep i '^hwang:' /etc/passwd) passwd=$(cat /etc/passwd) # capture file Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 3
Arithmetic Substitution Arithmetic evaluation uses either let expr or ((expr)). Substitution uses $((expr)). b=12 let a=$b*3 # a is 36 ((a = $b * 3)) # alternative to let val=$(($b**2)) # val is 144 count=0 while [ $count le 10 ] do # do stuff here count=$(($count + 1)) # or ((count++)) done Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 4
HERE Documents HERE documents are a special form of input redirection that reads input from the script. cat <<EOTEXT Hi there! How are you? EOTEXT # Display multiline message # Example with variable substitution sftp $user@csserver <<SFTPINPUT cd $remotedir get $file bye SFTPINPUT Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 5
Functions Functions only have local scope by default, but can be exported. error() { echo $* > /dev/stderr ; } Parameters are accessed using $*, $1, $2, etc. func() { if [ "$1" ]; then echo "Parameter 1 is \"$1\" fi } Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 6
Aliases You can create aliases for commands too: alias ls='ls color=tty' # alias for ls alias ll='ls l color=tty' alias rm='rm i' # force rm to prompt alias # display aliases \ls # run unaliased ls Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 7
BASH Startup Bash shells can be interactive or non-interactive a login or non-login shell. A login shell reads and executes commands from /etc/profile and then one of (in order) either ~/.bash_profile, ~/.bash_login, or ~/.profile. A non-login interactive shell reads commands only from ~/.bashrc. Set exported environment variables in ~/.bash_profile. Define functions and aliases in ~/.bashrc. Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 8
Useful Commands xargs takes filenames from standard input and feeds the names to a command. ls xargs p l gzip expr is an all-purpose expression evaluator a=$(expr 5 + 3) # arithmetic b=$(expr length $filename) # string length bc is an arbitrary precision calculator. # get pi to 40 places pi=$(echo 'scale=40; 4*a(1)' bc l) Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 9
Useful Commands export makes a shell variable available in subshells. export myhome=/home/hwang read will read a line of input from standard input. read line read # user input is $line # read line and throw away printf can give fancier output than echo. printf %8.4f\t\t%s\n $val $myhome Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 10
Useful Commands exec replaces the current process with a new one. It is often used at the end of a script. You can also use it from within a script to redirect subsequent standard output exec vi # "become" vi exec > output.txt echo hello there # sent to output.txt Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 11
Useful Commands eval evaluates its argument(s) as if they were typed on the command line, then the result is evaluated. set one two three # sets $1 $2 $3 n=1 # use value of n to determine which var eval echo \${$n} # evals to echo ${1} Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 12
Grouping Use the following code to read lines of input from a file: while read line do # Do something with $line done < inputfile > outputfile Input (and output) is redirected for all commands inside the body of the while-loop. Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 13
Grouping You can also redirect input and/or output by using braces to group commands: { read x; read y; } < input Use space between the command group and the braces. The final semicolon is required. Alternatively use this form: { read x read y } < input Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 14
Grouping You can use ( ) for grouping too. With ( ), the command group is executed in a subshell (another bash process), so if you set or change any variables those changes will not be seen in the parent process. myvar=abc { myvar=xyz; } echo $myvar (myvar=mno) echo $myvar # Will display xyz # Will display xyz Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 15
In-class Exercises Write a shell script named sortusers1 that displays every other username (from /etc/passwd) in alphabetical order. Display the username only. Write sortusers2 that displays every other username sorted by user id. Display the username only. Write a shell script named revargs that displays its arguments in reverse order (eval may be useful here). Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 16