+ All Categories
Home > Documents > shell.ppt

shell.ppt

Date post: 14-Apr-2018
Category:
Upload: subhrajitm47
View: 221 times
Download: 0 times
Share this document with a friend

of 24

Transcript
  • 7/30/2019 shell.ppt

    1/24

    (update Dickson Chiu, Spr ing 2001)

    The UNIX Shell

    Learning Objectives:

    1. To give detailed description for usingcommands in Unix Shell

    2. To introduce more advanced techniques for

    handling complicate commands in Unix Shell

    3. To understand the the usage of backgroundjobs & shell switching

    4. To learn more about pattern matching

  • 7/30/2019 shell.ppt

    2/24

    COMP111

    Lecture 5 / Slide 2

    The UNIX Shell

    Table of Content

    Basic Shell Syntax

    Command options

    How Does the Shell find a Command

    Alias Standard Input

    Tee

    Background Jobs

    Jobs

    Switching Shells

    Combining Commands

    More Pattern Matching

    More Pattern Matching (grep)

  • 7/30/2019 shell.ppt

    3/24

    COMP111

    Lecture 5 / Slide 3

    Basic Shell Syntax

    command[-[options]] [arg] [arg] The name of the command is first

    Options are normally single letters that turn an option

    on or off. They can be combined or given separately.

    $ ls -dil

    $ ls -l -d -i

    Options sometimes also take a value. The value can

    usually be either given right after the option orseparately:

    $ ypcat -d ug.cs.ust.hk passwd

  • 7/30/2019 shell.ppt

    4/24

    COMP111

    Lecture 5 / Slide 4

    Command Options (1)

    Most commands require you to give all options beforefilename arguments

    $ cat names -n

    Bill Clinton

    Bill Gates

    Bill GatesBill Clinton

    Monica Lewinski

    cat: cannot open -n

    Spaces separate options. To turn something with spaces intoa single argument, use quotes:

    $ grep some pizza letter1

    grep: cant open pizzaletter1:It is getting late. Please order some pizza and stop

    $ grep some pizza letter1

    It is getting late. Please order some pizza and stop

  • 7/30/2019 shell.ppt

    5/24

    COMP111

    Lecture 5 / Slide 5

    Command Options (2)

    Double quotes and single quotes are a bit different (say,when shell variables are involved), but you may use theminterchangeably here.

    $ 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/

  • 7/30/2019 shell.ppt

    6/24

    COMP111

    Lecture 5 / Slide 6

    How Does the Shell Find a

    Command? (1) The shell searches a list of directories for an executable file

    with the same name.

    The list of directories is stored in the PATH variable for

    Bourne shells and in the path array forcsh/tcsh$ PATH=/usr/local/bin:$PATH 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. 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

  • 7/30/2019 shell.ppt

    7/24

    COMP111

    Lecture 5 / Slide 7

    How Does the Shell Find a

    Command? (2)

    A few commands are built into the shell. This varies fromshell to shell. The echo command, for example, is often

    builtin, for efficiency. You can find out where the shell is getting a particular

    command using the which command in any shell:$ which echo

    echo: shell built-in command.

    $ which cat

    /usr/bin/cat

    $ which grep

    /usr/bin/grep

    $ which ls

    ls: aliased to ls -F

    Gives / after directories,

    * after executables, and

    @ after soft links

  • 7/30/2019 shell.ppt

    8/24

    COMP111

    Lecture 5 / Slide 8

    Alias

    The C Shell has the alias command, which allows you tocreate 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 canuse them every time you login.

  • 7/30/2019 shell.ppt

    9/24

    COMP111

    Lecture 5 / Slide 9

    Standard Input (1)

    Every time you login, or run a shell, you are connected tothe computer on a particular terminal.

    $ who

    horner pts/3 Feb 11 10:23 (csnt1.cs.ust.hk)

    horner pts/18 Feb 11 11:57 (csz096.cs.ust.hk)

    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

    COMP111

  • 7/30/2019 shell.ppt

    10/24

    COMP111

    Lecture 5 / Slide 10

    Standard Input (2)

    In fact, you can redirect stdout to a different device (e.g.,pts/18), if you have permission. (The write command

    works this way.)

    You can find out which terminal a particular shell is

    connected to using the tty command:$ tty

    /dev/pts/3

    $ echo "Hi Andrew!" > /dev/pts/18

    $

    COMP111

  • 7/30/2019 shell.ppt

    11/24

    COMP111

    Lecture 5 / Slide 11

    Tee

    A special command called tee acts like a T-joint inplumbing:

    $ 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 (csz096.cs.ust.hk)

    horner pts/3 Feb 11 10:23 (csnt1.cs.ust.hk)

    $

    In this example, the output of sort is placed in a filesortedwho and piped to wc -l, which counts thenumber of lines.

    COMP111

  • 7/30/2019 shell.ppt

    12/24

    COMP111

    Lecture 5 / Slide 12

    Background Jobs (1)

    A simple command or pipeline can be put into the background by

    following it with the & character:$ sort names > names.sort &

    [1] 3236

    $

    The shell will print the process ID (PID), and ajob 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

    $

    COMP111

  • 7/30/2019 shell.ppt

    13/24

    COMP111

    Lecture 5 / Slide 13

    Background Jobs (2)

    Put a job in the background by typing CTRL-Z$ ypcat passwd | sort >passwd.sort

    ^Z

    Suspended

    $

    The job is suspended - not running - until you either place itin the background using bg:

    $ bg

    [1] ypcat passwd | sort > passwd.sort &

    $or back to the foreground using fg:

    $ fg

    ypcat passwd | sort >passwd.sort

    $

    COMP111

  • 7/30/2019 shell.ppt

    14/24

    COMP111

    Lecture 5 / Slide 14

    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$

    You can stop a job with the kill command:$ ypcat passwd | sort > passwd.sort &

    [1] 3414 3415

    $ kill %1$

    [1] Terminated ypcat passwd |

    Exit 2 sort > passwd.sort

    $

    The %1 means job #1. You can also use the PID.

    COMP111

  • 7/30/2019 shell.ppt

    15/24

    COMP111

    Lecture 5 / Slide 15

    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 sort1401 pts/0 0:01 csh

    3476 pts/0 0:01 ypcat

    $

    COMP111

  • 7/30/2019 shell.ppt

    16/24

    COMP111

    Lecture 5 / Slide 16

    Jobs (3)

    Note that if you put something into the background, youbetter redirect stdout, or the output will appear on yourscreen 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/phsee_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

    $ ypcat passwd >file &

    $

    COMP111

  • 7/30/2019 shell.ppt

    17/24

    COMP111

    Lecture 5 / Slide 17

    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 tcsh3496 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

    COMP111

  • 7/30/2019 shell.ppt

    18/24

    COMP111

    Lecture 5 / Slide 18

    Combining Commands (1)

    Multiple pipelines can be input on one command line byseparating them with semicolons.

    When entering a long command, use a backslash (\) tocontinue the command on the next line.

    $ date; sort names; \who

    Thu Feb 11 19:40:28 HKT 1999

    Bill Clinton

    Bill Clinton

    Bill GatesBill Gates

    Monica Lewinski

    horner pts/3 Feb 11 10:23 (csnt1.cs.ust.hk)

    horner pts/0 Feb 11 19:11 (csz096.cs.ust.hk

    COMP111

  • 7/30/2019 shell.ppt

    19/24

    COMP111

    Lecture 5 / Slide 19

    Combining 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 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

    325

    This line has the effect of counting the files in secret, and thencounting the files in the current directory.

    COMP111

  • 7/30/2019 shell.ppt

    20/24

    COMP111

    Lecture 5 / Slide 20

    More Pattern Matching (1)

    The notation [abcd] matches any single one of the enclosed characters.$ ls [il]*

    it it1 ith its@ letter1 letter4

    $

    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

    COMP111

  • 7/30/2019 shell.ppt

    21/24

    COMP111

    Lecture 5 / Slide 21

    More Pattern Matching (2)

    Most shells allow you to give a list of strings in curlybrackets, comma separated:

    $ ls *{1,.sort}

    NAMES1 it1 names.sort s1@f1 letter1 passwd.sort

    secret1:

    secret1: Permission denied

    $

    COMP111

  • 7/30/2019 shell.ppt

    22/24

    COMP111

    Lecture 5 / Slide 22

    More Pattern Matching grep (1)

    Note the patterns for other utilities such as grep, etc., aredifferent. We use grep as an example. The followingsymbols are treated specially:

    ^ 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

    COMP111

  • 7/30/2019 shell.ppt

    23/24

    COMP111

    Lecture 5 / Slide 23

    More 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' namesBill GatesJacky Gs

    $ grep 'G.+s' names

    $ egrep 'G.+s' namesBill Gates$ egrep 'G.s' names$ egrep 'G.?s' namesJ. Jones

    $ grep 'J\.' namesJ. Jones

    Some versions of grep

    cannot process ?+

    COMP111

  • 7/30/2019 shell.ppt

    24/24

    COMP111

    Lecture 5 / Slide 24

    More Pattern Matching grep (3)

    $ cat namesBill Gates

    Jacky Gs

    James Bill

    J. Jones

    $ grep 'Ja[cm]' names

    Jacky Gs

    James Bill

    $ grep 'J[^a]' names

    J. Jones$ grep [t-y]' names

    Bill Gates

    Jacky Gs

    $ grep -in bill names1:Bill Gates3:James Bill

    $ grep -v Bill names

    Jacky GsJ. Jones

    $ grep -l Bill n*names

    $ grep -c Bill names2

    -i ignores case

    -n shows l ine number

    -v means line *not*

    con tain ing pattern

    -l shows *fi lename*

    con tain ing pattern

    -c shows match ing count