티스토리 뷰

반응형

Return Codes

The shell variable ? holds the return code of the last command executed

{

$ echo $?

1

$ adf

sh: adf:  not found.

$ echot $?

sh: echot:  not found.

$ echo $?

127

$ false

$ echo $?

1

$ echo $?

}


test command

test command can evaluate the condition of Integers, Strings, Files [조심! 실습파일은 test 말고 test.sh 등으로 작성, sh는 쉘을 의미]

test - Numeric Test

-lt, -le, -gt, -ge, -eq, -ne (less, less or equal, greater, greater or equal, equal, not equal]

{

$ test 10 -gt 5

$ echo $?

0

$ test 10 -lt 5

$ echo $?

1

$ x=10
$ test $x -gt 5
$ echo $?
0

$ [ $x -gt 5 ]      //test command syntax
$ echo $?
0

}

test - String Test

test str1=str2, str1!=str2 

{

$ x=yes

$ [ $x = yes ]

$ echo $?

0

$ [ $x != yes ]

$ echo $?

1

}

test - File Test

test -f filename or dirname, -d filename or dirname  (-f file?, -d directory?, -r readable? -w writable? -x exist and executable?)

test - Other Operator

-o OR, -a AND, ! NOT, \( \) GROUPING

exit - return code of the last command

$ cat exit_test

echo exiting program now

exit 99

$ exit_test

exiting program now

$ echo $?

99


if 

if [ $x -lt 10 ]

then echo String 1

fi


if-else 

if [ $x -lt 10 ]

then echo String 1

else then echo String 2

fi



if [ $x -lt 10 ]

then echo String 1

else 

if [ $x -gt 5 ]

then echo String 2

else echo String 3

fi

fi


elif

if [ $x -lt 10 ]

then echo String 1

elif [ $x -gt 5 ]

then echo String2

else echo String3

fi


case 

case $ANS in

yes)  echo O.K. 

;;                    //break;

no)  echo no go

;;

*) echo keep trying

;;

esac



$ vi menu_with_case

"menu_with_case" [New file]

echo type d date, w who, l ls

read choice

case $choice in

    [dD]*)  date    ;;

    [wW]*)  who     ;;

    l*|L*)  ls      ;;

    *)  echo invalid ;;

esac



exercise

{

$ vi greeting1

echo type [A] or [B,b] or [C,quit]
read ANS
case $ANS in
    A*) echo good morning   ;;
    [Bb]*) echo good after noon ;;
    C*|quit*) exit ;;
    *) echo "error!"
    exit 99 ;;
esac

}
{

$ vi greeting2

time=$(date +%H)
if [ $time -lt 12 ]
    then echo good morning
elif [ $time -ge 12 -a $time -lt 18 ]
    then echo good afternoon
else
    echo good evening
fi

}

{

$ vi m16_9.sh

echo display current directory? [yes or no]

read ANS

if [ "$ANS" = yes ]

    then echo $(pwd) | ls

elif [ "$ANS" = no ]

    then echo what directory would you like to see?

    read ANS

    if [ -d $ANS ]

        then cd $ANS

    else

        echo directory doesn\'t exist

    fi

fi

}


반응형

'IoT 과정' 카테고리의 다른 글

HPUX - Managing Users and Groups  (0) 2017.06.26
HPUX - Shell Programming - Loops  (0) 2017.06.23
HPUX - Shell Programming  (0) 2017.06.23
HPUX - Process Control  (0) 2017.06.22
HPUX - Pipes  (0) 2017.06.22