Adv. UNIX: Shell/21 Advanced UNIX v Objectives –to supplement the “Introduction to UNIX” slides with extra information about the Shell Special Topics in Comp. Eng. 1 Semester 2, The Shell (Ch.5, Sobell)
Adv. UNIX: Shell/22 Overview 1.Redirection 2.Pipes 3.Background Jobs 4.Filename Generation
Adv. UNIX: Shell/23 command standard input standard output 1. Redirection v Command I/O is stream-based:
Adv. UNIX: Shell/24 $ cat This is a line of text. This is a line of text. Cat keeps copying lines of text Cat keeps copying lines of text until you press control-D at the until you press control-D at the beginning of a line. beginning of a line. $ control-D You type a line; it is echoed
Adv. UNIX: Shell/25 command standard input standard output file Redirect Output v Use > to redirect standard output to a ‘file’:
Adv. UNIX: Shell/26 $ cat > sample.txt This text is being entered at the keyboard. Cat is copying it to a file. Press control-D to indicate the end of file. $ $ cat file1.c file2.c file3.c > all-files.c control-D
Adv. UNIX: Shell/27 command standard input standard output Redirect Input v Use < to redirect standard input from a ‘file’: file
Adv. UNIX: Shell/28 $ cat < supply_orders 2000 sheets letterhead ordered: 10/7/97 1 box masking tape ordered: 10/8/97 $ $ cat supply_orders $ mail < letter.txt
Adv. UNIX: Shell/29 Dangers Bad: $ cat orange pear > orange cat: input orange is output –see noclobber in C Shell Good: $ cat orange pear > temp $ mv temp orange
Adv. UNIX: Shell/210 Appending Output to a File v Use >> to append: $ date > whoson $ cat whoson Fri May 29 09:24:19 GMT 2000 $ who >> whoson $ cat whoson Fri May 29 09:24:19 GMT 2000 jenny tty02 May 29 07:21 ad tty06 May 28 11:01 $
Adv. UNIX: Shell/211 command1 standard input standard output command2 2. Pipes v Use a pipe to connect standard output of one command to standard input of another:
Adv. UNIX: Shell/212 v Use the ‘|’ operator between commands: $ command 1 | command2 v Same as: $ command1 > temp $ command2 temp $ command2 < temp $ rm temp
Adv. UNIX: Shell/213 v $ ls | more v $ who | grep ‘ad’ ad tty06 May 23 10:31 v $ who | sort ad tty06 May 23 10:31 jenny tty02 May 21 15:29 scott tty03 May 23 09:02 Same as: $ who > temp $ sort temp $ sort < temp or $ sort temp
Adv. UNIX: Shell/214 Filters v A filter is a command that modifies its standard input, putting the changes onto its standard output: $ who | sort | lpr $ ps | grep ad
Adv. UNIX: Shell/215 The tee Command v Passes its input through to standard output unchanged. Also saves input into a file: command1 standard input standard output command2tee file
Adv. UNIX: Shell/216 $ who | tee who.out | grep ad ad tty06 May 23 10:31 v $ cat who.out jenny tty02 May 21 15:29 ad tty06 May 23 10:31 scott tty03 May 23 09:02
Adv. UNIX: Shell/ Background Jobs v A normal command executes in the foreground: you wait until it finishes before another command can be typed. v Commands (jobs) can execute in the background. No need to wait for them before another command is typed.
Adv. UNIX: Shell/218 v Background jobs end with a ‘&’: $ gcc big-program.c & 1466 $ ls -l | lpr & 1467 $ vi report.txt
Adv. UNIX: Shell/219 Killing a Background Job Cannot type control-C Use kill and the process ID (PID): $ kill 1466 Use ps to list PIDs: $ ps PID TT STAT TIME COMMAND S 0:05 gcc big-program.c S 0:04 ls -l | lpr R 0:03 ps $
Adv. UNIX: Shell/ Filename Generation Commands involving filenames (e.g. cat, ls ) can include special characters in the filenames. –called metacharacters –three kinds: ?*[...]
Adv. UNIX: Shell/221 The ? Special Character v ? matches any single character $ ls mem memo12 memo9 memoalex newmemo5 memo memo5 memoa memos $ ls memo? memo9 memo5 memoa memos $ lpr memo? continued
Adv. UNIX: Shell/222 $ ls 7may4report may14report may4report.79 mayqreport may.report may4report may_report mayreport $ ls may?report mayqreport may.report may4report may_report
Adv. UNIX: Shell/223 The * Special Character v * matches any sequence of characters (0 or more characters) $ ls amemo memo memoa memosally user.memo mem memo.0612 memorandum sallymemo $ ls memo* memo memoa memosally memo.0612 memorandum continued
Adv. UNIX: Shell/224 $ ls *.txt $ lpr *.txt $ ls *.c $ cat *.c > all-files $ more all-files $ rm *.c $ mv all-files all-files.c
Adv. UNIX: Shell/225 The [...] Special Characters v Match against any single character given inside [...] v Can include ‘-’ to give a range $ ls part1.txt part2.txt part3.txt part4.txt part5.txt $ lpr part[135].txt $ cat part[1-3].txt continued
Adv. UNIX: Shell/226 Useful Ranges [a-z] any letter between a and z [A-Z] any letter between A and Z [0-9] any digit betwwn 0 and 9 v Can combine: [a-z,0-9] continued
Adv. UNIX: Shell/227 $ ls part0 part1 part2 part3 part4... part32 part33 part34 part35 $ ls part[0-9] $ ls part[12][0-9] $ ls part3[0-5]
Adv. UNIX: Shell/228 Combining Special Characters $ ls [a-m]* $ ls *[x-z] $ lpr p*[0-9].c &