实例(二)

    技术2022-05-19  17

    1.test和[]命令

    #!/bin/shecho

    if test -z "$1"then echo "No command-line arguments."else echo "First command-line arguments is $1."fi

    echo

    if /usr/bin/test -z "$1"then echo "No command-line arguments."else echo "First command-line argument is $1."fi

    echoif [ -z "$1" ]then echo "No command-line arguments."else echo "First command-line arguments is $1."fi

    if /usr/bin/[ -z "$1" ]then echo "No command-line arguments."else echo "First command-line argument is $1."fi

    echoexit 0

     

    2.(())号命令

    #! /bin/sh(( 0 ))echo "Exit status of /"(( 0 ))/" is $?."

    (( 1 ))echo "Exit status of /"(( 1 ))/" is $?."

    (( 5 > 4 ))echo "Exit status of /"(( 5 > 4 ))/" is $?."

    (( 5 > 9 ))echo "Exit status of /"(( 5 > 9 ))/" is $?."

    (( 5-5 ))echo "Exit status of /"(( 5 - 5))/" is $?."

    (( 5/4 ))echo "Exit status of /"((5/4))/" is $?."

    (( 1/2 ))echo "Exit status of /"((1 /2 ))/" is $?."

    (( 1/0 )) 2>/dev/nullecho "Exit status of /"(( 1/0 ))/" is $?."exit 0

     

    3.测试断掉的连接文件

    #! /bin/sh[ $# -eq 0 ] && directorys=`pwd` || directorys=$@echo $@linkchk () {for element in $1/*do[ -h "$element" -a ! -e "$element" ] && echo /"$element/"[ -d "$element" ] && linkchk $elementdone}

    for directory in $directorysdoif [ -d $directory ]then linkchk $directoryelse echo "$directory is not a directory" ehco "Usage: $0 dir1 dir2 ..."fi

    done

    exit 0

     

    4.算术和字符串比较

    #! /bin/sha=4b=5

    echoif [ "$a" -ne "$b" ]then  echo "$a is not equal to $b"  echo "(arithmetic comparison)"fi

    echo

    if [ "$a" != "$b" ]then  echo "$a is not equal to $b."  echo "(string comparison)"fi

    echoexit 0

     

    5.检查字符串是否为null

     

    #! /bin/shif [ -n $string1 ]then echo "String /"string/" is not null."else echo "String /"string/" is null."fi

    if [ -n "$string1" ]then echo "String /"string/" is not null."else echo "String /"string/" is null."fi

    if [ $string1 ]then echo "String /"string/" is not null."else echo "String /"string/" is null."fi

    echo

    string1=initialized

    if [ $string1 ]then echo "String /"string/" is not null."else echo "String /"string/" is null."fi

    string1="a = b"if [ $string1 ]then echo "String /"string/" is not null."else echo "String /"string/" is null."fi

    echoexit 0


    最新回复(0)