+ All Categories
Home > Documents > Unix Training2

Unix Training2

Date post: 03-Jun-2018
Category:
Upload: rajesh-ganta
View: 223 times
Download: 0 times
Share this document with a friend

of 52

Transcript
  • 8/11/2019 Unix Training2

    1/52

    UNIX Training

    Unix is simple But it requires a genius to understand its

    simplicity Dennis Ritchie

    Compiled by

    Ulagammal Paramasivam

  • 8/11/2019 Unix Training2

    2/52

    Unix Training

    Basic Terminologies

    Fundamentals of Unix OS

    Basic Unix Commands (internaland external)

    Filenames, Meta charactersUsing the vi Editor

    Shell Scripting Overview (Typesof Shells)

    Creating Shell ScriptsAdvanced Shell Scripting

    Scheduling jobs in Unix

  • 8/11/2019 Unix Training2

    3/52

    Basic Terminologies

    Unix WorkstationSingle user computer running Unix

    MultitaskingCooperative MultitaskingProcesses voluntarily sharing theCPU

    Preemptive MultitaskingOSforces the processes for the sharingof the CPU

  • 8/11/2019 Unix Training2

    4/52

    Basic Terminologies (contd)

    KernelThe core of the Unix OS

    Responsibilities of the KernelAddress space mapping

    Current status of the process

    Owner of the process

    Execution priority of the process

    Resource usage of the process

  • 8/11/2019 Unix Training2

    5/52

    Basic Terminologies (contd)

    ShellUser interface to the kernel. It is thecommand processor

    ProcessA running instance of any program

    Process ID (PID)

    Any process under Unix containsan address space and set of data.PID is a unique number thatidentifies a process in the Kernel

  • 8/11/2019 Unix Training2

    6/52

    Fundamentals of Unix

    The different flavors of UnixSolaris - Sun Microsystems, HIXHPUnix, AIXAT & T Bell Laboratories

    The four main blocks inUnix File System

    Boot Block

    Super Block

    I-Node ListData Block

  • 8/11/2019 Unix Training2

    7/52

    Fundamentals of Unix (contd)

    Super BlockKeeps track of the entire file system

    Boot Block

    Contains the code to bootstrapthe OS

    I(nformation)-NodeList of Inodes and the info aboutdata blocks

    Data BlockActual data

  • 8/11/2019 Unix Training2

    8/52

    Fundamentals of Unix (contd)

    File System

  • 8/11/2019 Unix Training2

    9/52

    Fundamentals of Unix (contd)

    Built-in Variables$HOMEThe Home directory of anuser

    $PATHThe path can be

    accessed using this$PS1Primary prompt

    $PS2Secondary prompt

    $SHELLType of shell being used

    $$ - Current shells Process ID

    $PPIDCurrent shells parentprocess ID

    $PWDPrint current workingdirectory

  • 8/11/2019 Unix Training2

    10/52

    Fundamentals of Unix (contd)

    Home DirectoryThe default directory which isavailable when a user logs in.

    To access the home use the$HOME built-in variable

    Types of ShellscshC Shell

    bshBourne ShellkshKorne Shell

  • 8/11/2019 Unix Training2

    11/52

    Fundamentals of Unix (contd)

    Using PathsAbsolute Path

    Relative Path

    Relationship of Kernel,Shells and Applications

  • 8/11/2019 Unix Training2

    12/52

    Filenames, Meta characters

    These are special characters inUnix which are "wild card"characters; they can representother characters.

    * - Represents zero or morecharacters? - Represents zero or onecharacterExamples:

    ls f* - List all files starting with "f"

    ls *.txt - List all files with ".txt" extension

    grep word *.txt - Grep for "word" in allfiles having ".txt" extension

  • 8/11/2019 Unix Training2

    13/52

    Unix Commands

    The shell does not start a new process toexecute the Internal Commandscd, pwd, read, exit, shift, set

    The shell starts a new process to executeExternal Commandsls, cat, grep, find, cut, tr, uniq, sort,

    diff, cal, more, tar, kill, wc, who, which,

    sleep, touch, chmod, date, head, tail, tac,

    id, hostname, passwd, stty, nohup, man, compress

  • 8/11/2019 Unix Training2

    14/52

    Unix Commands (contd)

    cd - Change Directorycd # Takes to $HOME dircd

    pwd - Print the current WorkingDirectory

    read - Read an input from userread var1

    exit - Exit from the current shellexit 0Indicates successNon-ZeroIndicates Failure

  • 8/11/2019 Unix Training2

    15/52

    Unix Commands (contd)

    shift - Shift one positional parameter in

    command line

    shift [n]

    set - Set environmental variables

    set -ax

    ls - List the contents of a directory

    Options:

    -x - Widthwise

    -l - Long listing-t - Time-wise

    -r - Reverse Listing

  • 8/11/2019 Unix Training2

    16/52

    Unix Commands (contd)

    cat - Concatenate filecontents

    cat []

    grep - Search for patterns in afile

    -i - Ignore case-l - Display filename-c - Count of occurrence-v - Negate match-n - Display with preceding line#

    tr - Translate characterstr [option] pattern1 [pattern2]-s - Trim multiple spaces to singlespace-dDelete occurrence of pattern

  • 8/11/2019 Unix Training2

    17/52

    Unix Commands (contd)

    find - Find files in the current

    directory and sub-directories

    find name filename [options] [type ] {-ls |[-exec } {}\;]

    options with -[ac]time:

    +nMore than N days

    -n - Less than N days

    n - Exactly N days

  • 8/11/2019 Unix Training2

    18/52

    Unix Commands (contd)

    cut - Cut a portion of a file

    cut [options] filename

    Options:-c - Cut columns in file based on position

    -d Extract fields using the

    delimiter

    -fCut fields based on the delimiterspecified. Default delimiter is

  • 8/11/2019 Unix Training2

    19/52

    Unix Commands (contd)

    sort - Sort the contents of a file

    sort [options]

    Options:

    -uSort and write unique records

    -fFold-case (Case insensitive)

    -rReverse Sorting

    -bIgnore leading blanks

    -o - Create output file with the sorted

    content

  • 8/11/2019 Unix Training2

    20/52

    Unix Commands (contd)

    uniq - Filter/ report uniq lines ofa file

    uniq [options] filename

    Displays the unique records in a file

    Options:

    -cCount of duplicate entries

    -dDisplay distinct records [which are

    duplicated]-uDisplay non-duplicate records

    Pre-requisite: Input file must be sorted to

    use uniq command

  • 8/11/2019 Unix Training2

    21/52

    Unix Commands (contd)

    diff - Find differencebetween two text files

    diff cal - Display the calendarof an year

    cal [[month] year]

    more - Displays the filepage-wisemore

  • 8/11/2019 Unix Training2

    22/52

    Unix Commands (contd)

    tar - Tape Archive

    tar [options] tar_filename

    Options:

    -cCreate tar file

    -vVerbose

    -fFile [Use the tar filename]

    -xExtract

    -tToC of tar file

  • 8/11/2019 Unix Training2

    23/52

    Unix Commands (contd)

    kill - Kill a Unix Processkill [-9] -9It is the sure kill command

    wc - count the number ofwords, characters and lines in afile

    wc [options] Options:-lReturn the number of lines in a

    file-wReturn the number of words

    in a file-c - Return the number of

    characters in a file

  • 8/11/2019 Unix Training2

    24/52

    Unix Commands (contd)

    who - List the users currentlylogged in

    who [am i]

    am iThe current users infois displayed

    which - Locate a commandwhich

    sleep - Suspend execution for

    n secondssleep

  • 8/11/2019 Unix Training2

    25/52

    Unix Commands (contd)

    touch - touch/ create a file

    touch

    chmod - Change the access

    permissions on a filechmod

    chmod [-R]

    Permissions: 4Read, 2Write,

    1Execute

    ugoUser, Group and Others

    +/- r, w, xRead, Write, Execute

  • 8/11/2019 Unix Training2

    26/52

    Unix Commands (contd)

    tacReverse the contents ofthe file

    tac filename

    date - Display the System

    date, Time and Zonedate +%m/%d/%y

    head - Display the first 10 linesof a file

    head [-n] tail - Display the last 10 lines ofa file

    tail [+/-n]

  • 8/11/2019 Unix Training2

    27/52

    Unix Commands (contd)

    id - Display the current users

    IDentification

    hostname - Display the hostname ofthe Unix system

    passwd - Change the password for

    the current user

    stty - Set Terminal Type

    stty erase ^H kill ^U

  • 8/11/2019 Unix Training2

    28/52

    Unix Commands (contd)

    nohup - No hang up

    nohup

    man - Manual pages for Unixcommands

    man

    compress - Compress the file

    contents

    compress

  • 8/11/2019 Unix Training2

    29/52

    Pipes, Redirection and Filters

    Accomplish complex tasks bycombining Unix commands

    RedirectionTransfer the output of acommand or content of a file toanother file or a command

    Pipes - A form of redirection used inUnix that sends the output of one

    program to another program forfurther processing. The symbol | isused for this

  • 8/11/2019 Unix Training2

    30/52

    Pipes, Redirection and Filters (contd)

    Input and OutputRedirection:

    Input RedirectionContents of a command arereceived as input to a file

    Output RedirectionContents of a command are sentas output to a file

  • 8/11/2019 Unix Training2

    31/52

    Pipes, Redirection and Filters (contd)

    Filtersawk and sed

    awk - Aho, Weinberger and Kernighan

    General purpose programming

    language for text processing

    sedStream Editor

    Both the filters does not impact the

    input files

  • 8/11/2019 Unix Training2

    32/52

    Pipes, Redirection and Filters (contd)

    AWK ProgrammingStructure of AWK programs

    pattern { action }

    Examples:

    print $1Displays the first field of the currentline

    print $1, $3

    Displays the first and third fields ofthe current line, separated by a

    predefined string called the outputfield separator (OFS) whose defaultvalue is a single space character

  • 8/11/2019 Unix Training2

    33/52

    Pipes, Redirection and Filters (contd)

    sedStream editor is used to perform basic texttransformations on an input stream. An e.g.:

    $ cat pear.txt

    jewel wormjewerly wormy worm jewelhay jewel jewel

    kitten

    $ cat pear.txt | sed -e "s/jewel/rock/"rock wormjewerly wormy worm rockhay rock jewelkitten

    $ cat pear.txt | sed -e "s/e/WWW/g"jWWWwWWWl wormjWWWwWWWrly wormy worm jWWWwWWWlhay jWWWwWWWl jWWWwWWWlkittWWWn$

  • 8/11/2019 Unix Training2

    34/52

    The visual Editor

    Using the "vi"sual EditorTwo modes of vi

    Command ModeInsert Mode

    Starting vivi filename edit a file named "filename"

    Entering texti insert text left of cursorI insert text in line beginninga append text right of cursorA append text at end of line

    Moving the cursorh left one spacej down one line

    k up one linel right one space

  • 8/11/2019 Unix Training2

    35/52

    Using the "vi"sual Editor (contd)

    Basic Editing

    nx delete n characters

    nX delete n characters before cursor

    ndw delete n words forward

    ndb delete n words backward

    ndd delete n lines

    D delete characters from cursor to end of line

    nr replace n characters under cursor

    ncw replace n words

    C change text from cursor to end of line

    o insert blank line below cursor

    (ready for insertion)

    O insert blank line above cursor

    (ready for insertion)

    nJ join n succeeding lines to current cursor line

    u undo last change

    U restore current line

  • 8/11/2019 Unix Training2

    36/52

    Using the "vi"sual Editor (contd)

    Moving around in a filenw forward word by word

    nb backward word by word

    $ to end of line

    0 (zero) to beginning of line

    - Goto previous line

    +/ enter Goto next line

    H to top line of screenM to middle line of screen

    L to last line of screen

    nG to line "n" of file

    f scroll forward one screen

    b scroll backward one screen

    d scroll down one-half screen

    u scroll up one-half screen

    / Search forward

    ? Search backward

    n repeat last search in same direction

    N repeat last search in opposite direction

  • 8/11/2019 Unix Training2

    37/52

    Using the "vi"sual Editor (contd)

    Closing and saving a file

    ZZ save and quit

    :wq Same as ZZ

    :w save and resume

    edit

    :q Quit

    :q! Quit discarding

    changes

  • 8/11/2019 Unix Training2

    38/52

    Shell Scripting Overview

    Types of ShellsBourne Shell: This is the original Unixshell written by Steve Bourne of BellLabs

    C Shell: This shell was written at theUniversity of California, Berkeley. Itprovides a C-like language withwhich to write shell scripts andhence its name

    Korn Shell: This shell was written by

    David Korn of Bell labs. It is nowprovided as the standard shell onUnix systems. It is the most efficientshell.

  • 8/11/2019 Unix Training2

    39/52

    Shell Scripting Overview (contd)

    Creating a Simple Shell ScriptTo list the users currently logged in:

    who | cut -c1-8 | sort -u

    vi disp_loggedusers

    #!/bin/ksh

    who | cut -c1-8 | sortu

    :wq

    $ chmod 755 disp_loggedusers

    $ disp_loggedusers

  • 8/11/2019 Unix Training2

    40/52

    Shell Scripting Overview (contd)

    Shell VariablesCreating Shell Variables

    =value

    No specific data types by default

    No space before and after = signTo access the value use$

    Example:

    var1=Hi

    var2=Krishnaecho $var1, $var2

    Output:

    Hi, Krishna

  • 8/11/2019 Unix Training2

    41/52

    Shell Scripting

    Declare an integer variabletypeseti a_number=5

    Declare an unsigned integertypesetui a_number=18

    Declare variable to store lowercase

    typesetl str1=HELLOecho $str1Output:hello

    Declare variable to store uppercasetypesetu str1=hello

    echo $str1Output:HELLO

  • 8/11/2019 Unix Training2

    42/52

    Shell Scripting

    Sample shell to display theProcess ID Details

    #!/bin/ksh

    echo This shells process id: $$

    echo This shells parent id: $PPIDexit 0

    Finding the exit status of a shell

    echo $?

    Reading Inputs from User

    read ,

  • 8/11/2019 Unix Training2

    43/52

    Shell Scripting

    Decision Making and Control Flow

    ifelse condition checking

    Logical and Relational Operators

    while loop

    for loop

    case construct

    until commandBreaking the loop

  • 8/11/2019 Unix Training2

    44/52

    Shell Scripting

    Decision Making andControl Flow (contd)

    ifelse condition checking

    if [ ]

    then< true statements>

    elif [ ]

    then

    else

    fi

  • 8/11/2019 Unix Training2

    45/52

    Shell Scripting

    Decision Making andControl Flow (contd)Logical Operators

    -aAnd

    -oOr!Not

    Relational Operators==/ -eqEqual!=/ -neNot equal=/ -geGreater than or equal to>/ -gtGreater than

  • 8/11/2019 Unix Training2

    46/52

    Shell Scripting

    Decision Making and ControlFlow (contd)

    Looping constructs

    while loop

    while [ ]do

    done

    for loopfor in do

    done

  • 8/11/2019 Unix Training2

    47/52

    Shell Scripting

    Decision Making and ControlFlow (contd)

    Looping constructsuntil loopuntil [ ]do

    done

    E.g.:i=0until [ $i -gt 4 ]do

    i=`expr $i + 1`echo $i

    done

    1

    23

    4

    5

  • 8/11/2019 Unix Training2

    48/52

    Shell Scripting

    Decision Making andControl Flow (contd)

    case construct

    case $var in

    opt1) statements;;break;;

    opt2) statements;;

    break;;

    # Default option

    *) default_statements;;

    esac

  • 8/11/2019 Unix Training2

    49/52

    Shell Scripting

    Decision Making andControl Flow (contd)

    case construct

    case $var in

    opt1) statements;;break;;

    opt2) statements;;

    break;;

    # Default option

    *) default_statements;;

    esac

  • 8/11/2019 Unix Training2

    50/52

    Shell Scripting

    Breaking from the loopE.g.:

    i=0

    until [ $i -gt 4 ]

    do

    i=`expr $i + 1`

    echo $i

    break

    done

    Will produce the following output:

    1

  • 8/11/2019 Unix Training2

    51/52

    Scheduling jobs in Unix

    crontabs and cronjobs

    Restrictions: cron.allow and cron.deny

    Crontab commands:

    crontabl, -e

    Crontab

    E.g.:

    30 18 * * * /home/user/t.ksh

  • 8/11/2019 Unix Training2

    52/52

    Scheduling jobs in Unix

    Interpreting a cron entry:* * * * * command to be executed- - - - -| | | | |

    | | | | +----- day of week (0 - 6)| | | | (Sunday=0)| | | +------- month (1 - 12)| | +--------- day of month (1 - 31)

    | +----------- hour (0 - 23)+------------- min (0 - 59)


Recommended