UNIX+Shell Scripting+Basics

download UNIX+Shell Scripting+Basics

of 48

Transcript of UNIX+Shell Scripting+Basics

  • 8/6/2019 UNIX+Shell Scripting+Basics

    1/48

    Shell

    UNIX

  • 8/6/2019 UNIX+Shell Scripting+Basics

    2/48

    Agenda

    Shell

    bash

    Shell Environment Shell Programming

  • 8/6/2019 UNIX+Shell Scripting+Basics

    3/48

    WhatWhat is a shell?WhatWhat is a shell?

    Interactive UserInterface with anoperating system

  • 8/6/2019 UNIX+Shell Scripting+Basics

    4/48

    Types of Shell

    The Bourne Shell ( /bin/sh)

  • 8/6/2019 UNIX+Shell Scripting+Basics

    5/48

    Types of Shell

    The C Shell ( /bin/csh)

  • 8/6/2019 UNIX+Shell Scripting+Basics

    6/48

    Types of Shell

    The Bourne Again Shell ( /bin/bash)

  • 8/6/2019 UNIX+Shell Scripting+Basics

    7/48

    How Shell Works?

    INPUT

    shell

    OUTPUT ERROR

  • 8/6/2019 UNIX+Shell Scripting+Basics

    8/48

    So, Shell is any program that cantake input and interacts with OS

  • 8/6/2019 UNIX+Shell Scripting+Basics

    9/48

    Shell Basics

    FilePathAbsolute Path (/etc/X11/xserver/SecurityPolicy)

    Relative Path (../../file_name)

  • 8/6/2019 UNIX+Shell Scripting+Basics

    10/48

    Basics

    Special Symbols ~ ( home Directory)

    . ( current Directory) .. ( parent Directory)

    ` ( back tick )

  • 8/6/2019 UNIX+Shell Scripting+Basics

    11/48

    Basics

    Redirections(,>>) Input Redirection

    Output Redirection

  • 8/6/2019 UNIX+Shell Scripting+Basics

    12/48

  • 8/6/2019 UNIX+Shell Scripting+Basics

    13/48

    Basics

    Input Redirection cat < /etc/resolv.conf

    Input Piping echo I Love Unix | wc -w

  • 8/6/2019 UNIX+Shell Scripting+Basics

    14/48

    Basics

    Error Redirection $ls -l /etc/not_existing_file

    ls: /etc/reslo.conf: No such file or directory $ls -l /etc/not_existing_file 2> error.log

    $cat error.log

    ls: /etc/reslo.conf: No such file or directory

  • 8/6/2019 UNIX+Shell Scripting+Basics

    15/48

    Basics

    Output & Error Redirection $ ls -ld /etc/resolv.conf /etc/noname &> output.txt

    $echo output.txt ls: /etc/noname: No such file or directory

    -rw-r--r-- 1 root root 50 2009-04-12 09:59/etc/resolv.conf

  • 8/6/2019 UNIX+Shell Scripting+Basics

    16/48

    Basics

    The backtick (`)

    allows to assign output of a shell

    command to a variable.

    $testing=`date`

    $echo testing

    Mon Apr 20 15:07:51 BDT 2009

  • 8/6/2019 UNIX+Shell Scripting+Basics

    17/48

    Environment Variables

    Local Variable

    Global Variable$ set | moreBASH=/bin/bashBASH_ARGC=()BASH_ARGV=()BASH_COMPLETION=/etc/bash_completionBASH_COMPLETION_DIR=/etc/bash_completion.dBASH_LINENO=()

    BASH_SOURCE=()BASH_VERSINFO=([0]="3" [1]="2" [2]="13" [3]="1" [4]="release" [5]="i486-pc-linux-gnu")BASH_VERSION='3.2.13(1)-release'.......

  • 8/6/2019 UNIX+Shell Scripting+Basics

    18/48

    Setting & Unsetting EnvironmentVariables

    $$test=testingtest=testing$$echo $testecho $testtestingtesting$$unset $testunset $test

    $$echo $testecho $test

  • 8/6/2019 UNIX+Shell Scripting+Basics

    19/48

    Creating Global Variable

    $test=it is not global variable$/bin/bash$echo $test

    $test=it is global$export test$/bin/bash$echo $testit is global

  • 8/6/2019 UNIX+Shell Scripting+Basics

    20/48

    What is a Shell Script? A Text File

    #hello.sh

    echo hello, world

    Running the Script

    $chmod +x hello.sh$./hello.shhello, world

  • 8/6/2019 UNIX+Shell Scripting+Basics

    21/48

    Exit Status

    $?

    0 is True

    % ls /does/not/exist

    % echo $?

    1

    % echo $?

    0

  • 8/6/2019 UNIX+Shell Scripting+Basics

    22/48

    Exit Status: exit

    % cat > test.sh

  • 8/6/2019 UNIX+Shell Scripting+Basics

    23/48

    Logic: test

    % test 1 -lt 10

    % echo $?

    0% test 1 == 10

    % echo $?

    1

  • 8/6/2019 UNIX+Shell Scripting+Basics

    24/48

    Logic: test

    test

    [ ]

    [ 1 lt 10 ] [[ ]]

    [[ thisstring =~ this ]]

    (( )) (( 1 < 10 ))

  • 8/6/2019 UNIX+Shell Scripting+Basics

    25/48

    Logic: test

    [ -f /etc/passwd ]

    [ ! f /etc/passwd ]

    [ -f /etc/passwd a f /etc/shadow ]

    [ -f /etc/passwd o f /etc/shadow ]

  • 8/6/2019 UNIX+Shell Scripting+Basics

    26/48

    An aside: $(( )) for Math

    % echo $(( 1 + 2 ))

    3

    % echo $(( 2 * 3 ))6

    % echo $(( 1 / 3 ))

    0

  • 8/6/2019 UNIX+Shell Scripting+Basics

    27/48

    Logic: if

    if something

    then

    :

    # elif a contraction of else if:

    elif something-elsethen

    :

    else

    then:

    fi

  • 8/6/2019 UNIX+Shell Scripting+Basics

    28/48

    Logic: if

    if [ $USER eq borwicjh ]

    then

    :

    # elif a contraction of else if:elif ls /etc/oratab

    then

    :

    else

    then

    :

    fi

  • 8/6/2019 UNIX+Shell Scripting+Basics

    29/48

    Logic: if

    # see if a file exists

    if [ -e /etc/passwd ]

    thenecho /etc/passwd exists

    else

    echo /etc/passwd not found!fi

  • 8/6/2019 UNIX+Shell Scripting+Basics

    30/48

    Logic: for

    for i in 1 2 3

    do

    echo $idone

  • 8/6/2019 UNIX+Shell Scripting+Basics

    31/48

    Logic: for

    for i in /*

    do

    echo Listing $i:ls -l $i

    done

  • 8/6/2019 UNIX+Shell Scripting+Basics

    32/48

    Logic: for

    for i in /*

    do

    echo Listing $i:ls -l $i

    done

  • 8/6/2019 UNIX+Shell Scripting+Basics

    33/48

    Logic: for

    for i in /*

    do

    echo Listing $i:ls -l $i

    done

  • 8/6/2019 UNIX+Shell Scripting+Basics

    34/48

    Logic: C-style for

    for (( expr1 ;

    expr2 ;expr3 ))

    do

    listdone

  • 8/6/2019 UNIX+Shell Scripting+Basics

    35/48

    Logic: C-style for

    LIMIT=10

    for (( a=1 ;

    a

  • 8/6/2019 UNIX+Shell Scripting+Basics

    36/48

    Logic: while

    while something

    do:

    done

  • 8/6/2019 UNIX+Shell Scripting+Basics

    37/48

    Logic: while

    a=0; LIMIT=10

    while [ "$a" -lt "$LIMIT" ]

    doecho -n "$a

    a=$(( a + 1 ))

    done

  • 8/6/2019 UNIX+Shell Scripting+Basics

    38/48

    Counters

    COUNTER=0

    while [ -e $FILE.COUNTER ]

    doCOUNTER=$(( COUNTER + 1))

    done

  • 8/6/2019 UNIX+Shell Scripting+Basics

    39/48

    Running Programs

  • 8/6/2019 UNIX+Shell Scripting+Basics

    40/48

    Searching: grep

    % grep rayra /etc/passwd

    % grep r rayra /etc

    % grep r RAYRA /etc% grep ri RAYRA /etc

    % grep rli rayra /etc

  • 8/6/2019 UNIX+Shell Scripting+Basics

    41/48

    Searching: find

    % find /home/cention

    -name *.lis

    [all files matching *.lis]% find /home/cention

    -mtime -1 name *.lis

    [*.lis, if modified within 24h]% man find

  • 8/6/2019 UNIX+Shell Scripting+Basics

    42/48

    Searching: locate

    % locate .lis

    [files with .lis in path]

    % locate log[also finds /var/log/messages]

  • 8/6/2019 UNIX+Shell Scripting+Basics

    43/48

    Make Your Life Easier

    TAB completion

    Control+R

    history cd -

    Study a UNIX Editor

  • 8/6/2019 UNIX+Shell Scripting+Basics

    44/48

    pushd/popd

    % cd /tmp

    % pushd /var/log

    /var/log /tmp

    % cd ..

    % pwd

    /var

    % popd

    /tmp

  • 8/6/2019 UNIX+Shell Scripting+Basics

    45/48

    Monitoring processes

    ps

    ps ef

    ps u oracle ps Csshd

    man ps

  • 8/6/2019 UNIX+Shell Scripting+Basics

    46/48

    Passing Arguments

    % cat > test.sh

  • 8/6/2019 UNIX+Shell Scripting+Basics

    47/48

    Scheduling Jobs

    % crontab -l

    00 * * * daily-midnight-job.sh

    0 * * * * hourly-job.sh* * * * * every-minute.sh

    01 * * 01AM-on-sunday.sh

    % EDITOR=vi crontab e% man 5 crontab

  • 8/6/2019 UNIX+Shell Scripting+Basics

    48/48