01c shell

300 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
300
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

01c shell

  1. 1. The UNIX Shell COMP 2021
  2. 2. Basic Shell Syntax <ul><li>command [-[options]] [arg] [arg] … </li></ul><ul><li>The name of the command is first </li></ul><ul><li>Options are normally single letters that turn an option on or off. They can be combined or given separately. </li></ul><ul><li>$ ls -dil </li></ul><ul><li>$ ls -l -d -i </li></ul><ul><li>Options sometimes also take a value. The value can usually be either given right after the option or separately: </li></ul><ul><li> $ ypcat -d ug.cs.ust.hk passwd </li></ul>
  3. 3. Command Options <ul><li>Most commands require you to give all options before filename arguments. The following command works in Linux, but not SunOS: </li></ul><ul><li>$ cat names -n </li></ul><ul><li>George W. Bush </li></ul><ul><li>Bill Gates </li></ul><ul><li>Bill Gates </li></ul><ul><li>Bill Clinton </li></ul><ul><li>George W. Bush </li></ul><ul><li>cat: cannot open –n </li></ul><ul><li>Spaces separate options. To turn something with spaces into a single argument, use quotes: </li></ul><ul><li>$ grep vote thing letter1 </li></ul><ul><li>grep: can’t open thing </li></ul><ul><li>letter1:You have the Florida vote thing </li></ul><ul><li>$ grep ”vote thing” letter1 </li></ul><ul><li>You have the Florida vote thing </li></ul>
  4. 4. Command Options <ul><li>Double quotes and single quotes are a bit different. For now, you can use them interchangeably. </li></ul><ul><li>$ grep ‘vote thing’ letter1 </li></ul><ul><li> You have the Florida vote thing </li></ul><ul><li>To escape a single character (prevent it from being treated specially) proceed it with a backslash: </li></ul><ul><li>$ grep ”We’ll” letter2 </li></ul><ul><li>by my office. We'll tidy up a few more things before </li></ul><ul><li>$ echo ”*” </li></ul><ul><li>* </li></ul><ul><li>$ echo ‘*’ </li></ul><ul><li>* </li></ul><ul><li>$ echo * </li></ul><ul><li>* </li></ul><ul><li>$ echo * </li></ul><ul><li>letter1 letter2 names secret/ </li></ul>
  5. 5. How Does the Shell Find a Command ? <ul><li>The shell searches a list of directories for an executable file with the same name. </li></ul><ul><li>The list of directories is stored in the PATH variable for Bourne shells and in the path array for csh/tcsh </li></ul><ul><li>$ PATH=/usr/local/bin:$PATH sh </li></ul><ul><li>% set path=(/usr/local/bin $path) csh, tcsh </li></ul><ul><li>If there is a match in more than one directory, the shell uses the first one it finds. </li></ul><ul><li>If you want to run a command that is not in one of these directories, you can give a pathname (relative or absolute) instead. </li></ul><ul><li>~horner/bin/csound </li></ul>
  6. 6. How Does the Shell Find a Command ? <ul><li>A few commands are built into the shell. This varies from shell to shell. The echo command, for example, is often builtin, for efficiency. </li></ul><ul><li>You can find out where the shell is getting a particular command using the “ which ” command in any shell: </li></ul><ul><li>$ which echo </li></ul><ul><li>echo: shell built-in command. </li></ul><ul><li>$ which cat </li></ul><ul><li>/bin/cat </li></ul><ul><li>$ which grep </li></ul><ul><li>/bin/grep </li></ul><ul><li>$ which ls </li></ul><ul><li>ls: aliased to ls --color=tty </li></ul>Makes directories blue, executables green, and soft links aqua
  7. 7. Alias <ul><li>The C Shell has the alias command, which allows you to create command shortcuts. </li></ul><ul><li>$ alias ls &quot;ls -F&quot; </li></ul><ul><li>$ alias rm &quot;rm -i&quot; </li></ul><ul><li>$ alias + &quot;chmod u+x *&quot; </li></ul><ul><li>$ alias - &quot;chmod u-x *&quot; </li></ul><ul><li>$ alias 2021 &quot;cd ~horner/2021&quot; </li></ul><ul><li>$ pwd </li></ul><ul><li>/bin </li></ul><ul><li>$ 2021 </li></ul><ul><li>$ pwd </li></ul><ul><li>/homes/horner/2021 </li></ul><ul><li>On most Unix machines (except Mandriva/Mandrake in CSLab2), if you put the alias commands in your .cshrc file, you can use them every time you login. </li></ul>
  8. 8. Standard Input <ul><li>Every time you login, or run a shell, you are “connected” to the computer on a particular terminal. </li></ul><ul><li>$ who </li></ul><ul><li>horner pts/3 Sep 12 10:23 (csnt1.cs.ust.hk) </li></ul><ul><li>horner pts/0 Sep 12 11:57 (csz096.cs.ust.hk) </li></ul><ul><li>These devices ( pts/0 ) are actually files in the directory /dev . So, if you are logged in on pts/0 , this works just fine: </li></ul><ul><li>$ date > /dev/pts/0 </li></ul><ul><li>Mon Sep 12 17:08:21 HKT 2011 </li></ul>
  9. 9. Standard Input <ul><li>In fact, you can redirect stdout to a different device (e.g., pts/3 ), if you have permission. (The write command works this way.) </li></ul><ul><li>You can find out which terminal a particular shell is connected to using the tty command: </li></ul><ul><ul><ul><li>$ tty </li></ul></ul></ul><ul><ul><ul><li>/dev/pts/0 </li></ul></ul></ul><ul><ul><ul><li>$ echo &quot; Hi Andrew! &quot; > /dev/pts/3 </li></ul></ul></ul><ul><li>$ </li></ul>
  10. 10. Tee <ul><li>A special command called tee acts like a T-joint in plumbing: </li></ul><ul><li>$ who </li></ul><ul><li>horner pts/3 Feb 14 10:23 (csnt1.cs.ust.hk) </li></ul><ul><li>horner pts/0 Feb 14 11:57 (csz096.cs.ust.hk) </li></ul><ul><ul><ul><li>$ who | sort | tee sortedwho | wc -l </li></ul></ul></ul><ul><ul><ul><li>2 </li></ul></ul></ul><ul><ul><ul><li>$ cat sortedwho </li></ul></ul></ul><ul><li>horner pts/0 Feb 14 11:57 (csz096.cs.ust.hk) </li></ul><ul><li>horner pts/3 Feb 14 10:23 (csnt1.cs.ust.hk) </li></ul><ul><ul><ul><li>$ </li></ul></ul></ul><ul><li>In this example, the output of sort is placed in a file “ sortedwho “ and piped to wc -l , which counts the number of lines. </li></ul>
  11. 11. Background Jobs <ul><li>A simple command or pipeline can be put into the background by following it with the “&” character: </li></ul><ul><li>$ sort names > names.sort & </li></ul><ul><li>[1] 3236 </li></ul><ul><li>$ </li></ul><ul><li>The shell will print the process ID (PID), and a job number (1, in this case). </li></ul><ul><li>In some shells, you will be notified when the job is done (you may have to hit return again): </li></ul><ul><li>$ sort names > names.sort & </li></ul><ul><li>[1] 3236 </li></ul><ul><li>$ </li></ul><ul><li>[1] Done sort names > names.sort </li></ul><ul><li>$ </li></ul>
  12. 12. Background Jobs <ul><li>Put a job in the background by typing “CTRL-Z” </li></ul><ul><li>$ ypcat passwd | sort >passwd.sort </li></ul><ul><li>^Z </li></ul><ul><li>Suspended </li></ul><ul><li>$ </li></ul><ul><li>The job is suspended - not running - until you either place it in the background using bg : </li></ul><ul><li>$ bg </li></ul><ul><li>[1] ypcat passwd | sort > passwd.sort & </li></ul><ul><li>$ </li></ul><ul><li>or back to the foreground using fg : </li></ul><ul><li>$ fg </li></ul><ul><li>ypcat passwd | sort >passwd.sort </li></ul><ul><li>$ </li></ul>
  13. 13. Jobs <ul><li>The jobs command tells you what jobs are running: </li></ul><ul><li>$ ypcat passwd | sort > passwd.sort </li></ul><ul><li>^Z </li></ul><ul><li>Suspended </li></ul><ul><li>$ jobs </li></ul><ul><li>[1] + Suspended ypcat passwd | sort > passwd.sort </li></ul><ul><li>$ </li></ul><ul><li>You can stop a job with the kill command: </li></ul><ul><li>$ ypcat passwd | sort > passwd.sort & </li></ul><ul><li>[1] 3414 3415 </li></ul><ul><li>$ kill %1 </li></ul><ul><li>$ </li></ul><ul><li>[1] Terminated ypcat passwd | </li></ul><ul><ul><li>Exit 2 sort > passwd.sort </li></ul></ul><ul><li>$ </li></ul><ul><ul><li>The “%1” means “job #1”. You can also use the PID. </li></ul></ul>
  14. 14. Jobs <ul><li>The ps command is the main way to find out about jobs: </li></ul><ul><li>$ ps </li></ul><ul><li>PID TTY TIME CMD </li></ul><ul><li>1401 pts/0 0:01 csh </li></ul><ul><li>$ </li></ul><ul><li>$ ypcat passwd | sort > passwd.sort & </li></ul><ul><li>[1] 3476 3477 </li></ul><ul><li>$ ps </li></ul><ul><li>PID TTY TIME CMD </li></ul><ul><li>3477 pts/0 0:00 sort </li></ul><ul><li>1401 pts/0 0:01 csh </li></ul><ul><li>3476 pts/0 0:01 ypcat </li></ul><ul><li>$ </li></ul>
  15. 15. Jobs <ul><li>Note that if you put something into the background, you better redirect stdout, or the output will appear on your screen anyway! </li></ul><ul><li>$ ypcat passwd & </li></ul><ul><li>ma_wmkaa:uq2jXK0sFQ8Jg:36747:5000:Woo Man Kei,,=EXP.2001.05.30=nc99S:/homes/ma_h </li></ul><ul><li>ma_chyaa:CS9wq.1zOxnhI:35435:5000:Chu How Yin Agnes,,=EXP.2001.05.30=nc98F:/homs </li></ul><ul><li>ee_tkcaa:9LtI7Tipk2Ca6:35651:5000:Tsang Kong Chau,,=EXP.2001.05.30=nc99S:/homesh </li></ul><ul><li>eg_cck:yi7XtKxxP5KaQ:43555:10010:Cheung Chi Keung,,ce98_yr1:/homes/eg_cck:/bin/h </li></ul><ul><li>cs_wks:dtjvwifI2G7v2:24514:10001:Wong Kin Shing,,cs98_yr1:/homes/cs_wks:/bin/tch </li></ul><ul><li>cs_lwk:OWiGoJRXSjn.s:24032:10001:Leung Wai Kei Ricky,,cs98_yr2:/homes/cs_lwk:/bh </li></ul><ul><li>ph_chyac:CSJUo9e2KGqKg:35955:5000:Chan Hoi Yan,,=EXP.2001.05.30=nc98F:/homes/phs </li></ul><ul><li>ee_wkkab:dfbi3GqWjvf5U:35644:5000:Wong Ka Keung,,=EXP.2001.05.30=nc99S:/homes/eh </li></ul><ul><li>ph_lcy:CSwGgr5IeIvqc:36689:5000:Lam Chi Yin,,=EXP.2001.05.30=nc99S:/homes/ph_lcx </li></ul><ul><li>^C </li></ul><ul><li>$ ypcat passwd >file & </li></ul><ul><li>$ </li></ul>
  16. 16. More Pattern Matching <ul><li>The notation “[abcd]” matches any single one of the enclosed characters. </li></ul><ul><ul><ul><li>$ ls [il]* </li></ul></ul></ul><ul><ul><ul><li>it it1 ith its@ letter1 letter4 </li></ul></ul></ul><ul><ul><ul><li>$ </li></ul></ul></ul><ul><li>The notation “[a-z]” matches any lowercase letter. </li></ul><ul><li>The notation “[0-9]” matches any digit character. </li></ul><ul><li>The notation “[0-59]” matches any the digit characters 0, 1, 2, 3, 4, 5, and 9. </li></ul><ul><ul><ul><li>$ ls letter* </li></ul></ul></ul><ul><ul><ul><li>letter1 letter4 </li></ul></ul></ul><ul><ul><ul><li>$ ls letter[0-35] </li></ul></ul></ul><ul><ul><ul><li>letter1 </li></ul></ul></ul><ul><ul><ul><li>$ ls letter[0-24] </li></ul></ul></ul><ul><ul><ul><li>letter1 letter4 </li></ul></ul></ul>
  17. 17. More Pattern Matching <ul><li>Most shells allow you to give a list of strings in curly brackets, comma separated: </li></ul><ul><ul><ul><li>$ ls *{1,.sort} </li></ul></ul></ul><ul><ul><ul><li>NAMES1 it1 names.sort s1@ </li></ul></ul></ul><ul><ul><ul><li>f1 letter1 passwd.sort </li></ul></ul></ul><ul><ul><ul><li>secret1: </li></ul></ul></ul><ul><ul><ul><li>secret1: Permission denied </li></ul></ul></ul><ul><ul><ul><li>$ </li></ul></ul></ul>
  18. 18. Switching Shells <ul><li>You can switch shells by just typing its name: </li></ul><ul><ul><li>csl2wk01.cs.ust.hk> ps </li></ul></ul><ul><ul><li>PID TTY TIME CMD </li></ul></ul><ul><ul><li>3496 pts/0 0:01 csh </li></ul></ul><ul><ul><li>csl2wk01.cs.ust.hk> tcsh </li></ul></ul><ul><ul><li>csl2wk01.cs.ust.hk> ps </li></ul></ul><ul><ul><li>PID TTY TIME CMD </li></ul></ul><ul><ul><li>3650 pts/0 0:00 tcsh </li></ul></ul><ul><ul><li>3496 pts/0 0:01 csh </li></ul></ul><ul><ul><li>csl2wk01.cs.ust.hk> sh </li></ul></ul><ul><ul><li>$ ps </li></ul></ul><ul><ul><li>PID TTY TIME CMD </li></ul></ul><ul><ul><li>3650 pts/0 0:00 tcsh </li></ul></ul><ul><ul><li>3496 pts/0 0:01 csh </li></ul></ul><ul><ul><li>3659 pts/0 0:00 sh </li></ul></ul><ul><ul><li>$ ^D </li></ul></ul><ul><ul><li>csl2wk01.cs.ust.hk> ps </li></ul></ul><ul><ul><li>PID TTY TIME CMD </li></ul></ul><ul><ul><li>3650 pts/0 0:00 tcsh </li></ul></ul><ul><ul><li>3496 pts/0 0:01 csh </li></ul></ul>
  19. 19. Combining Commands <ul><li>Multiple pipelines can be input on one command line by separating them with semicolons. </li></ul><ul><li>When entering a long command, use a backslash () to continue the command on the next line. </li></ul><ul><ul><li>$ date; sort names; </li></ul></ul><ul><ul><li>who </li></ul></ul><ul><ul><li>Mon Feb 14 19:40:28 HKT 2005 </li></ul></ul><ul><li>Bill Clinton </li></ul><ul><li>Bill Gates </li></ul><ul><li>Bill Gates </li></ul><ul><li>George W. Bush </li></ul><ul><li>George W. Bush </li></ul><ul><li>horner pts/3 Sep 12 10:23 (csnt1.cs.ust.hk) </li></ul><ul><li>horner pts/0 Sep 12 19:11 (csz096.cs.ust.hk) </li></ul>
  20. 20. Combining Commands <ul><li>Commands can be grouped together using parentheses </li></ul><ul><li>There are two main reasons to group commands: </li></ul><ul><ul><li>To create a “single command” out of a group of commands (especially useful before a pipe): </li></ul></ul><ul><ul><li> $ (cat letter1; head -2 names) | sort >list </li></ul></ul><ul><ul><li>To run a set of commands in their own subshell (especially when trying to limit the effect of a cd command): </li></ul></ul><ul><ul><ul><li>$ (cd secret; ls | wc -l); ls | wc -l </li></ul></ul></ul><ul><ul><ul><li> 3 </li></ul></ul></ul><ul><ul><ul><li> 25 </li></ul></ul></ul><ul><ul><ul><li>This line has the effect of counting the files </li></ul></ul></ul><ul><ul><ul><li>in secret, and then counting the files in the </li></ul></ul></ul><ul><ul><ul><li>current directory. </li></ul></ul></ul>

×