标签:back stdin ack code backup use 表达 格式 sed
shell中的循环语法
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
1 for 变量 in 值1 值2 值3 ...
2 do
3 源代码
4 done
1 for (( 初始值;循环控制条件;变量变化 ))
2 do
3 源代码
4 done
1 [root@yinzhengjie backup]# more for1.sh
2 #!/bin/bash
3 #@author :yinzhengjie
4 #blog:http://www.cnblogs.com/yinzhengjie
5 #EMAIL:y1053419035@qq.com
6
7 for i in 1 2 3
8 do
9 echo "第$i次循环!"
10 done
11 [root@yinzhengjie backup]#
1 [root@yinzhengjie backup]# more for3.sh
2 #!/bin/bash
3 #@author :yinzhengjie
4 #blog:http://www.cnblogs.com/yinzhengjie
5 #EMAIL:y1053419035@qq.com
6
7 sum=0
8 read -t 30 -p "Please enter the start number>>>" StartNumber
9 read -t 30 -p "Please enter an end number>>>" EndNumber
10
11 for ((i=$StartNumber;i<=$EndNumber;i=i+1))
12 do
13 sum=$(($sum+$i))
14 done
15
16 echo "从$StartNumber加到$EndNumber的总和是:$sum"
17 [root@yinzhengjie backup]#
1 [root@yinzhengjie backup]# more useradd.sh
2 #!/bin/bash
3 #@author :yinzhengjie
4 #blog:http://www.cnblogs.com/yinzhengjie
5 #EMAIL:y1053419035@qq.com
6
7
8 read -p "Please input username(default "yinzhengjie"):" -t 30 username
9 read -p "Please input the number of users:" -t 30 UsersNumber
10 read -p "Please input the password of users:" -t 30 password
11
12 if [ -z $username ]
13 then
14 username=yinzhengjie
15 fi
16
17
18 if [ ! -z "$username" -a ! -z "$UsersNumber" -a ! -z "$password" ]
19 then
20 y=$(echo $UsersNumber|sed ‘s/[0-9]//g‘)
21 if [ -z "$y" ]
22 then
23 for (( i=1;i<=$UsersNumber;i=i+1 ))
24 do
25 useradd "$username$i" &>/dev/null
26 echo $password | passwd --stdin "$username$i" &> /dev/null
27 done
28 fi
29 fi
30
31 [root@yinzhengjie backup]#
1 while [ 条件判断式 ] 2 do 3 源代码 4 done
1 [root@yinzhengjie shell]# more while.sh 2 #!/bin/bash 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie 5 #EMAIL:y1053419035@qq.com 6 7 sum=0 8 read -t 30 -p "Please enter the start number>>>" StartNumber 9 read -t 30 -p "Please enter an end number>>>" EndNumber 10 11 while [ $StartNumber -le $EndNumber ] 12 do 13 sum=$(( $sum + $StartNumber )) 14 StartNumber=$(( $StartNumber + 1 )) 15 done 16 17 StartNumber=$(( $StartNumber - $EndNumber )) 18 19 echo "从$StartNumber加到$EndNumber的总和是:$sum" 20 [root@yinzhengjie shell]#
1 until [ 条件表达式 ] 2 do 3 源代码 4 done
1 [root@yinzhengjie shell]# more until.sh 2 #!/bin/bash 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie 5 #EMAIL:y1053419035@qq.com 6 7 sum=0 8 read -t 30 -p "Please enter the start number>>>" StartNumber 9 read -t 30 -p "Please enter an end number>>>" EndNumber 10 11 until [ $StartNumber -gt $EndNumber ] 12 do 13 sum=$(( $sum + $StartNumber )) 14 StartNumber=$(( $StartNumber + 1 )) 15 done 16 17 StartNumber=$(( $StartNumber - $EndNumber )) 18 19 echo "从$StartNumber加到$EndNumber的总和是:$sum" 20 [root@yinzhengjie shell]#
标签:back stdin ack code backup use 表达 格式 sed
原文地址:http://www.cnblogs.com/yinzhengjie/p/7679640.html