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 :

相关推荐