Professional Documents
Culture Documents
Learning Objectives:
1.
2.
3.
4.
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
Options sometimes also take a value. The value can usually be either given right after the option or separately:
$ ypcat -d ug.cs.ust.hk passwd
Spaces separate options. To turn something with spaces into a single argument, use quotes:
$ grep some pizza letter1 grep: cant open pizza
letter1:It is getting late. Please order some pizza and stop
$ grep some pizza letter1 It is getting late. Please order some pizza and stop
To escape a single character (prevent it from being treated specially) proceed it with a backslash:
$ grep We\ll letter1 by my office. We'll tidy up a few more things before $ echo * * $ echo * * $ echo \* * $ echo * letter1 names
secret/
If there is a match in more than one directory, the shell uses the first one it finds. If you want to run a command that is not in one of these directories, you can give a pathname (relative or absolute) instead.
~horner/bin/csound
Alias
The C Shell has the alias command, which allows you to create command shortcuts.
$ alias dir "ls -F" $ alias rm "rm -i" $ alias + "chmod u+x *" $ 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.
These devices (pts/3) are actually files in the directory /dev. So, if you are logged in on pts/3, this works just fine (otherwise Permission denied):
$ date > /dev/pts/3 Thu Feb 11 17:08:21 HKT 1999
You can find out which terminal a particular shell is connected to using the tty command:
$ tty /dev/pts/3 $ echo $
"Hi
Tee
A special command called tee acts like a T-joint in plumbing:
$ who horner pts/3 Feb 11 10:23 (csnt1.cs.ust.hk) horner pts/0 Feb 11 11:57 (csz096.cs.ust.hk) $ who | sort | tee sortedwho | wc -l
2
$ cat sortedwho horner pts/0 Feb 11 11:57 horner pts/3 Feb 11 10:23 $ (csz096.cs.ust.hk) (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.
The shell will print the process ID (PID), and a job number (1, in this case). In some shells, you will be notified when the job is done (you may have to hit return again):
$ sort names > names.sort & [1] 3236 $ [1] Done sort names > names.sort
The job is suspended - not running - until you either place it in the background using bg:
$ bg [1] $ ypcat passwd | sort > passwd.sort &
Jobs (1)
The jobs command tells you what jobs are running:
$ ypcat passwd | sort > passwd.sort ^Z Suspended $ jobs [1] + Suspended ypcat passwd | sort > passwd.sort $
The %1 means job #1. You can also use the PID.
Jobs (2)
The ps command is the main way to find out about jobs:
$ ps PID TTY TIME CMD 1401 pts/0 0:01 csh $ $ ypcat passwd | sort > passwd.sort & [1] 3476 3477 $ ps PID TTY TIME CMD 3477 pts/0 0:00 sort 1401 pts/0 0:01 csh 3476 pts/0 0:01 ypcat $
Jobs (3)
Note that if you put something into the background, you better redirect stdout, or the output will appear on your screen anyway!
$ ypcat passwd &
ma_wmkaa:uq2jXK0sFQ8Jg:36747:5000:Woo Man Kei,,=EXP.2001.05.30=nc99S:/homes/ma_h ma_chyaa:CS9wq.1zOxnhI:35435:5000:Chu How Yin Agnes,,=EXP.2001.05.30=nc98F:/homs ee_tkcaa:9LtI7Tipk2Ca6:35651:5000:Tsang Kong Chau,,=EXP.2001.05.30=nc99S:/homesh eg_cck:yi7XtKxxP5KaQ:43555:10010:Cheung Chi Keung,,ce98_yr1:/homes/eg_cck:/bin/h cs_wks:dtjvwifI2G7v2:24514:10001:Wong Kin Shing,,cs98_yr1:/homes/cs_wks:/bin/tch cs_lwk:OWiGoJRXSjn.s:24032:10001:Leung Wai Kei Ricky,,cs98_yr2:/homes/cs_lwk:/bh ph_chyac:CSJUo9e2KGqKg:35955:5000:Chan Hoi Yan,,=EXP.2001.05.30=nc98F:/homes/phs ee_wkkab:dfbi3GqWjvf5U:35644:5000:Wong Ka Keung,,=EXP.2001.05.30=nc99S:/homes/eh ph_lcy:CSwGgr5IeIvqc:36689:5000:Lam Chi Yin,,=EXP.2001.05.30=nc99S:/homes/ph_lcx ^C
Switching Shells
You can switch shells by just typing its name:
csl3su1.cs.ust.hk> ps PID TTY TIME CMD 3496 pts/0 0:01 csh csl3su1.cs.ust.hk> tcsh csl3su1.cs.ust.hk> ps PID TTY TIME CMD 3650 pts/0 0:00 tcsh 3496 pts/0 0:01 csh csl3su1.cs.ust.hk> sh $ ps PID TTY TIME CMD 3650 pts/0 0:00 tcsh 3496 pts/0 0:01 csh 3659 pts/0 0:00 sh $ ^D csl3su1.cs.ust.hk> ps PID TTY TIME CMD 3650 pts/0 0:00 tcsh 3496 pts/0 0:01 csh
(csnt1.cs.ust.hk) (csz096.cs.ust.hk)
To create a single command out of a group of commands (especially useful before a pipe):
$ (cat letter1; head -2 names) | sort >list
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.
The notation [a-z] matches any lowercase letter. The notation [0-9] matches any digit character. The notation [0-59] matches any the digit characters 0, 1, 2, 3, 4, 5, and 9.
$ ls letter* letter1 letter4 $ ls letter[0-35] letter1 $ ls letter[0-24] letter1 letter4
[aeiou0-9] [^aeiou0-9]
match a,e,i,o,u, and 0 thru 9 match anything but a,e,i,o,u, and 0 thru 9