Shell的While循环

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  

 

posted @ 2019-12-05 21:51  mongotea  阅读(1586)  评论(0编辑  收藏  举报