WL files1

download WL files1

of 145

Transcript of WL files1

  • 8/11/2019 WL files1

    1/145

    SLC/UNI X/JULY20001

    By the end of the session...

    UNIX Operating system

    Commands

    Shell programming

  • 8/11/2019 WL files1

    2/145

    SLC/UNI X/JULY20002

    Types of Shells

    There are many shells (interpreters)

    available on UNIX

    There are 3 widely used shells namely:

    Bourne shell

    C Shell

    Korn Shell or K-Shell

  • 8/11/2019 WL files1

    3/145

    SLC/UNI X/JULY20003

    Bourne Shell

    Known after the inventor Steve Bourne

    Very popular

    Comes with UNIX

  • 8/11/2019 WL files1

    4/145

    SLC/UNI X/JULY20004

    C Shell

    Created by Bill Joy

    Used by serious UNIX programmers

    It allows aliasing of commands

    Command history maintenance. This is

    more like the DOS doskey.

    These are the two key features

  • 8/11/2019 WL files1

    5/145

    SLC/UNI X/JULY20005

    Korn Shell

    Also known as K-shell

    Designed by David Korn of AT&T

    Very powerful

    Superset of Bourne shell

  • 8/11/2019 WL files1

    6/145

    SLC/UNI X/JULY20006

    Access to UNIX

    Access to UNIX is given through login &

    password

    login:abc

    password :xyz

    login & password will be created by the

    system administrator

    password is not visible while typing

    After 3 successive failures of login, the

    login will be disabled

  • 8/11/2019 WL files1

    7/145

    SLC/UNI X/JULY20007

    Access to UNIX

    If the login or password is not entered

    correctly, it will give login incorrect

    message

    It wont tell which is not correct - login /password. This is a security measure

    After the successful login, it will go to $

    prompt for ordinary user, # prompt forsuper user

    The prompt can be changed through PS1

  • 8/11/2019 WL files1

    8/145

    SLC/UNI X/JULY20008

    UNIX Commands

    All UNIX commands should be entered

    in lower case

    Between the command and the

    arguments, there must be at least onespace or tab

    e.g.: ls -l

    All command flags will be given with -

    (Hyphen)

  • 8/11/2019 WL files1

    9/145

    SLC/UNI X/JULY20009

    UNIX Commands

    One or more flags can be clubbed

    e.g.: ls -l -acan be given as ls -la

    DEL-key erases characters and cancels a

    command

    Terminal should not be turned-off as

    soon as the work is over. Press ^d

    (Ctrl+d) to come into login:prompt

  • 8/11/2019 WL files1

    10/145

    SLC/UNI X/JULY200010

    UNIX file system

    Creation of login involves

    Identifying login name to the user

    Identifying initial password

    Identify a group for the user

    Identifying a default shell to the user

    Identifying a working directory

  • 8/11/2019 WL files1

    11/145

    SLC/UNI X/JULY200011

    UNIX file system

    UNIX understands everything as a file. It

    may be disk, terminal, directory or file.

    A file is a stream of bytes.

    A file will have properties like block

    special, character special devices etc.

    A file system begins with a directory

    called root

  • 8/11/2019 WL files1

    12/145

    SLC/UNI X/JULY200012

    UNIX file system - root directory

    Denoted by / slash

    Every directory will branch from root

    Other directories under root directory

    are bin, lib, usr, etc, tmp & dev

    Contains the kernel (UNIX)

    All directories will have files with specific

    purpose

  • 8/11/2019 WL files1

    13/145

    SLC/UNI X/JULY200013

    UNIX file system - Structure

    UsersHardware

    Kernel

    Shell

    Tools & Applications

  • 8/11/2019 WL files1

    14/145

    SLC/UNI X/JULY200014

    UNIX file system - bin directory

    Contains the binary files i.e. executables

    UNIX commands are mostly C programs

    or shell programs

    Shell programs are collection of several

    UNIX commands

  • 8/11/2019 WL files1

    15/145

    SLC/UNI X/JULY200015

    UNIX file system - lib directory

    Contains library functions

    The programmers under UNIX will use

    these libraries

  • 8/11/2019 WL files1

    16/145

    SLC/UNI X/JULY200016

    UNIX file system - dev directory

    Contains device driver files of input /

    output

    The devices like terminals, printers, disk

    drivers etc

    Separate file for each device

    Each device is implemented as a file

  • 8/11/2019 WL files1

    17/145

    SLC/UNI X/JULY200017

    UNIX file system - usr directory

    Contains several user directories

    Created by system administrator when

    creating new accounts

    Each user is allowed to work in his

    directory often known as home directory

    User can organize his directory by

    creating subdirectories

    usr contains another bin directory

    containing additional UNIX commands

  • 8/11/2019 WL files1

    18/145

    SLC/UNI X/JULY200018

    UNIX file system - tmp

    Contains temporary files created by

    UNIX or by other users

    Will be deleted by UNIX at shutdown

    time

  • 8/11/2019 WL files1

    19/145

    SLC/UNI X/JULY200019

    Date - Command

    This command is used to display the currentSystem date or set the system date.Example :$ date

    Thu Nov 8 03:13:18 MST 2007

    $ date +%a

    Thu

    $ date +%A

    Thursday

    $ date +%b

    Nov

    $ date +%B

    November

    $ date +%d08

    $ date +%D

    11/08/07

    $ date +"%r %a %d %b %y"

    03:18:59 AM Thu 08 Nov 07

  • 8/11/2019 WL files1

    20/145

    SLC/UNI X/JULY200020

    Creating files

    Can be created using two commands

    touch & cat

    $touchfile1 file2

    Creates file-name-1, file-name-2 files

    of zero size

    Used to create several empty files

  • 8/11/2019 WL files1

    21/145

    SLC/UNI X/JULY200021

    Creating files - cat

    $cat>file1

    Whatever typed will go to the file file1

    Press ^d (Ctrl+d) to abort the cat

    command

    ^d inserts the EOF (End Of File)

    character

    $catfile1

    Displays the contents of the file1

  • 8/11/2019 WL files1

    22/145

  • 8/11/2019 WL files1

    23/145

    SLC/UNI X/JULY200023

    Creating files - cat

    To add to file3 by:

    $catfile1 file2>>file3

    Appends the contents of file1, file2 to file3

  • 8/11/2019 WL files1

    24/145

    SLC/UNI X/JULY200024

    CAT Command

    Can create files

    Can club files

    Can be used to see the files

    Can append to files

  • 8/11/2019 WL files1

    25/145

    SLC/UNI X/JULY200025

    File Copy Command - cp

    To copy file1 to file2 by:

    $cpfile1 file2

    To copy files to directory

    $cpfile1 file2 dir-name

    Can copy files across directories by

    providing absolute or relative path:

    $cp/usr1/file1 /usr2/file1

  • 8/11/2019 WL files1

    26/145

    SLC/UNI X/JULY200026

    File Delete Command - rm

    Removes the file

    The file removed can not be recovered

    like in DOS using undelete

    Care should be taken while deleting:

    $rmfile1

    Removes file1

  • 8/11/2019 WL files1

    27/145

    SLC/UNI X/JULY200027

    File Delete Command - rm

    $rm -ifile1

    Removes file interactively. Removes after

    confirmation

    $rm -rdir1

    Removes files from dir1 recursively

    $rm -irdir1

    Removes file from dir1 recursively and

    interactively

  • 8/11/2019 WL files1

    28/145

  • 8/11/2019 WL files1

    29/145

    SLC/UNI X/JULY200029

    File Listing Command - ls

    Provides the current directory listing:

    $ls

    Provides the directory contents:

    $lsdir-name

    Displays the file names in alphabetically

    sorted order

    Wont display the file names starting

    with . period.

  • 8/11/2019 WL files1

    30/145

    SLC/UNI X/JULY200030

    File Listing Command - ls

    To display all files including the filesstarting with .

    $ls -a

    To display only file-names starting with

    a,b,c only

    $ls [a-c]*

    To display long listing

    $ls -l

  • 8/11/2019 WL files1

    31/145

    SLC/UNI X/JULY200031

    File Listing Command - ls

    $ls -l gives something like thistotal 32

    -rwxr-x--x 1 usr1 group1 24 jan 00 10:13 file1

    drwxr-x--x 1 usr1 group1 24 jan 00 10:13 file2

    -rwxr-x--x 2 usr1 group1 24 jan 00 10:13 file3

    total 32 means current directory

    occupied 32 blocks

    Generally each block in UNIX is 1024

  • 8/11/2019 WL files1

    32/145

    SLC/UNI X/JULY200032

    File Listing Command - ls

    The next 9 characters provides theinformation about file permissions

    Next column gives the number of links

    Owner name

    Group name

    File size in bytes

    Date & Time of last modified

    Finally the file-name

  • 8/11/2019 WL files1

    33/145

    SLC/UNI X/JULY200033

    File Listing Command - ls

    Owner concept comes with multi-user OS

    The file creator is the owner by default

    Group is formed with same type of users

    Anybody other than the above are others The first 3 characters in permission field is

    owner permissions

    The next 3 characters tells about the grouppermissions

    Last 3 characters gives the other permissions

  • 8/11/2019 WL files1

    34/145

    SLC/UNI X/JULY200034

    File Listing Command - ls

    There are three types of permissions like

    r- read permission

    w- write permission

    x- execution permission

    rwxr-x--xmeans

    Owner of this file has read, write and

    execute permissions

    Group has read and execute permissions

    Others has only execution permissions

  • 8/11/2019 WL files1

    35/145

  • 8/11/2019 WL files1

    36/145

    SLC/UNI X/JULY200036

    File Change Permission - chmod

    Existing file permissions can be changedwith the command chmod

    To change the rwxr-x--x(751)

    permissions to rwxrwxrwx(777)permissions of file1 is

    $chmod777 file1

    To give write permission to all

    $chmod +wfile1

  • 8/11/2019 WL files1

    37/145

    SLC/UNI X/JULY200037

    File Change Permission - chmod

    To remove write permissions to allincluding the owner

    $chmod-wfile1

    The above command is generally used to

    keep a file safe by making it READ-

    ONLY

  • 8/11/2019 WL files1

    38/145

    SLC/UNI X/JULY200038

    Directory Permissions

    A directory is also a file for UNIX

    Directory will have the permissions as

    that of any file

    A directory is writable if the user has

    write permissions etc

    754 are the most widely used permissions

    for a directory

  • 8/11/2019 WL files1

    39/145

    SLC/UNI X/JULY200039

    Directory Commands - pwd

    User goes to home directory uponsuccessful login

    To know which directory we are

    currently in:$pwd

    pwd stands for present working directory

  • 8/11/2019 WL files1

    40/145

  • 8/11/2019 WL files1

    41/145

    SLC/UNI X/JULY200041

    Directory Commands - mkdir

    To create a directory with givenpermissions:

    $mkdir -m754 new-dir

    new-dir will be created with 754

    permissions

  • 8/11/2019 WL files1

    42/145

  • 8/11/2019 WL files1

    43/145

  • 8/11/2019 WL files1

    44/145

    SLC/UNI X/JULY200044

    UNIX commands - banner

    Prints the given string in big letters

    $banner

  • 8/11/2019 WL files1

    45/145

    SLC/UNI X/JULY200045

    UNIX commands - touch

    Used to change the last access time of afile

    $touch -afile1

    Changes the access timeof file1 with

    current time

    $touchfile1

    Changes access time as well as

    modification time will be changed to

    current time

  • 8/11/2019 WL files1

    46/145

    SLC/UNI X/JULY200046

    UNIX commands - file

    Used to know the file type like shellscript, executable

    $filefile-name

    english text

  • 8/11/2019 WL files1

    47/145

    SLC/UNI X/JULY200047

    UNIX file commands - wc

    Used to know the number of characters,number of files and number of lines in a

    file

    $wcfile1$wc -lfile1

    Displays the number of lines in file1

    Can take the input from terminal directly

  • 8/11/2019 WL files1

    48/145

    SLC/UNI X/JULY200048

    UNIX file commands - sort

    Used to sort a file

    $sortfile1

    Sorts file1 and displays the sorted file on

    terminal

    Instead of displaying the sorted file on

    terminal, can be written in to a file by:

    $sort o file2 file1

    Writes the sorted file1 in file2

  • 8/11/2019 WL files1

    49/145

    SLC/UNI X/JULY200049

    UNIX file commands - cut

    Cut is a filter.

    Picks up given number of bytes from a

    given file by:

    $cut -f5,10 file1

    Displays 5th field to 10th field in file1.

    Each field is assumed to be separated by

    TAB character

  • 8/11/2019 WL files1

    50/145

    SLC/UNI X/JULY200050

    UNIX file commands - grep

    This is the good example of unfriendlyUNIX command

    Acronym for Globally search a Regular

    Expression and Print it Searches for a pattern in file(s)

    $grepabc file1

    Searches for the string abc in file1

    fi

  • 8/11/2019 WL files1

    51/145

    SLC/UNI X/JULY200051

    UNIX file commands - head

    Displays only first few lines of a file asper the argument

    $head -15file1

    Displays only first 15 lines of file1

    UNIX fil d il

  • 8/11/2019 WL files1

    52/145

    SLC/UNI X/JULY200052

    UNIX file commands - tail

    Displays only last few lines of a file as perthe argument

    $tail -15file1

    Displays the last 15 lines of file1

    UNIX fil d /

  • 8/11/2019 WL files1

    53/145

    SLC/UNI X/JULY200053

    UNIX file commands - pg/more

    Both commands work almost like thesame with few differences

    $pg +5 -20 -p -sfile1

    Starts displaying file1 20 lines per page,starting from 5th line, with prompt for

    each page of display

  • 8/11/2019 WL files1

    54/145

    UNIX fil d l t t

  • 8/11/2019 WL files1

    55/145

    SLC/UNI X/JULY200055

    UNIX file commands - lpstat

    Gives the printer status like which iscurrently printing, how many jobs are

    still in queue etc

    $lpstat -tscheduler is running

    system default destination : lp

    device for lp: /dev/lp0lp accepting requests since

    Queue information

    UNIX fil d l

  • 8/11/2019 WL files1

    56/145

    SLC/UNI X/JULY200056

    UNIX file commands - cancel

    Used to cancel the given print job$cancellp-15

    request lp-15,226 cancelled

    UNIX fil d

  • 8/11/2019 WL files1

    57/145

    SLC/UNI X/JULY200057

    UNIX file commands - compress

    Two commands provided for filecompression namely compress and pack

    They behave the same way

    $compress -vfile1

    file1: Compression: 90.1%

    Compresses the file file1 and creates a file

    by name file1.z

  • 8/11/2019 WL files1

    58/145

    UNIX fil d

  • 8/11/2019 WL files1

    59/145

    SLC/UNI X/JULY200059

    UNIX file commands - man

    UNIX provides on-line manuals by:$man

    Displays all the information about

    I/O R di ti & Pi i

  • 8/11/2019 WL files1

    60/145

    SLC/UNI X/JULY200060

    I/O Redirection & Piping

    I/O can be redirected using

    symbol implies - write the output inthe file instead of display unit

    2> symbols implies - write the error in a

    file rather than display unit

    e.g.:abcout-file 2>err-file

    I/O R di ti & Pi i

  • 8/11/2019 WL files1

    61/145

    SLC/UNI X/JULY200061

    I/O Redirection & Piping

    A >> indicates the appending of data toa file if it exists, instead of over-writing

    1>&2indicates - redirect Standard Output

    to Standard Er ror | means -

    the standard output of Command-1 is

    going to be the standard input ofCommand-2

    I/O R di ti & Pi i

  • 8/11/2019 WL files1

    62/145

    SLC/UNI X/JULY200062

    I/O Redirection & Piping

    UNIX supports joining of commandsthrough | piping character

    $ls| wc -l

    The output of ls is the input for wc

    $ls | wc -l >outfile

    Output of wc will be written in outfile

    I/O Redirection & Piping

  • 8/11/2019 WL files1

    63/145

    SLC/UNI X/JULY200063

    I/O Redirection & Piping

    Output of first process will be written topipe and the pipe then hands it over to

    the next process.

    tee can be used to capture the pipe file$who | tee file1 | sort

    Output of who will be written into file1

    and the file will be sorted and the sortedfile will be written to standard output

    UNIX Command ps

  • 8/11/2019 WL files1

    64/145

    SLC/UNI X/JULY200064

    UNIX Command - ps

    To see the processes running currentlyunder UNIX can be had by:

    $ps

    PID TTY TIME COMMAND2266 tty2 0:06 sh

    2269 tty2 0.02 ps

    Process Status command is shortlyknown as ps

    UNIX Background Process

  • 8/11/2019 WL files1

    65/145

    SLC/UNI X/JULY200065

    UNIX Background Process

    Time consuming tasks may be run inbackground

    Place & at the end of the command, the

    process will go to background Displays the PID after submission

    Success or failure of the background

    process will not be reported

    UNIX Background Process

  • 8/11/2019 WL files1

    66/145

    SLC/UNI X/JULY200066

    UNIX Background Process

    Better to redirect the background processoutput to some file. Otherwise, it will

    disturb the foreground process messages

    on the screen Too many background processes will

    degrade the performance

    If logged out, all the backgroundprocesses will be terminated

    UNIX Command Sleep

  • 8/11/2019 WL files1

    67/145

    SLC/UNI X/JULY200067

    UNIX Command - Sleep

    This command is used to delay a process for a periodof timeFor Example :

    To run a command at regular intervals, enter:

    while true

    do

    date

    sleep 60

    done

    This shell procedure displays the date and time once a minute.

    To stop it, press the Interrupt key sequence.

    Killing a Process

  • 8/11/2019 WL files1

    68/145

    SLC/UNI X/JULY200068

    Killing a Process

    To terminate a process$killPID

    An user can kill only his processes

    UNIX Communication write

  • 8/11/2019 WL files1

    69/145

    SLC/UNI X/JULY200069

    UNIX Communication - write

    Any user can write a message on otherusers terminal.

    Other user should give permissions to

    write message$write

    hello

    ^d (Ctrl+d)

    Hellowill be written on user-names

    terminal

    UNIX Communication write

  • 8/11/2019 WL files1

    70/145

    SLC/UNI X/JULY200070

    UNIX Communication - write

    The user should be logged-in to receive the message

    The recipient should give permissions for

    message Message writing can be denied by:

    $mesg-n

    Super user can write to any terminal

    without permissions

    UNIX Communication finger

  • 8/11/2019 WL files1

    71/145

    SLC/UNI X/JULY200071

    UNIX Communication - finger

    Can find who are the users who setmessage -n by:

    $finger -i

    Login TTY When Idleabc *tty1

    def tty2

    ghi tty3 a * before the tty means - set mesg -n

    UNIX Communication motd

  • 8/11/2019 WL files1

    72/145

    SLC/UNI X/JULY200072

    UNIX Communication - motd

    The Message of the day news will betyped in a file called /etc/motd. The

    contents will be displayed upon login.

    News like The system will be taken formaintenance from 14.00 Hrs to 16.00

    Hrs so that the users will schedule their

    work accordingly.

    Shell Programming

  • 8/11/2019 WL files1

    73/145

    SLC/UNI X/JULY200073

    Shell Programming

    A shell program is a series of UNIXcommands

    Instead of typing the commands one after

    the other, the commands may be writtenin a file and execute the file.

    Shell programming offers much more

    versatility than mere UNIX commands Almost a system can be developed by

    using shell script with UNIX commands

    Shell Programming

  • 8/11/2019 WL files1

    74/145

    SLC/UNI X/JULY200074

    Shell Programming

    UNIX shell is the interface between theoperating system & user

    It is an interpreter, not an executable

    A shell script can be executed by givingexecute permissions ($chmod +x shell) or

    by shell command ($sh shell)

    Shell programming is so powerful, evensystem shutdown like tasks can be

    handled

    Shell Programming

  • 8/11/2019 WL files1

    75/145

    SLC/UNI X/JULY200075

    Shell Programming

    Three shells available under UNIX -Bourne, C & Korn shell

    The following shell scripts were written

    in Bourne shell Bourne shell script will work with Korn

    shell and the reverse is not true

    Bourne is the default shell in most of theinstallations

    Shell Programming

  • 8/11/2019 WL files1

    76/145

    SLC/UNI X/JULY200076

    Shell Programming

    Shell scripts are used to accomplishvariety of tasks like:

    Customizing the working environment

    Automating the daily tasks like backupsetc

    Automating repetitive tasks

    Executing important system procedureslike shutdown

    Shell Programming

  • 8/11/2019 WL files1

    77/145

    SLC/UNI X/JULY200077

    Shell Programming

    When the user logins into the system, theUNIX starts a shell (sh) on that terminal

    which accepts commands from $

    prompt When a shell-script is given for execution,

    the shell executes the commands from the

    shell-script

    Interactive Shell Programming

  • 8/11/2019 WL files1

    78/145

    SLC/UNI X/JULY200078

    Interactive Shell Programming

    Two basic words in shell are read& echo read accepts input

    echo writes the output

    echo Enter your name \?

    read name

    echo Good morning $name

    Write the above 3 lines in a file name

    sh01

  • 8/11/2019 WL files1

    79/145

    Interactive Shell Programming

  • 8/11/2019 WL files1

    80/145

    SLC/UNI X/JULY200080

    Interactive Shell Programming

    name is a shell variable receives datafrom keyboard

    $name will display the contents of the

    variable

    Interactive Shell Programming

  • 8/11/2019 WL files1

    81/145

    SLC/UNI X/JULY200081

    Interactive Shell Programming

    echo Enter three values X Y Zread x y z

    echo $x $y $z

    If 1 2 3 entered, x will have 1, 2 to y & 3 to z

    If 1 2 3 4 are entered, then z will have 3 4

    If only 1, 2 are entered, then z will be assigned

    null value

    If more arguments entered, last variable will beassigned rest. If less, null will be assigned

    Interactive Shell Programming

  • 8/11/2019 WL files1

    82/145

    SLC/UNI X/JULY200082

    Interactive Shell Programming

    echo This is new line \nAnd this is secondline

    The above will be displayed as

    This is new lineAnd this is second line

    The escape sequence was brought from C

    language

    Interactive Shell Programming

  • 8/11/2019 WL files1

    83/145

    SLC/UNI X/JULY200083

    Interactive Shell Programming

    \b is blank, \t is tab char etc. To make the cursor at the end of the

    echoed line

    echo Enter your Name :\c

    Enter your Name :_ (Cursor waits here)

    echo \007 gives bell

    Shell Variables

  • 8/11/2019 WL files1

    84/145

    SLC/UNI X/JULY200084

    Shell Variables

    They are an integral part of shellprogramming

    Provide ability to store and manipulate

    data Fully under the control of the shell

    Can create and destroy the variables as

    the user wants

    Shell Variables - Guidelines

  • 8/11/2019 WL files1

    85/145

    SLC/UNI X/JULY200085

    Shell Variables Guidelines

    Any combination of alphabet, numbersand underscore(_ )

    No editing characters like commas,

    blanks The first character of the name should be

    alpha or underscore

    May be of any reasonable length Case sensitive ie abc & Abc are different

    Shell - Assigning values

  • 8/11/2019 WL files1

    86/145

    SLC/UNI X/JULY200086

    Shell Assigning values

    Values can be assigned to variable using= equal sign

    $name=Vijay

    $echo $namedisplays Vijay

  • 8/11/2019 WL files1

    87/145

    System Variables

  • 8/11/2019 WL files1

    88/145

    SLC/UNI X/JULY200088

    System Variables

    UNIX defines certain variables for itsown usage

    e.g.: PS1 stands for prompt string1

    $PS1=ABC

    From this time onwards, UNIX displays

    ABC as prompt

    System Variables

  • 8/11/2019 WL files1

    89/145

    SLC/UNI X/JULY200089

    System Variables

    PS2 is prompt string 2, default value is>

    PATH defines the directories to searched

    by shell to execute any command or file HOME defines the default working

    directory

    LOGNAME stores the log name of theuser

    System Variable

  • 8/11/2019 WL files1

    90/145

    SLC/UNI X/JULY200090

    System Variable

    MAIL stores the file name of the mailboxalong path

    MAILCHECK duration of the mail

    checking SHELL keeps the default shell name

    System Variables

  • 8/11/2019 WL files1

    91/145

    SLC/UNI X/JULY200091

    Sys e V b es

    TERM gives the name of the terminal theuser is using

    All the above variable values can be

    known by set command$set

    Displays all the values

  • 8/11/2019 WL files1

    92/145

    Tips & Traps

  • 8/11/2019 WL files1

    93/145

    SLC/UNI X/JULY200093

    p p

    Can display more than one variable in aline

    $echo Name is $nameand age is $age

    All variables in a shell script areautomatic variable. i.e. they will be

    created as soon as the shell script

    execution starts and dies as soon as theexecution is over

    Tips & Traps

  • 8/11/2019 WL files1

    94/145

    SLC/UNI X/JULY200094

    p p

    A null variable can be created by$a1= or a1= or a1=

    Shell ignore if any shell variable is having

    null value

    Unchanging Variable - readonly

  • 8/11/2019 WL files1

    95/145

    SLC/UNI X/JULY200095

    g g y

    User can make a variable unchangedduring execution by:

    $age=20

    $readonlyage The the shell does not allow us to change

    the value

  • 8/11/2019 WL files1

    96/145

    Positional Parameters

  • 8/11/2019 WL files1

    97/145

    SLC/UNI X/JULY200097

    Many occasions, a program expects thevariables in a certain fashion. This is

    achieved through positional parameters

    from $0 through $9. $0is the program itself. Thus

    $abc par1 par2 par3 par4 assigns

    abc to $0, par1 to $1 par4 to $4

    Passing command line arguments

  • 8/11/2019 WL files1

    98/145

    SLC/UNI X/JULY200098

    g g

    To write a shell by name sh02 whichcopies one file to another

    echo Copying $1 to $2

    cp $1 $2 By executing sh02

    $sh02 file1 file2

    Copying file1 to file2. Positional parameters can not assigned values

    like $1=100

    Shell Variables

  • 8/11/2019 WL files1

    99/145

    SLC/UNI X/JULY200099

    To know the number of variables givenfor the shell through $#

    $abc file1 file

    echo $# The above shell displays 3

    Shell Variables - shift

  • 8/11/2019 WL files1

    100/145

    SLC/UNI X/JULY2000100

    Shell can handle only 9 variables at a time. Toaccess more than 9, the shift command is used

    $set You have the capacity to learn the shell

    programming in a very easy way

    $echo $10 displays You0 as shell interprets $10

    as $1 with 0.

    $shift 5 : makes the 5th argument as $1

    argument. Thus $echo $1 displays learn

    $* handles all positional parameters

    Positional Parameters

  • 8/11/2019 WL files1

    101/145

    SLC/UNI X/JULY2000101

    $ set a b c d e f g h i j k l m n o p$ echo $1 $2 $3 $4 $5 $6 $7 $8 $9

    a b c d e f g h i

    $ shift 7

    $ echo $1 $2 $3 $4 $5 $6 $7 $8 $9

    h I j k l m n o p

    Positional Parameters

    If the number of parameters are greater than nineThe shiftcommand can be used to shift the parameters

    For Example :

    Arithmetic in Shell

  • 8/11/2019 WL files1

    102/145

    SLC/UNI X/JULY2000102

    Write the following in a file and execute:

    a=10 b=4

    echo expr $a + $b

    echo expr $a - $b

    echo expr $a \* $b

    echo expr $a / $b

    echo expr $a % $b #modulus

    Arithmetic in Shell

  • 8/11/2019 WL files1

    103/145

    SLC/UNI X/JULY2000103

    On execution, the output is:

    14

    6

    40

    2

    2

    Arithmetic in Shell

  • 8/11/2019 WL files1

    104/145

    SLC/UNI X/JULY2000104

    Anything after # sign will be treated ascomment

    expris the key word for doing arithmetic

    A multiplication symbol (*) should bepreceded by \

    Terms in expr should be separated by

    space Parenthesis may be used for clarity of

    expression

  • 8/11/2019 WL files1

    105/145

    UNIX decision loops

  • 8/11/2019 WL files1

    106/145

    SLC/UNI X/JULY2000106

    There are 4 decision making loops

    if then fi

    if then else fi

    if then elif else fi

    case - esac

    UNIX - if then fi

  • 8/11/2019 WL files1

    107/145

    SLC/UNI X/JULY2000107

    Key word ifand the delimiter is fi

    if

    then

    statements

    fi

    e.g.: if cp $1 $2

    then echo Copied successfully

    fi

    UNIX - if then else fi

  • 8/11/2019 WL files1

    108/145

    SLC/UNI X/JULY2000108

    The structure of this construct is:

    if

    then

    statements

    else

    statements

    fi

    UNIX - if then else fi

  • 8/11/2019 WL files1

    109/145

    SLC/UNI X/JULY2000109

    e.g.:if cp $1 $2

    then

    echo File copiedsuccessfully

    else

    echo File copy failed

    fi

    Example if else - fi

  • 8/11/2019 WL files1

    110/145

    SLC/UNI X/JULY2000110

    # Example of numeric testecho Enter basic salary

    read bs

    if [ $bs -lt 2500 ]

    then

    hra=echo $bs \* 10 / 100 | bc

    da=echo $bs \* 90 / 100 bc

    else

    hra=500

    da=echo $bs \* 98 / 100 | bc

    fi

    gs=echo $bs + $hra + $da | bc

    echo Gross salary = Rs. $gs

    UNIX - if then elif else fi

  • 8/11/2019 WL files1

    111/145

    SLC/UNI X/JULY2000111

    Used for multilevel decision making.

    if then

    statements

    elif

    statements

    else

    statements

    fi

    UNIX - test

  • 8/11/2019 WL files1

    112/145

    SLC/UNI X/JULY2000112

    if depends upon the exit status of thecommand given

    test verbs translates the result into

    success or failure There are three tests namely

    Numerical test

    String test

    File test

    UNIX - test - numerical

  • 8/11/2019 WL files1

    113/145

    SLC/UNI X/JULY2000113

    Used to compare numerical

    -gt= greater than

    -lt = less than

    -ge= greater than or equal to

    -le = less than or equal to

    -ne= not equal

    -eq= equal

    UNIX - test - numerical

  • 8/11/2019 WL files1

    114/145

    SLC/UNI X/JULY2000114

    if [ $1 -lt 5 ]then

    echo the value is < 5

    elif [ $1 -le 7 ]

    echo the value is 7

    fi

    UNIX - test

  • 8/11/2019 WL files1

    115/145

    SLC/UNI X/JULY2000115

    Use square braces to avoid writing test

    Provide a space after [

    Provide a space before ]

  • 8/11/2019 WL files1

    116/145

    UNIX - test - String

  • 8/11/2019 WL files1

    117/145

    SLC/UNI X/JULY2000117

    s1 = s2 returns true if both are same -n returns true if string length > 0

    if [ $1 = $2 ] then

    echo Both strings are same

    else

    echo $?

    fi

    $?Contains the value of the last command

    0 = true, 1 = false

    UNIX logical conditions

  • 8/11/2019 WL files1

    118/145

    SLC/UNI X/JULY2000118

    -astands for AND condition

    -ostands for OR condition

    -!Is negation

    if -a

    Returns true if both the conditions are

    true

    if [ $1 -gt 60 ] -a [ $2 -lt 50 ]then

    statements...

    UNIX case

  • 8/11/2019 WL files1

    119/145

    SLC/UNI X/JULY2000119

    To handle multiple choices

    casevalue in

    choice 1) statements;;

    choice 2) statements;;

    *) statements;;

    esac

    UNIX case

  • 8/11/2019 WL files1

    120/145

    SLC/UNI X/JULY2000120

    esac is the delimiter of case

    Used for menus

    *) is the default choice

    All choice statements should beterminated by double semicolon(;;)

    UNIX case example

  • 8/11/2019 WL files1

    121/145

    SLC/UNI X/JULY2000121

    case $option in1) echo Financial accounting;;

    2) echo Materiel accounting;;

    *) echo Invalid Opt - Try;;

    esac

  • 8/11/2019 WL files1

    122/145

    UNIX case

  • 8/11/2019 WL files1

    123/145

    SLC/UNI X/JULY2000123

    Multiple options can be groupede.g.:

    case $1 in

    banana | orange) echo Fruit;;dog | pig) echo Animal;;

    lion) echo Wild animal;;

    esac

    UNIX case

  • 8/11/2019 WL files1

    124/145

    SLC/UNI X/JULY2000124

    Can use shells pattern matchinge.g.:

    case $1 in

    [a-z]) echo Small alpha;;[A-Z]) echo Capital;;

    esac

    UNIX Loop Controls

  • 8/11/2019 WL files1

    125/145

    SLC/UNI X/JULY2000125

    Provided 3 loop constructs namely: whileloop

    forloop

    untilloop

    UNIX - whileloop

  • 8/11/2019 WL files1

    126/145

    SLC/UNI X/JULY2000126

    while do

    statements

    done done is the delimiter of do

    UNIX - while example

  • 8/11/2019 WL files1

    127/145

    SLC/UNI X/JULY2000127

    count = 1while [ $count -le 3 ]

    do

    echo Loop value $countcount = `expr $count + 1`

    done

    Examples of while loop

  • 8/11/2019 WL files1

    128/145

    SLC/UNI X/JULY2000128

    # calculation of simple interest

    count=1

    while [ $count -le 3 ]do

    echo \nEnter p,n,r\c

    read p n r

    si=echo $p \* $n \* $r /100 | bc

    echo Simple interest = Rs.$si

    count=expr $count + 1

    done

    # printing numbers using while

    i=1

    while [ $i -le 10 ]

    doecho $i

    i=expr $i + 1

    done

  • 8/11/2019 WL files1

    129/145

    UNIX - until loop

  • 8/11/2019 WL files1

    130/145

    SLC/UNI X/JULY2000130

    until do

    statements

    done

    until continues its loop so long as the

    condition is false

    except this, while & until are identical

    Example - until loop

  • 8/11/2019 WL files1

    131/145

    SLC/UNI X/JULY2000131

    # printing numbers using until

    i=1

    until [ $i -gt 10 ]

    do

    echo $1

    i=expr $i + 1

    done

    UNIX - for loop

  • 8/11/2019 WL files1

    132/145

    SLC/UNI X/JULY2000132

    Most frequently used loopfor control-var in value1

    value2

    dostatements

    done

    for takes a list of variables

    UNIX - for example

  • 8/11/2019 WL files1

    133/145

    SLC/UNI X/JULY2000133

    for word in $*do

    echo $word

    done

    Examplefor loop

  • 8/11/2019 WL files1

    134/145

    SLC/UNI X/JULY2000134

    Example

    # To print names of all sub-directories

    for entry in *

    do

    if [ -d $entry ]then

    echo $entry

    fi

    done

    UNIX - break statement

  • 8/11/2019 WL files1

    135/145

    SLC/UNI X/JULY2000135

    Used to break the current loop and comesout of the loop

    Usually associated with i f

    if [ $1 -eq 5 ]then

    I = 2

    break

    fi

    UNIX - continue statement

  • 8/11/2019 WL files1

    136/145

    SLC/UNI X/JULY2000136

    To take the control to the beginning ofthe loop bypassing the statements

    I=1

    while [ $I -le 5 ] thendo

    I = `expr $I + 1`

    continue

    done

    UNIX - Metacharacters

  • 8/11/2019 WL files1

    137/145

    SLC/UNI X/JULY2000137

    Called as regular expressions. Classified as follows:

    File name : ? * [] [!]

    I/O redirection : < > >> m>&n

    Process execution: ; ( ) & && ||

    Positional paras : $1..$9

    Spl characters : $0 $* $@ $# $! $$ $-

    UNIX - File name

  • 8/11/2019 WL files1

    138/145

    SLC/UNI X/JULY2000138

    ls??- lists all files with 2 chars long lsa*- lists all file names begin with a

    ls[a-c]*-file names begin with a,b &c

    ls[!a-c]- file names not starting witha,b,c

    UNIX - I/O Redirection

  • 8/11/2019 WL files1

    139/145

    SLC/UNI X/JULY2000139

    write output to

    >>append output

  • 8/11/2019 WL files1

    140/145

  • 8/11/2019 WL files1

    141/145

    Tips & Traps

  • 8/11/2019 WL files1

    142/145

    SLC/UNI X/JULY2000142

    Try to use the absolute paths in the shellscripts. This saves the search time of the

    system for the command

    Remove write permissions to files thatare important. This even prevents the

    accidental update of the files

    Compress the print/report files. These

    files occupies lot of disk space. Better

    pack them and keep

    Tips & Traps

  • 8/11/2019 WL files1

    143/145

    SLC/UNI X/JULY2000143

    The compressed print files can be printeddirectly without uncompress them by

    using the pcat command

    $pcattextfile.txt.z > /dev/lp0 The above command will send the print

    file directly to printer

    Tips & Traps

  • 8/11/2019 WL files1

    144/145

    SLC/UNI X/JULY2000144

    Untouched files in the directory may befound using findcommand.

    $find . -mtime 10 -print

    displays the files that are untouched forthe last 10 days

    Clean up of the directory should be done

    periodically. Otherwise, any amount diskspace will be inadequate.

    UNIX

  • 8/11/2019 WL files1

    145/145

    Thank Q