Title: The UNIX Shell
1The UNIX Shell
- Learning Objectives
- To give detailed description for using commands
in Unix Shell - To introduce more advanced techniques for
handling complicate commands in Unix Shell - To understand the the usage of background jobs
shell switching - To learn more about pattern matching
2How Does the Shell Find a Command?
- The shell searches a list of directories for an
executable file with the same name. - echo path shows you the current path
- The list of directories is stored in the PATH
variable for Bourne shells and in the path array
for csh/tcsh - PATH/usr/local/binPATH sh
- set path(/usr/local/bin path) csh, tcsh
- If there is a match in more than one directory,
the shell uses the first one it finds. - It is usually set in your .cshrc file in your
home directory
3Alias
- The C Shell has the alias command, which allows
you to create command shortcuts. - alias dir "ls -F"
- alias rm "rm -i"
- alias "chmod ux "
- alias - "chmod u-x "
- alias 111 "cd horner/111"
- pwd
- /bin
- 111
- pwd
- /homes/horner/111
- If you put the alias commands in your .cshrc
file, you can use them every time you login.
4Tee
- A special command called tee acts like a T-joint
in plumbing - who
- horner pts/3 Feb 11 1023 (csnt1.cs.ust.hk)
- horner pts/0 Feb 11 1157
(csz096.cs.ust.hk) - who sort tee sortedwho wc -l
- 2
- cat sortedwho
- horner pts/0 Feb 11 1157
(csz096.cs.ust.hk) - horner pts/3 Feb 11 1023 (csnt1.cs.ust.hk)
-
- In this example, the output of sort is placed
in a file sortedwho and piped to wc -l, which
counts the number of lines.
5Background Jobs
- A simple command or pipeline can be put into the
background by following it with the
character - sort names gt names.sort
- 1 3236
- The shell will print the process ID (PID), and a
job number (1, in this case). - Put a job in the background by typing CTRL-Z
- ypcat passwd sort gtpasswd.sort
- Z
- Suspended
- The job is suspended - not running - until you
either place it in the background using bg - bg
- 1 ypcat passwd sort gt passwd.sort
- or back to the foreground using fg
- fg
- ypcat passwd sort gtpasswd.sort
6Jobs
- You can stop a job with the kill command
- ypcat passwd sort gt passwd.sort
- 1 3414 3415
- kill 1
- 1 Terminated ypcat passwd
- Exit 2 sort gt passwd.sort
- The 1 means job 1. You can also use the
PID. - The ps command is the main way to find out about
jobs - ypcat passwd sort gt passwd.sort
- 1 3476 3477
- ps
- PID TTY TIME CMD
- 3477 pts/0 000 sort
- 1401 pts/0 001 csh
- 3476 pts/0 001 ypcat
-
7Combining Commands (1)
- Multiple pipelines can be input on one command
line by separating them with semicolons. - When entering a long command, use a backslash (\)
to continue the command on the next line. - date sort names \
- who
- Thu Feb 11 194028 HKT 1999
- Bill Clinton
- Bill Clinton
- Bill Gates
- Bill Gates
- Monica Lewinski
- horner pts/3 Feb 11 1023
(csnt1.cs.ust.hk) - horner pts/0 Feb 11 1911
(csz096.cs.ust.hk)
8Combining Commands (2)
- Commands can be grouped together using
parentheses - There are two main reasons to group commands
- To create a single command out of a group of
commands (especially useful before a pipe) - (cat names head -2 names) wc l
- if names has 8 lines, what does this line
return? - To run a set of commands in their own subshell
(especially when trying to limit the effect of a
cd command) - (cd secret ls wc -l) ls wc -l
- 3
- 25
- This line has the effect of counting the files in
secret, and then counting the files in the
current directory. - Question what does this do?
- cd cd 111 (cd ..cd 122) cd ..
9cut and tr
- cut -cx-y cuts characters from position x to
position y (the first position is 1). - 1 2 3 4
5 6 - 1234567890123456789012345678901234567890123456789
01234567890123 - -r--r--r-- 1 horner cs 155 Feb 12
1600 letter1 - What does this do?
- ls -l sort 4 tail -1 cut -c55-70
- The following command returns the user name
- who am i cut -f1 -d'
- -f1 specifies the first field
- Field is defined by the delimiter
- The following command translates all lower case
to upper case - tr a-z A-Z filename
- tr means translate
- To compress all spaces tr -s ' ' '
- What does this do?
- Who tr s cut d f2
10Patterns
- The pattern abcd matches any single one of
the enclosed characters. - The following ls command lists any file whose
name start with i or l followed by any - other characters
- ls il
- it it1 ith its_at_ letter1
letter4 -
- matches anything
- The notation a-z matches any lowercase letter
once. - The notation 0-9 matches any digit character
once. - The notation 0-59 matches any the digit
characters 0, 1, 2, 3, 4, 5, and 9. - ls letter
- letter1 letter4
- ls letter0-35
- letter1
- ls letter0-24
- letter1 letter4
11More Pattern Matching grep (1)
- grep finds all matches of a pattern in a file
- To build a pattern, use the following
- start of line end of line
- . any character \ quote next character
- match zero or more match one or more
- ? match zero or one
- aeiou0-9 match a,e,i,o,u, and 0 thru 9
- aeiou0-9 match anything but a,e,i,o,u, and
0 thru 9 - a match zero or more occurrences of a
- a match one or more occurrences of a
(egrep) -
12More Pattern Matching grep (2)
- cat names
- Bill Gates
- Jacky Gs
- James Bill
- J. Jones
- grep 'Bill names
- Bill Gates
- grep 'Bill names
- James Bill
- grep J...s names
- James Bill
- J. Jones
grep 'G.s' names Bill Gates Jacky Gs grep
'G.s' names egrep 'G.s' names Bill Gates
egrep 'G.s' names egrep 'G.?s' names J.
Jones grep 'J\.' names J. Jones
Some versions of grep cannot process ?
13More Pattern Matching grep (3)
- cat names
- Bill Gates
- Jacky Gs
- James Bill
- J. Jones
- grep 'Jacm' names
- Jacky Gs
- James Bill
- grep 'Ja' names
- J. Jones
- grep t-y' names
- Bill Gates
- Jacky Gs
grep -in bill names 1Bill Gates 3James
Bill grep -v Bill names Jacky Gs J. Jones
grep -l Bill n names grep -c Bill names 2
-i ignores case -n shows line number
-v means line not containing pattern
-l shows filename containing pattern
-c shows matching count
14How to do this?
- Find all lines in file1 that does not contain
any vowels - Hint use v option
- Find all lines in file1 that contains only
vowels. - Hint use string
- Find all lines in file1 with length 7 and a as
the fourth character - Find all lines that contain a, c, and e, in
that order