shell两种循环《一》(小白)

来源:互联网 发布:js根据name赋值 编辑:程序博客网 时间:2024/05/29 23:24

while循环

while循环:
while (条件)
do
动作
done

#!/bin/bashwhile :do    read -p 'please input your name: ' name    read -p 'please input your password: ' pwd    if [ $name = 'batman' ] && [ $pwd = '123' ]        then            echo 'login sucessful'            break #continue    fidone-----------------用户名密码错误---------------------------please input your name: sbplease input your password: 345please input your name: dashabiplease input your password: 765please input your name: jihlplease input your password: 839828please input your name: -------------用户名密码正确-------------------------------please input your name: batmanplease input your password: 123login sucessful
#!/bin/bashi=1while ((i<10))doecho $i((i++))done-------------------------输出结果----------------------[root@bogon ~]# ./b.sh 123456789

for循环

for (变量) in( 列表)
do
动作
done

#!/bin/bashfor loop in 1 2 3 4 5do    echo "The value is: $loop"done-----------------结果----------------------------[root@bogon ~]# ./f.shThe value is: 1The value is: 2The value is: 3The value is: 4The value is: 5--------------例子------------------------------检查内网存活的IP#!/bin/bashfor i in {1..254}do            (ping -W 1 -c 1 192.168.1.$i &> /dev/null && echo 192.168.1.$i) &    done

shell中的for常用in列表方式:

for i in 1 2 3for i in {1,2,3}for i in {1..9}for i in {9..1}for i in {a..z}for i in {A..Z}for i in {X..Z}for i in $(cmd)for i in $(find ...)