lecture 4
play

Lecture 4 Log into Linux Reminder: Homework 1 due today, 4:30pm - PowerPoint PPT Presentation

Lecture 4 Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions? Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 1 Outline More


  1. Lecture 4  Log into Linux  Reminder: Homework 1 due today, 4:30pm  Homework 2 out, due next Tuesday  Project 1 out, due next Thursday  Questions? Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 1

  2. Outline  More BASH programming  Command Substitution  Arithmetic Substitution  Additional Useful Commands  Grouping  Exercises Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 2

  3. 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 '^ar63:' /etc/passwd) passwd=$(cat /etc/passwd) # capture file Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 3

  4. 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 7 CS 375 UNIX System Programming - Lecture 4 4

  5. HERE Documents  HERE documents are a special form of input redirection that read input from the script. cat <<EOTEXT # Display multiline message Hi there! How are you? EOTEXT # Example with variable substitution sftp $user@csserver <<SFTPINPUT cd $remotedir get $file bye SFTPINPUT Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 5

  6. Functions and Aliases  Functions only have local scope by default, but can be exported. error() { echo “$*” > /dev/stderr ; }  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 7 CS 375 UNIX System Programming - Lecture 4 6

  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 7 CS 375 UNIX System Programming - Lecture 4 7

  8. Useful Commands  find will locate files and directories that satisfy certain criteria. It recurses through directories.  Syntax: find <dir> <matching criteria> <actions>  Lots of matching criteria, e.g. -mtime n , -type c , -user u , -name <regex> . (See man page.) All must match unless use -o (OR) explicitly.  Some built-in actions, e.g. -print , -ls . General -exec <cmd> {} \; (where optional ' {} ' represents the current match). Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 8

  9. Useful Commands  find examples: find . -name '*.txt' # all *.txt in current dir find . -mtime -1 # modified in last day # Use grep to search for strings # print is required here find . -exec grep -iq "dh27" {} \; -print # Change perms on all files find . -type f -exec chmod 644 {} \; Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 9

  10. Useful Commands  grep search for files containing regular expressions (similar to the wildcards, see BLP pages 66-67).  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 Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 10

  11. 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  bc is an arbitrary precision calculator. # get pi to 40 places pi=$(echo 'scale=40; 4*a(1)' | bc -l) Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 11

  12. 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 # user input is $line read # read line and throw away  printf can give fancier output than echo . printf “%8.4f\t\t%s\n” $val $myhome Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 12

  13. 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 7 CS 375 UNIX System Programming - Lecture 4 13

  14. 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 7 CS 375 UNIX System Programming - Lecture 4 14

  15. 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 # Will display xyz (myvar=mno) echo $myvar # Will display xyz Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 15

  16. In-class Exercises  Write a shell script named lastfirst that takes a single filename argument and writes the file to standard output in complete reverse order (last line, last character first).  Hint: Try “man -k reverse” to find relevant utilities.  Write appropriate error messages if an incorrect number of arguments is used or if the input file does not exist.  Exit with appropriate status.  Read from standard input if no arguments are given. Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 16

  17. 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 7 CS 375 UNIX System Programming - Lecture 4 17

Recommend


More recommend