设为首页 加入收藏

TOP

Shell编程学习之判断语句
2014-11-24 12:46:58 来源: 作者: 【 】 浏览:0
Tags:Shell 编程 习之 判断 语句

shell编程学习之控制流


1、if.....the......efif.......efif.....else......fi


bash-3.2# cat test.sh
#!/bin/bash
if [ "10" -lt "8" ]
then
echo "10小于8"
elif [ "10" -lt "9" ]
then
echo "10小于9"
elif [ "10" -lt "11" ]
then
echo "10小于11"
else
echo "再重新比较"
fi
bash-3.2# ./test.sh
10小于11


2、case....in....esac


bash-3.2# cat test.sh
#!/bin/bash
while [ "1"=="1" ]
do
echo clear
echo "------------------------"
echo "1) free -m"
echo "2) uname -a"
echo "3) service httpd restart"
echo "------------------------"
echo -n "enter a numbre 1-3 :"
read i
case $i in
1) free -m
;;
2) uname -a
;;
3) service httpd restart
;;
*) echo "enter a number 1-3:"
;;
esac
done


以下是执行结果:


bash-3.2# ./test.sh
clear
------------------------
1) free -m
2) uname -a
3) service httpd restart
------------------------
enter a numbre 1-3 :1
total used free shared buffers cached
Mem: 503 347 155 0 58 203
-/+ buffers/cache: 85 417
Swap: 1027 0 1027
clear
------------------------
1) free -m
2) uname -a
3) service httpd restart
------------------------
enter a numbre 1-3 :2
Linux linux-3 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux
clear
------------------------
1) free -m
2) uname -a
3) service httpd restart
------------------------
enter a numbre 1-3 :3
停止 httpd:[确定]
启动 httpd:httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[确定]
clear
------------------------
1) free -m
2) uname -a
3) service httpd restart
------------------------
enter a numbre 1-3 :4
enter a number 1-3:
clear
------------------------
1) free -m
2) uname -a
3) service httpd restart
------------------------
enter a numbre 1-3 :


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Shell编程学习之函数 下一篇Shell编程学习之条件测试

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)
·SQL CREATE INDEX 语 (2025-12-25 01:21:45)
·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)