hellopasswd
for循环
- 语法:for变量名in条件;do...;done
- 案例1 #!/bin/bash sum=0 for i in
seq 1 100
do sum=$[$sum+$i] echo $i done echo $sum
[root@localhost ~]# cd shell/[root@localhost shell]# vi 1.sh 1 #!/bin/bash 2 for i in `seq 1 10` 3 do 4 echo $i 5 done[root@localhost shell]# sh 1.sh 12345678910
[root@localhost shell]# vi 1.sh 1 #!/bin/bash 2 sum=0 3 for i in `seq 1 100` 4 do 5 sum=$[$sum+$i] 6 done 7 echo $sum[root@localhost shell]# sh 1.sh 5050
想知道for语句的计算过程可以添加一个参数-x
[root@localhost shell]# vi 1.sh 1 #!/bin/bash 2 cd /etc/ 3 for a in `ls /etc/` 4 do 5 if [ -d $a ] 6 then 7 ls -d $a 8 fi 9 done
遍历
[root@localhost shell]# for i in `seq 1 3`; do echo $i; done123[root@localhost shell]# seq 1 3123[root@localhost shell]# for i in 1 2 3; do echo $i; done123
注意
[root@localhost shell]# mkdir user[root@localhost shell]# cd user/[root@localhost user]# touch 1 2[root@localhost user]# touch 3\ 4[root@localhost user]# ls -ltotal 0-rw-r--r--. 1 root root 0 Feb 10 21:04 1-rw-r--r--. 1 root root 0 Feb 10 21:04 2-rw-r--r--. 1 root root 0 Feb 10 21:04 3 4[root@localhost user]# for i in `ls ./`; do echo $i; done1234
以这种形式,for循环会以空格、回车、制表符当成一个分隔符
修改于 180211