shell 练习1编写Shell条件句练习

Posted 旅行者-Travel

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell 练习1编写Shell条件句练习相关的知识,希望对你有一定的参考价值。

实例一、比较两个整数大小

#!/bin/bash
while true
do
    read -p "Please input two int nums:" a b
    expr $a + $b + 0 >/dev/null 2>&1     #判断是否为整数
    if [ $? -eq 0 ];then                 #返回值是否为0
        if [ $a -gt $b ];then
           echo " $a > $b"
           exit 0
        elif [ $a -lt $b ];then
           echo " $a < $b "
           exit 0
        else [ $a -eq $b ]
           echo " $a = $b "
           exit 0
        fi
    fi
done

 实例二 、测试语句和if条件句混合使用,通过目录安装lamp,lnmp

目录安装:
#!/bin/sh
path=/server/scrips
[ ! -d "$path" ] && mkdir $path -p
#menu
cat <<END
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    pls input the num you wang:
END
read num
expr $num + 1 &>/dev/null
if [ $? -ne 0 ];then
   echo "the num you input must be {1|2|3}"
   exit 1
fi
[ $num -eq 1 ]&&{
    echo "start install lamp."
    sleep 2;
    [ -x "$path/lamp.sh" ]||{
    echo " does not exist or can not be exec."
    exit 1
    }
    $path/lamp.sh
    #source $path/lamp.sh
    exit $?
}
if [ $num -eq 2 ];then
   echo "start install lnmp."
   sleep 2;
   if [ -x "$path/lamp.sh" ];then
   $path/lnmp.sh
   #source $path/lamp.sh
   exit $?
   else 
       echo " does not exist or can not be exec. "
       exit
   fi
fi
if [ $num -eq 3 ];then
    echo bye
    exit 3
fi
[[ ! $num =~[1-3] ]] &&{
    echo "the num you input must be {1|2|3}"
    echo "Input ERROR"
    exit 4
}

 

 

以上是关于shell 练习1编写Shell条件句练习的主要内容,如果未能解决你的问题,请参考以下文章

shell练习

shell语法习题练习进阶版

shell练习题

shell练习题

shell脚本条件语句练习

shell脚本小练习