• if condition
    Syntax:
    if condition
    then
               command1 if condition is true or if exit status of condition is 0 (zero)
              
              
    fi
    Ex:
    #!/bin/sh
    # Script to test rm command and exist status
    if rm $1
    then
        echo "$1 file deleted"
    fi
  • Condition is nothing but comparison between two values. For compression you can use test or [ expr ] statements or even exist status can be also used.
  • test command or [ expr ], test command or [ expr ] is used to see if an expression is true, and if it is true it return zero(0), otherwise returns nonzero for false.
    Syntax: test expression OR [ expression ]
    Ex:
    #!/bin/sh
    # Script to see whether argument is positive
    if test $1 -gt 0        /* Or, if [ $1 –gt 0 ] */
    then
        echo "$1 number is positive"
    fi
  • test or [ expr ] works with
    • Integer ( Number without decimal point)
    • File types
    • Character strings
  • Operators in shell script

Mathematical operator in shell script

Meaning

Normal arithmetical / mathematical statements

In shell

test statement

[ expr ] statement

-eq

Is equal to

5 == 6

if test 5 -eq 6

if [ 5 -eq 6 ]

-ne

Is not equal to

5 != 6

if test 5 -ne 6

if [ 5 -ne 6 ]

-lt

Is less than

5 < 6

if test 5 -lt 6

if [ 5 -lt 6 ]

-le

Is less than or equal to

5 <= 6

if test 5 -le 6

if [ 5 -le 6 ]

-gt

Is greater than

5 > 6

if test 5 -gt 6

if [ 5 -gt 6 ]

-ge

Is greater than or equal to

5 >= 6

if test 5 -ge 6

if [ 5 -ge 6 ]

  • String comparison

Operator

Meaning

string1 = string2

string1 is equal to string2

string1 != string2

string1 is NOT equal to string2

string1

string1 is NOT NULL or not defined

-n string1

string1 is NOT NULL and does exist

-z string1

string1 is NULL and does exist

  • Tests for file and directory types

Test

Meaning

-s file

Non empty file

-f file

Is file exist or normal file and not a directory

-d dir

Is directory exist and not a file

-w file

Is writeable file

-r file

Is read-only file

-x file

Is file is executable

  • Logical operators

Operator

Meaning

!expression

Logical NOT

expression1 –a expression2

Logical AND

expression1 –o expression2

Logical OR

  • if…else…fi
    Syntax:
    if condition
    then
               condition is zero (true), execute all commands up to else statement
    else
               if condition is not true then execute all commands up to fi
    fi
    Ex:
    #!/bin/sh
    # Script to see whether argument is positive or negative
    if [ $# -eq 0 ]
    then
        echo "$0 : You must give/supply one integers"
       
    exit 1
    fi
    if test $1 -gt 0
    then
        echo "$1 number is positive"
    else
        echo "$1 number is negative"
    fi
  • Multilevel if-then-else
    Syntax:
    if condition1
    then
               condition1 is zero (true), execute all commands up to elif statement
    elif condition2
    then
               condition2 is zero (true), execute all commands up to elif statement
    elif condition3
    then
               condition3 is zero (true), execute all commands up to elif statement
    else
               None of the above condition1, condition2, condition3 is true execute all commands up to fi
    fi
  • Computer can repeat particular instruction again and again, until particular condition satisfies. A group of instruction that is executed repeatedly is called a loop.
  • for loop
    Syntax:
    for { variable_name } in { list }
    do
               execute one for each item in the list until the list is not finished (And repeat all statement between do and done)
    done
    Or
    for (( expr1; expr2; expr3 ))
    do
               repeat all statements between do and done until expr2 is TRUE
    done
    Ex:
    #!/bin/sh
    #Script to test for loop
    if [ $# -eq 0 ]
    then
        echo "Error - Number missing form command line argument"
       
    echo "Syntax : $0 number"
       
    echo "Use to print multiplication table for given number"
       
    exit 1
    fi
    n=$1
    for i in 1 2 3 4 5 6 7 8 9 10
    do
        echo "$n * $i = `expr $i \* $n`"
    done
    Ex:
    #!/bin/sh
    for (( i = 1; i <= 9; i++ ))             ### Outer for loop ###
    do
        for (( j = 1 ; j <= 9; j++ ))            ### Inner for loop ###
       
    do
           
    tot=`expr $i + $j`
           
    tmp=`expr $tot % 2`
           
    if [ $tmp -eq 0 ]; then
               
    echo -e -n "\033[47m "
           
    else
               
    echo -e -n "\033[40m "
           
    fi
       
    done
       
    echo -e -n "\033[40m"              #### set back background color to black
       
    echo ""                                         #### print the new line ###
    done
  • while loop
    Syntax:
    while [ condition ]
    do
               command1
              
              
    done
    Ex:
    #!/bin/sh
    #Script to test while statement
    if [ $# -eq 0 ]
    then
        echo "Error - Number missing form command line argument"
       
    echo "Syntax : $0 number"
       
    echo " Use to print multiplication table for given number"
       
    exit 1
    fi
    n=$1
    i=1
    while [ $i -le 10 ]
    do
        echo "$n * $i = `expr $i \* $n`"
       
    i=`expr $i + 1`
    done
  • The case statement
    Syntax:
    case $variable_name in          /* the $variable_name is compared against the patterns until a match is found. */
               pattern1)     command
                                   
                                    command;;
               pattern2)     command
                                   
                                    command;;
               patternN)    command
                                   
                                    command;;
               *)                  command
                                   
                                    command;;
    esac
    Ex:
    # if no vehicle name is given i.e. -z $1 is defined and it is NULL, if no command line arg
    if [ -z $1 ]
    then
        rental="*** Unknown vehicle ***"
    elif [ -n $1 ]
    then   # otherwise make first arg as rental
        rental=$1
    fi
    case $rental in
        "car")           echo "For $rental Rs.20 per k/m";;
       
    "van")          echo "For $rental Rs.10 per k/m";;
       
    "jeep")        echo "For $rental Rs.5 per k/m";;
       
    "bicycle")    echo "For $rental 20 paisa per k/m";;
       
    *)                  echo "Sorry, I can not gat a $rental for you";;
    esac
  • Debug the shell script.
    sh option { shell-script-name }
    • -v Print shell input lines as they are read.
    • -x After expanding each simple-command, bash displays the expanded value of PS4 system variable, followed by the command and its expanded arguments.
arrow
arrow
    全站熱搜

    nix 發表在 痞客邦 留言(0) 人氣()