Advanced UNIX (Shell) : Objectives
Advanced UNIX (Shell) : Objectives
Objectives
to supplement the “Introduction to UNIX” slide
s with extra information about the Shell
Overview
1. Redirection
2. Pipes
3. Background Jobs
4. Filename Generation
1. Redirection
Command I/O is stream-based:
standard
output
command
standard input
You type a line;
it is echoed
$ 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
Redirect Output
Use > to redirect standard output t
o a ‘file’:
standard
output file
command
standard input
$ 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 file1.c file2.c file3.c > all-files.c
Redirect Input
Use < to redirect standard input fro
m a ‘file’:
standard standard
input output
file command
$ cat < supply_orders
2000 sheets letterhead ordered: 10/7/97
1 box masking tape ordered: 10/8/97
$
$ cat supply_orders
Good:
$ cat orange pear > temp
$ mv temp orange
Appending Output to a File
Use >> to append:
standard standard
input output
Use the ‘|’ operator between commands:
$ command 1 | command2
Same as:
$ command1 > temp
$ command2 < temp
$ rm temp
$ 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
Filters
A filter is a command that modifies
its standard input, putting the chan
ges onto its standard output:
$ ps | grep ad
The tee Command
Passes its input through to standard output u
nchanged. Also saves input into a file:
file
command1 tee command2
standard standard
input output
$ 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
3. Background Jobs
A normal command executes in the foregroun
d: you wait until it finishes before another com
mand can be typed.
Commands (jobs) can execute in the backgro
und. No need to wait for them before another
command is typed.
Background jobs end with a ‘&’:
$ ls -l | lpr &
1467
$ vi report.txt
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
$
4. Filename Generation
Commands involving filenames (e.g. cat, ls) c
an include special characters in the filenames.
called metacharacters
three kinds:
?
*
[...]
The ? Special Character
? matches any single character
$ ls
mem memo12 memo9 memoalex newme
mo5
memo memo5 memoa memos
$ ls memo?
memo9 memo5 memoa memos
$ lpr memo?
continued
$ ls
7may4report may14report may4report.
79
mayqreport may.report may4report
may_report mayreport
$ ls may?report
mayqreport may.report may4report
may_report
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
$ ls *.txt
$ lpr *.txt
$ ls *.c
$ cat *.c > all-files
$ more all-files
$ rm *.c
$ mv all-files all-files.c
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
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
$ ls
part0 part1 part2 part3 part4 ...
part32 part33 part34 part35
$ ls part[0-9]
$ ls part[12][0-9]
$ ls part3[0-5]
Combining Special Characters
$ ls [a-m]*
$ ls *[x-z]