CIS 218 Advanced UNIX 1 Advanced UNIX Objectives – to supplement the “Introduction to UNIX” slides with extra information about the Shell CIS 218 Advanced UNIX 2. The Shell (Ch.5, Sobell)
CIS 218 Advanced UNIX 1
Advanced UNIX
Objectives
– to supplement the “Introduction to UNIX”
slides with extra information about the Shell
CIS 218 Advanced UNIX
2. The Shell
(Ch.5, Sobell)
CIS 218 Advanced UNIX 2
Overview
1. Redirection
2. Pipes
3. Background Jobs
4. Filename Generation
CIS 218 Advanced UNIX 3
command
standard input
standard
output
1. Redirection
Command I/O is stream-based:
CIS 218 Advanced UNIX 4
$ 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
CIS 218 Advanced UNIX 5
command
standard input
standard
output file
Redirect Output
Use > to redirect standard output to a ‘file’:
CIS 218 Advanced UNIX 6
$ 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
CIS 218 Advanced UNIX 7
command
standard
input
standard
output
Redirect Input
Use < to redirect standard input from a
‘file’:
file
CIS 218 Advanced UNIX 8
$ cat < supply_orders
2000 sheets letterhead ordered: 10/7/97
1 box masking tape ordered: 10/8/97
$
$ cat supply_orders
$ mail [email protected] < letter.txt
CIS 218 Advanced UNIX 9
Dangers
Bad: $ cat orange pear > orange
cat: input orange is output
– see noclobber in C Shell
Good: $ cat orange pear > temp
$ mv temp orange
CIS 218 Advanced UNIX 10
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
$
CIS 218 Advanced UNIX 11
command1
standard
input
standard
output
command2
2. Pipes
Use a pipe to connect standard output of
one command to standard input of another:
CIS 218 Advanced UNIX 12
Use the ‘|’ operator between commands:
$ command 1 | command2
Same as:
$ command1 > temp
$ command2 < temp
$ rm temp
CIS 218 Advanced UNIX 13
$ ls | more
$ who | grep ‘ad’
ad tty06 May 23 10:31
$ 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 or $ sort temp
CIS 218 Advanced UNIX 14
Filters
A filter is a command that modifies its
standard input, putting the changes onto its
standard output:
$ who | sort | lpr
$ ps | grep ad
CIS 218 Advanced UNIX 15
The tee Command
Passes its input through to standard output
unchanged. Also saves input into a file:
command1
standard
input
standard
output
command2 tee
file
CIS 218 Advanced UNIX 16
$ 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
CIS 218 Advanced UNIX 17
3. Background Jobs
A normal command executes in the
foreground: you wait until it finishes
before another command can be typed.
Commands (jobs) can execute in the
background. No need to wait for them
before another command is typed.
CIS 218 Advanced UNIX 18
Background jobs end with a ‘&’:
$ gcc big-program.c &
1466
$ ls -l | lpr &
1467
$ vi report.txt
CIS 218 Advanced UNIX 19
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
$
CIS 218 Advanced UNIX 20
4. Filename Generation
Commands involving filenames (e.g. cat,
ls) can include special characters in the
filenames.
– called metacharacters
– three kinds:
?
*
[...]
CIS 218 Advanced UNIX 21
The ? Special Character
? matches any single character
$ ls
mem memo12 memo9 memoalex newmemo5
memo memo5 memoa memos
$ ls memo?
memo9 memo5 memoa memos
$ lpr memo?
continued
CIS 218 Advanced UNIX 22
$ ls
7may4report may14report may4report.79
mayqreport may.report may4report
may_report mayreport
$ ls may?report
mayqreport may.report may4report
may_report
CIS 218 Advanced UNIX 23
The * Special Character
* 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
CIS 218 Advanced UNIX 24
$ ls *.txt
$ lpr *.txt
$ ls *.c
$ cat *.c > all-files
$ more all-files
$ rm *.c
$ mv all-files all-files.c
CIS 218 Advanced UNIX 25
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
CIS 218 Advanced UNIX 26
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
CIS 218 Advanced UNIX 27
$ ls
part0 part1 part2 part3 part4 ...
part32 part33 part34 part35
$ ls part[0-9]
$ ls part[12][0-9]
$ ls part3[0-5]
CIS 218 Advanced UNIX 28
Combining Special Characters
$ ls [a-m]*
$ ls *[x-z]
$ lpr p*[0-9].c &