歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> SHELL編程

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 :

Copyright © Linux教程網 All Rights Reserved