Unix Shell 的 While 循環
博客分類:
· Unix
UnixBashPHPCC++
首先解釋下 unix shell 是什么?
unix shell就是unix系統的命令解釋器,比如你敲個ls,它給你返回當前目錄下的文件、目錄列表,返回這個列表就是shell的工作。
unix shell有哪些種類?
感覺這話有點問題,應該是解釋器有哪些種類?但是也可以說unix shell有哪些種類,因為解釋器不一樣,語法還是稍微有些差別。比較常見的解釋器有:csh,ksh,bash。很多系統默認的就是bash,/usr/bin/sh 就是它。
我使用的是什么shell?
你只需要more一下 /etc/passwd,找到你的用戶的哪一行,看一下就明白了。
shell怎么切換?
這個很簡單,假設你現在想用ksh了,僅僅執行一下: /usr/bin/ksh 或者 /bin/ksh ,你的命令解釋器就變成ksh了。那么在shell編程的時候怎么指定呢?你只需要在第一行加入#!/usr/bin/ksh 或者 #!/bin/ksh 就可以了,其它的雷同.
這里順便說一下 /usr/bin 和 /bin的區別,/bin 會放置一些普通用戶不讓使用的命令,所以它的目錄權限可能會更嚴一些, 如果沒有權限使用這目錄就可以用 /usr/bin 了。
現在言歸正傳:
bash的while loop:
Php代碼
1. 語法:
2. while [ condition ]
3. do
4. command1
5. command1
6. commandN
7. done
8. 示例:
9. #!/bin/bash
10. c=1
11. while [ $c -le 5 ]
12. do
13. echo "Welcone $c times"
14. (( c++ ))
15. done
ksh的while loop:
Php代碼
1. 語法:
2. while [[ condition ]] ; do
3. command1
4. command1
5. commandN
6. done
7.
8. 示例:
9. #!/bin/ksh
10. c=1
11. while [[ $c -le 5 ]]; do
12. echo "Welcome $c times"
13. (( c++ ))
14. done
csh的while loop:
Php代碼
1. while ( condition )
2. commands
3. end
4.
5. #!/bin/csh
6. set yname="foo"
7. while ( $yname != "" )
8. echo -n "Enter your name : "
9. set yname = $<
10. if ( $yname != "" ) then
11. echo "Hi, $yname"
12. endif
13. end