Unix Shell 的 While 循环

首先解释下 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  
语法:
while [ condition ] 
do
	command1
	command1
	commandN
done
示例:
#!/bin/bash
c=1
while [ $c -le 5 ]
do
	echo "Welcone $c times"
	(( c++ ))
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  
语法:
while [[ condition ]] ; do
	command1
	command1
	commandN
done

示例:
#!/bin/ksh
c=1
while [[ $c -le 5 ]]; do
	echo "Welcome $c times"
	(( c++ ))
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值