Date post: | 10-Apr-2018 |
Category: |
Documents |
Upload: | sangram-anand |
View: | 219 times |
Download: | 0 times |
of 28
8/8/2019 02 Shell Intro
1/28
Advanced UNIX (Shell)
Objectives to supplement the Introduction to UNIX
slides with extra information about the Shell
8/8/2019 02 Shell Intro
2/28
Overview1. Redirection
2. Pipes
3. Background Jobs
4. Filename Generation
8/8/2019 02 Shell Intro
3/28
command
standard input
standard
output
1. Redirection
Command I/O is stream-based:
8/8/2019 02 Shell Intro
4/28
$ catThis is a line of text.This is a line of text.
Cat keeps copying lines of textCat keeps copying lines of text
until you press control-D at theuntil you press control-D at the
beginning of a line.beginning of a line.
$
control-D
You type a line;
it is echoed
8/8/2019 02 Shell Intro
5/28
command
standard input
standard
output file
RedirectOutput Use > to redirect standard output
to a file:
8/8/2019 02 Shell Intro
6/28
$ cat > sample.txtThis 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
8/8/2019 02 Shell Intro
7/28
command
standard
input
standard
output
Redirect Input Use < to redirect standard input
from a file:
file
8/8/2019 02 Shell Intro
8/28
$ cat < supply_orders
2000 sheets letterhead ordered: 10/7/971box masking tape ordered: 10/8/97
$
$ cat supply_orders
$ mail [email protected] < letter.txt
8/8/2019 02 Shell Intro
9/28
D
angers Bad:
$ cat orange pear > orange
cat: input orange is output
see noclobber in C Shell
Good:$ cat orange pear > temp$ mv temp orange
8/8/2019 02 Shell Intro
10/28
Appending Output to a File 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
$
8/8/2019 02 Shell Intro
11/28
command1
standardinput
standardoutput
command2
2
. Pipes Use a pipe to connect standard
output of one command to
standard input of another:
8/8/2019 02 Shell Intro
12/28
Use the | operator between commands:
$ command 1| command2
Same as:
$ command1> temp$ command2 < temp
$ rm temp
8/8/2019 02 Shell Intro
13/28
$ ls | more
$ who | grep ad
ad tty06 May 23 10:31
$ who | sortad tty06 May 23 10:31jenny tty02 May 21 15:29scott tty03 May 23 09:02
Same as:$ who > temp
$ sort < temp or $ sort temp
8/8/2019 02 Shell Intro
14/28
Filters A filter is a command that modifies
its standard input, putting the
changes onto its standard output:
$ who | sort | lpr
$ ps | grep ad
8/8/2019 02 Shell Intro
15/28
The tee Command
Passes its input through to standard output
unchanged.Also saves input into a file:
command1
standardinput
standardoutput
command2tee
file
8/8/2019 02 Shell Intro
16/28
$ who | tee who.out | grep ad
ad tty06 May 23 10:31
$ cat who.out
jenny tty02 May 21 15:29
ad tty06 May 23 10:31
scott tty03 May 23 09:02
8/8/2019 02 Shell Intro
17/28
3. Background Jobs
A normal command executes in the
foreground: you waituntil it finishes before
another command can be typed.
Commands (jobs) can execute in the
background.No need to waitfor them before
another command is typed.
8/8/2019 02 Shell Intro
18/28
Background jobs end with a &:
$ gcc big-program.c &
1466
$ ls -l | lpr &
1467
$ vi report.txt
8/8/2019 02 Shell Intro
19/28
Killing a Background Job Cannot type control-C
Use killand the process ID (PID):$ kill 1466
Use psto 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
$
8/8/2019 02 Shell Intro
20/28
4. Filename Generation
Commands involving filenames (e.g.cat, ls)
can include special characters in the
filenames.called metacharacters
three kinds:
?
*
[...]
8/8/2019 02 Shell Intro
21/28
The ? Special Character
? matches any single character
$ lsmem memo12 memo9 memoalex newmemo5memo memo5 memoa memos
$ ls memo?
memo9 memo5 memoa memos
$ lpr memo?
continued
8/8/2019 02 Shell Intro
22/28
$ ls
7may4report may14reportmay4report.79
mayqreport may.report may4reportmay_report mayreport
$ ls may?report
mayqreport may.report may4reportmay_report
8/8/2019 02 Shell Intro
23/28
The * Special Character
* matches any sequence of characters
(0 or more characters)
$ ls
amemo memo memoa memosallyuser.memo mem memo.0612
memorandum sallymemo
$ ls memo*memo memoa memosally memo.0612
memorandum
continued
8/8/2019 02 Shell Intro
24/28
$ ls *.txt
$ lpr *.txt
$ ls *.c$ cat *.c > all-files
$ more all-files$ rm *.c
$ mv all-files all-files.c
8/8/2019 02 Shell Intro
25/28
The [...]Special Characters
Match against any single character given inside
[...]
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
8/8/2019 02 Shell Intro
26/28
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
Can combine:
[a-z,0-9]
continued
8/8/2019 02 Shell Intro
27/28
$ lspart0 part1 part2 part3 part4 ...
part32 part33 part34 part35
$ ls part[0-9]$ ls part[12][0-9]
$ ls part3[0-5]
8/8/2019 02 Shell Intro
28/28
Combining Special Characters
$ ls [a-m]*
$ ls *[x-z]
$ lpr p*[0-9].c &