码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2018-06-24 16:02:24      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:bsp   lamp   can   exec   style   server   nbsp   one   serve   

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

#!/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条件句练习

标签:bsp   lamp   can   exec   style   server   nbsp   one   serve   

原文地址:https://www.cnblogs.com/yangleitao/p/9206567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!