Download presentation
Presentation is loading. Please wait.
Published byCharlene Bridges Modified over 8 years ago
1
240-491 Adv. UNIX: Shell/21 Advanced UNIX v Objectives –to supplement the “Introduction to UNIX” slides with extra information about the Shell 240-491 Special Topics in Comp. Eng. 1 Semester 2, 2000-2001 2. The Shell (Ch.5, Sobell)
2
240-491 Adv. UNIX: Shell/22 Overview 1.Redirection 2.Pipes 3.Background Jobs 4.Filename Generation
3
240-491 Adv. UNIX: Shell/23 command standard input standard output 1. Redirection v Command I/O is stream-based:
4
240-491 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
5
240-491 Adv. UNIX: Shell/25 command standard input standard output file Redirect Output v Use > to redirect standard output to a ‘file’:
6
240-491 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
7
240-491 Adv. UNIX: Shell/27 command standard input standard output Redirect Input v Use < to redirect standard input from a ‘file’: file
8
240-491 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 ad@ratree.psu.ac.th < letter.txt
9
240-491 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
10
240-491 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 $
11
240-491 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:
12
240-491 Adv. UNIX: Shell/212 v Use the ‘|’ operator between commands: $ command 1 | command2 v Same as: $ command1 > temp $ command2 temp $ command2 < temp $ rm temp
13
240-491 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
14
240-491 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
15
240-491 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
16
240-491 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
17
240-491 Adv. UNIX: Shell/217 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.
18
240-491 Adv. UNIX: Shell/218 v Background jobs end with a ‘&’: $ gcc big-program.c & 1466 $ ls -l | lpr & 1467 $ vi report.txt
19
240-491 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 1466 03 S 0:05 gcc big-program.c 1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $
20
240-491 Adv. UNIX: Shell/220 4. Filename Generation Commands involving filenames (e.g. cat, ls ) can include special characters in the filenames. –called metacharacters –three kinds: ?*[...]
21
240-491 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
22
240-491 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
23
240-491 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
24
240-491 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
25
240-491 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
26
240-491 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
27
240-491 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]
28
240-491 Adv. UNIX: Shell/228 Combining Special Characters $ ls [a-m]* $ ls *[x-z] $ lpr p*[0-9].c &
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.