13 shells

Upload: saravanaraajaa

Post on 14-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 13 Shells

    1/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    1

    Shells and Shell Scripts

    COMP 444/5201

    Revision 1.3January 25, 2005

  • 7/27/2019 13 Shells

    2/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    2

    Content

    Shells and Shell Scripts

    tcsh, enhanced C-Shell

    bash, Bourne-Again Shell

  • 7/27/2019 13 Shells

    3/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    3

    Shell Commands

    Shell commands are interpreted directly by theshell you specify.

    The commands are similar to the statement insome programming languages, such as C.

    Popular shells include: Enhanced C-shell tchs (csh+)

    Bourne-Again Shell, bash (sh+) Korn Shell (ksh)

    These notes will focus on the first two shells.

  • 7/27/2019 13 Shells

    4/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    4

    Shells Features The bash an tcsh shells are similar in the

    features the offer. In particular: Pass arguments to your script

    Set and reference variables

    Use of control flow

    Interact with the user (read user input)

    Comments

    Info on commands a given shell offers can befound in the man pages for that shell.

    There are many Linux/UNIX references that givedetailed information and tips.

  • 7/27/2019 13 Shells

    5/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    5

    Shell Scripts

    What are they for?

    To automate certain common activities an user

    performs routinely.They serve the same purpose as batch files in

    DOS/Windows.

    Example: rename 1000 files from upper case to lowercase

  • 7/27/2019 13 Shells

    6/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    6

    What are Shell Scripts

    Just text/ASCII files with: a set of standard UNIX/Linux commands (ls, mv, cp,less, cat, etc.) along with

    flow of control

    some conditional logic and branching (if-then),

    loop structures (foreach, for, while), and

    I/O facilities (echo, print, set, ...).

    They allow use of variables.

    They are interpreted by a shell directly.

    Some of them (csh, tcsh) share some of C syntax.

    DOS/Win equivalent - batch files (.bat)

  • 7/27/2019 13 Shells

    7/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    7

    Why not use C/C++ for that?

    C/C++ programming requires compilation

    and linkage, maybe libraries, which may not

    be available (production servers). For the typical tasks much faster in

    development, debugging, and maintenance

    (because they are interpreted and do notrequire compilation).

  • 7/27/2019 13 Shells

    8/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    8

    Shell Script Invocation

    Specify the shell directly: % tcsh myshellscript

    % tcsh -v myshellscript(-v = verbose, useful for debugging)

    Make the shell an executable first and then run is acommand (set up an execution permission): % chmod u+x myshellscript

    Then either this:

    % myshellscript(if the path variable has . in it; security issue!)

    Or: % ./myshellscript

    (should always work)

  • 7/27/2019 13 Shells

    9/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    9

    Shell Script Invocation (2)

    If you get an error:myshellscrip: command not found The probably . is not in your path or theres no

    execution bit set. When writing scripts, choose unique names, that

    preferably do not match system commands. Bad name would be test for example, since there are

    many shells with this internal command. To disambiguate, always precede the shell with

    ./ or absolute path in case you have to nameyour thing not very creatively.

  • 7/27/2019 13 Shells

    10/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    10

    Start Writing a Shell Script The very first line, often called 'shebang' (#!) should

    precede any other line, to assure that the right shell isinvoked.

    Comments start with '#', with the exception of #!, $#,

    which are a special character sequences. Everything on a line after # is ignored if # is not a part

    of a quoted string or a special character sequence.

    #!/bin/tcsh #!/bin/bash

    # This is for tcsh # For Bourne-Again Shell

    #!/bin/sh

    # This is for Bourne Shell

  • 7/27/2019 13 Shells

    11/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    11

    tchs

    Quick Ref

  • 7/27/2019 13 Shells

    12/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    12

    Variables

    Variables start with a $ sign when they are used. $x, $val

    There's no $ when a variable is declared. set x = 3 @ y = 1

    set input = "$

  • 7/27/2019 13 Shells

    13/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    13

    if

    if( ) then

    else if ( ) then

    else

    endif

  • 7/27/2019 13 Shells

    14/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    14

    foreach

    foreach var ( )

    end

  • 7/27/2019 13 Shells

    15/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    15

    switch

    switch ( string )

    case str1:

    breaksw

    ...

    default:

    breakswendsw

  • 7/27/2019 13 Shells

    16/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    16

    while

    while ( )

    end

  • 7/27/2019 13 Shells

    17/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    17

    File Inquiry Operators:

    -op filer Read access

    w Write access

    x Execute access

    e Existenceo Ownership

    z Zero size

    s Non-zero size

    f Plain file

    d Directory

    l Symbolic link

    b Block special filec Character special file

    p Named pipe (FIFO)

    S Socket special file

  • 7/27/2019 13 Shells

    18/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    18

    Example

    See creator and uptolow.

    NOTE: run them in a some temporary

    directory to do not mess with your ownvaluable files.

    The uptolow script:

    will convert any uppercase letters in anordinary file name to lowercase.

    will leave directories untouched.

  • 7/27/2019 13 Shells

    19/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    19

    Bourne Shell

    Quick Ref

  • 7/27/2019 13 Shells

    20/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    20

    Quick Note

    In no way this going to be a duplication for

    the zillions of resources on Bourne Shell,

    but more a quick reference/syntax for mostoften used constructs and pointers to

    resources where else to find that kind of

    stuff. Some of it is a lame reap off the manpage and so on.

  • 7/27/2019 13 Shells

    21/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    21

    Quick Resource Summary

    Manual Pages:

    man bash

    An Intro to UNIX Shell:

    How To Write a Shell Script:

  • 7/27/2019 13 Shells

    22/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    22

    Bourne Shell Script Constructs

    Reference System/Internal Variables

    Control Flow (if, for, case)

  • 7/27/2019 13 Shells

    23/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    23

    Internal Variables$# Will tell you # of command line arguments supplied

    $0 Ourselves (i.e. name of the shell script executed

    with path)

    $1 First argument to the script

    $2 Second argument, and so on

    $? Exit status of the last command

    $$ Our PID

    $! PID of the last background process

    $- Current shell status

  • 7/27/2019 13 Shells

    24/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    24

    Internal Variables (2)

    Use shift command to shift the arguments oneleft: Assume intput:

    ./shift.sh 1 2 foo bar

    $0 = /shift.sh

    $1 = 1

    $3 = 2

    $4 = foo

    $5 = bar

    shift:

    $1 = 2

    $2 = foo

    $3 = bar

  • 7/27/2019 13 Shells

    25/34

    July 17, 2003 Serguei A. Mokhov,[email protected]

    25

    Environment

    These (and very many others) are available to your

    shell:

    $PATH - set of directories to look for commands $HOME - home directory

    $MAIL

    $PWDpersonal working directory

    $PS1primary prompt

    $PS2input prompt

    $IFS - what to treat as blanks

  • 7/27/2019 13 Shells

    26/34

    July 17, 2003 Serguei A. Mokhov,[email protected] 26

    Control Flow: if

    General Syntax:

    can either be a logicalexpression or a command and usually acombo of both.

    if[ ]; then

    elif

    else

    fi

  • 7/27/2019 13 Shells

    27/34

    July 17, 2003 Serguei A. Mokhov,[email protected] 27

    if (2)

    Some Logical Operators:-eq --- Equal

    -ne --- Not equal

    -lt --- Less Than

    -gt --- Greater Than

    -o --- OR

    -a --- AND

    File or directory?-f --- file

    -d --- directory

  • 7/27/2019 13 Shells

    28/34

    July 17, 2003 Serguei A. Mokhov,[email protected] 28

    case

    Syntax:

    case in

    |)command1

    ;;

    |)command2;;

    esac

  • 7/27/2019 13 Shells

    29/34

    July 17, 2003 Serguei A. Mokhov,[email protected] 29

    case (2)

    case $# in

    1) cat >> $1

    ;;

    2) cat >>$2

  • 7/27/2019 13 Shells

    30/34

    July 17, 2003 Serguei A. Mokhov,[email protected] 30

    for

    Syntax:

    List can also be a result of a command.

    for variable in [;]

    docommand1

    command2

    done

  • 7/27/2019 13 Shells

    31/34

    July 17, 2003 Serguei A. Mokhov,[email protected] 31

    for (3)

    for file in *.txt

    do

    echo File $file:

    echo "======"

    cat $file

    echo "======"

    done

  • 7/27/2019 13 Shells

    32/34

    July 17, 2003 Serguei A. Mokhov,[email protected] 32

    while

    Syntax

    while

    do

    command1

    command2

    done

  • 7/27/2019 13 Shells

    33/34

    July 17, 2003 Serguei A. Mokhov,[email protected] 33

    until

    Syntax

    until

    do

    command1

    command2

    done

  • 7/27/2019 13 Shells

    34/34

    July 17 2003 Serguei A Mokhovkh @ di 34

    To Be Continued...