shelll script for while until loop

Process Control

Logic programming:

顺序执行
选择执行
循环执行

cycle

循环执行
    将某代码段重复运行多次
    重复运行多少次
        循环次数事先已知
        循环次数事先未知
    有进入条件和退出条件
for, while, until

for loop

for 变量名 in 列表;do
    循环体
 done
执行机制:
依次将列表中的元素赋值给“变量名”; 每次赋值后即执行一次循环体; 直到列表中的元素耗尽,循环结束

for loop

列表生成方式:
(1) 直接给出列表
(2) 整数列表:
    (a) {start..end}
    (b) $(seq [start [step]] end) 
(3) 返回列表的命令
    $(COMMAND)
(4) 使用glob,如:*.sh
(5) 变量引用
    $@, $*

for special format

双小括号方法,即((…))格式,也可以用于算术运算
双小括号方法也可以使bash Shell实现C语言风格的变量操作
    I=10
    ((I++)) 
for循环的特殊格式:
 for ((控制变量初始化;条件判断表达式;控制变量的修正表达式))
    do
    循环体
 done
控制变量初始化:仅在运行到循环代码段时执行一次
控制变量的修正表达式:每轮循环结束会先进行控制变量修正运算,而后再做条件判断

Exercise: implemented for

1, the determination / var / directory of all files of type

read -p "input catalogue (eg:/var/):" DIRNAME
for FILE in `ls -l ${DIRNAME} | tr -s ' '|cut -d' ' -f9`
        do
        NAME=${DIRNAME}$FILE
        [ -b $NAME ] && echo "${NAME} The is blockfile"
        [ -c $NAME ] && echo "${NAME} The is devicefile"
        [ -d $NAME ] && echo "${NAME} The is catalogue file"
        [ -f $NAME ] && echo "${NAME} The is flat file"
        [ -L $NAME ] && echo "${NAME} The is link file"
        [ -p $NAME ] && echo "${NAME} The is pipe file"
        [ -S $NAME ] && echo "${NAME} The is paper file"
done

 2, adding 10 users user1-user10, 8-bit random password character

for NU in `seq 10`
 do
 useradd user$NU
 PASS=`cat /dev/urandom | tr -dc '0-9'|head -c8`
 echo user$NU:$PASS >> /data/userpasswd.txt
 echo $PASS | passwd --stdin user$NU &> /dev/null
 passwd -e user$NU
 echo "user$NU is created"
done   

3, scripting, prompts positive integer value of n, the sum of the calculated n 1 + ... + 2 +

 #!/bin/bash
 sum=0
read -p "pleass input positive integer:" N
for (( i=1;i<=$N;i++ ));do                                       
        let sum+=i
done
echo $sum

4, all within 100 calculates an integer divisible by 3 and the sum

#!/bin/bash
#
#********************************************************************
#Author: wangyakun
#Date: 2019-08-22
#FileName: for_3number.sh
#URL: http://www.magedu.com
#Description: The test script
#Copyright (C): 2019 All rights reserved
#********************************************************************
sum=0
for (( i=3;i<=100;i+=3 ));do
        let sum+=i              
done
echo $sum

5, writing scripts, prompts Enter the network address, such as network 192.168.0.0, enter judgment in the host state line

 #!/bin/bash
read -p " pleass input Network segment (eg:192.168.7.0):" IPS
NET=`echo $IPS | sed -nr 's/(.*)\..*\.[^.]$/\1/p'`
for i in {1..255};do
        {
        for j in {1..255};do
                {
                ping -c1 -w1 ${NET}.$i.$j &> /dev/null && echo "${NET}.$i.$j is up"||echo "${NET}.$i.$j is down"
                }&
        done
        }&
        wait
done
wait
echo "scan hostis finished"  

while loop

while CONDITION; do
循环体
 done
CONDITION:循环控制条件;进入循环之前,先做一次判断;每一次循环之后
会再次做判断;条件为“true”,则执行一次循环;直到条件测试状态为“false”
终止循环
因此:CONDTION一般应该有循环控制变量;而此变量的值会在循环体不断地被
修正
进入条件:CONDITION为true
退出条件:CONDITION为false

until loop

until CONDITION; do
循环体
done
进入条件: CONDITION 为false
退出条件: CONDITION 为true

Loop control statements continue

用于循环体中
continue [N]:提前结束第N层的本轮循环,而直接进入下一轮判断;最内层为
第1层
 while CONDTIITON1; do
CMD1
···
if CONDITION2; then
continue
fi
CMDn
...
 done

Loop control statements break

用于循环体中
break [N]:提前结束第N层循环,最内层为第1层
 while CONDTIITON1; do
CMD1
...
if CONDITION2; then
break
fi
CMDn
...
 done

Loop control shift command

shift [n]
用于将参量列表 list 左移指定次数,缺省为左移一次。
参量列表 list 一旦被移动,最左端的那个参数就从列表中删除。while 循环遍
历位置参量列表时,常用到 shift
./doit.sh a b c d e f g h
./shfit.sh a b c d e f g h
示例:doit.sh
#!/bin/bash
# Name: doit.sh
# Purpose: shift through command line arguments
# Usage: doit.sh [args]
while [ $# -gt 0 ] # or (( $# > 0 ))
do
 echo $*
 shift
done
示例:shift.sh
#!/bin/bash
#step through all the positional parameters
until [ -z "$1" ] 
do
 echo "$1"
 shift
done
echo 

Create an infinite loop

while true; do
循环体
done
until false; do
循环体
Done

Guess you like

Origin blog.51cto.com/13626938/2432539