Advanced UNIX 240 491 Special Topics in Comp

  • Slides: 28
Download presentation
Advanced UNIX 240 -491 Special Topics in Comp. Eng. 1 Semester 2, 2000 -2001

Advanced UNIX 240 -491 Special Topics in Comp. Eng. 1 Semester 2, 2000 -2001 . 2 The Shell (Ch. 5, Sobell( v Objectives – to supplement the “Introduction to UNIX” slides with extra information about the Shell 240 -491 Adv. UNIX: Shell/2 1

Overview. 1. 2. 3. 4 240 -491 Adv. UNIX: Shell/2 Redirection Pipes Background Jobs

Overview. 1. 2. 3. 4 240 -491 Adv. UNIX: Shell/2 Redirection Pipes Background Jobs Filename Generation 2

. 1 Redirection v Command I/O is stream-based: command standard output standard input 240

. 1 Redirection v Command I/O is stream-based: command standard output standard input 240 -491 Adv. UNIX: Shell/2 3

$cat This is a line of text. This is a Cat keeps until you

$cat This is a line of text. This is a Cat keeps until you beginning $ You type a line; it is echoed line of text. copying lines of text press control-D at the of a line. control-D 240 -491 Adv. UNIX: Shell/2 4

Redirect Output v Use > to redirect standard output to a ‘file: ’ standard

Redirect Output v Use > to redirect standard output to a ‘file: ’ standard output file command standard input 240 -491 Adv. UNIX: Shell/2 5

$cat > sample. txt This text is being entered at the keyboard. Cat is

$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. $ control-D $cat file 1. c file 2. c file 3. c > all-files. c 240 -491 Adv. UNIX: Shell/2 6

Redirect Input v Use < to redirect standard input from a ‘file: ’ file

Redirect Input v Use < to redirect standard input from a ‘file: ’ file standard input 240 -491 Adv. UNIX: Shell/2 command standard output 7

$cat < supply_orders 2000 sheets letterhead 1 box masking tape $ ordered: 10/7/97 ordered:

$cat < supply_orders 2000 sheets letterhead 1 box masking tape $ ordered: 10/7/97 ordered: 10/8/97 $cat supply_orders $mail ad@ratree. psu. ac. th < letter. txt 240 -491 Adv. UNIX: Shell/2 8

Dangers v Bad: $ cat orange pear cat: input orange > orange is output

Dangers v Bad: $ cat orange pear cat: input orange > orange is output – see noclobber in C Shell v Good: $ cat orange pear > temp $ mv temp orange 240 -491 Adv. UNIX: Shell/2 9

Appending Output to a File v Use >> to append: $date > whoson $

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 tty 02 May 29 07: 21 ad tty 06 May 28 11: 01 $ 240 -491 Adv. UNIX: Shell/2 10

. 2 Pipes v Use a pipe to connect standard output of one command

. 2 Pipes v Use a pipe to connect standard output of one command to standard input of another: command 1 standard input 240 -491 Adv. UNIX: Shell/2 command 2 standard output 11

v Use the ‘|’ operator between commands: $command 1 | command 2 v Same

v Use the ‘|’ operator between commands: $command 1 | command 2 v Same as: $command 1 > temp $ command 2 < temp $ rm temp 240 -491 Adv. UNIX: Shell/2 12

v $ls | more v $who | grep ‘ad’ ad tty 06 May 23

v $ls | more v $who | grep ‘ad’ ad tty 06 May 23 10: 31 v $who | sort ad tty 06 May 23 10: 31 jenny tty 02 May 21 15: 29 scott tty 03 May 23 09: 02 v Same as: $ who > temp $ sort < temp 240 -491 Adv. UNIX: Shell/2 or $ sort temp 13

Filters v A filter is a command that modifies its standard input, putting the

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 240 -491 Adv. UNIX: Shell/2 14

The tee Command v Passes its input through to standard output unchanged. Also saves

The tee Command v Passes its input through to standard output unchanged. Also saves input into a file : file command 1 standard input 240 -491 Adv. UNIX: Shell/2 tee command 2 standard output 15

v v $who | tee who. out | grep ad ad tty 06 May

v v $who | tee who. out | grep ad ad tty 06 May 23 10: 31 $cat who. out jenny tty 02 ad tty 06 scott tty 03 240 -491 Adv. UNIX: Shell/2 May May 21 23 23 15: 29 10: 31 09: 02 16

. 3 Background Jobs v A normal command executes in the foreground: you wait

. 3 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. 240 -491 Adv. UNIX: Shell/2 17

v Background jobs end with a: ’&‘ $gcc big-program. c & 1466 $ls -l

v Background jobs end with a: ’&‘ $gcc big-program. c & 1466 $ls -l | lpr & 1467 $vi report. txt 240 -491 Adv. UNIX: Shell/2 18

Killing a Background Job v Cannot type control-C v Use kill and the process

Killing a Background Job v Cannot type control-C v Use kill and the process ID (PID: ( $kill 1466 v Use ps to list PIDs: $ps PID 1466 1467 1524 $ 240 -491 Adv. UNIX: Shell/2 TT 03 03 03 STAT S S R TIME 0: 05 0: 04 0: 03 COMMAND gcc big-program. c ls -l | lpr ps 19

. 4 Filename Generation involving filenames (e. g. cat, ls) can include special characters

. 4 Filename Generation involving filenames (e. g. cat, ls) can include special characters in the filenames. v Commands – called metacharacters – three kinds: ? * [. . . ] 240 -491 Adv. UNIX: Shell/2 20

The ? Special Character v ? matches any single character $ls memo 12 memo

The ? Special Character v ? matches any single character $ls memo 12 memo 5 memo 9 memoalex memos $ ls memo? memo 9 memo 5 $ lpr memo? memoa memos 240 -491 Adv. UNIX: Shell/2 newmemo 5 continued 21

$ls 7 may 4 report mayqreport may_report may 14 report may 4 report. 79

$ls 7 may 4 report mayqreport may_report may 14 report may 4 report. 79 may. report may 4 report mayreport $ ls may? report mayqreport may_report 240 -491 Adv. UNIX: Shell/2 may 4 report 22

The * Special Character v *matches any sequence of characters (0 or more characters(

The * Special Character v *matches any sequence of characters (0 or more characters( $ls amemo user. memorandum $ ls memo* memoa memorandum 240 -491 Adv. UNIX: Shell/2 memoa memosally memo. 0612 sallymemosally memo. 0612 continued 23

$ls *. txt $ lpr *. txt $ls *. c $ cat *. c

$ls *. txt $ lpr *. txt $ls *. c $ cat *. c > all-files $ more all-files $ rm *. c $ mv all-files. c 240 -491 Adv. UNIX: Shell/2 24

The [. . . ] Special Characters v Match against any single character given

The [. . . ] Special Characters v Match against any single character given inside[. . . ] v Can include ‘-’ to give a range $ls part 1. txt part 2. txt part 3. txt part 4. txt part 5. txt $ lpr part[135]. txt $ cat part[1 -3]. txt 240 -491 Adv. UNIX: Shell/2 continued 25

Useful Ranges v ]a-z] any letter between a and z v ]A-Z] any letter

Useful Ranges v ]a-z] any letter between a and z v ]A-Z] any letter between A and Z v 0 -9]] any digit betwwn 0 and 9 v Can combine: ]a-z, 0 -9[ 240 -491 Adv. UNIX: Shell/2 continued 26

$ls part 0 part 32 $ $ $ ls ls ls part 1 part

$ls part 0 part 32 $ $ $ ls ls ls part 1 part 2 part 3 part 4. . . part 33 part 34 part 35 part[0 -9] part[12][0 -9] part 3[0 -5[ 240 -491 Adv. UNIX: Shell/2 27

Combining Special Characters $ls [a-m]* $ ls *[x-z[ $lpr p*[0 -9]. c& 240 -491

Combining Special Characters $ls [a-m]* $ ls *[x-z[ $lpr p*[0 -9]. c& 240 -491 Adv. UNIX: Shell/2 28