Shell script for While Until loop

Flow control

Logic processing in programming:

Sequential execution
Select execution
Circular execution

Loop

 Loop execution
Repeat a certain code segment several times
How many times it repeats
The number of loops is known in advance
The number of loops is unknown in advance
There are entry and exit conditions
br />?for, while, until

for loop

for variable name in list;do
loop body
done
execution mechanism:
Assign the elements in the list to the "variable name" in turn; after each assignment, the loop body will be executed once; until the elements in the list are exhausted, the loop ends

for loop

List generation method:
(1) Give the list directly
(2) List of integers:
(a) {start..end}
(b) $( seq [start [step]] end)
(3) Command to return the list
$(COMMAND)
(4) Use glob, such as: *.sh
(5) Variable reference
[email protected], $*

for special format

The double parenthesis method, namely ((...)) format, can also be used for arithmetic operations< br />The double parenthesis method can also make bash Shell realize C language style variable operation
I=10
((I++))
Special format of for loop:
for ((Control variable initialization; conditional judgment expression; modified expression of control variable))
do
loop body
done
control variable initialization: only run to the loop code segment Execute once at the time
The modification expression of the control variable: At the end of each cycle, the control variable modification operation will be performed first, and then the condition judgment will be performed.

Exercise: Use for to achieve

1. Determine the type of all files in the /var/ directory

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 , Add 10 users user1-user10, the password is 8 random characters

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. Write a script and prompt for input The value of integer n, calculate the sum of 1+2+…+n

#!/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. Calculate the sum of all integers within 100 that are divisible by 3

#!/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. Write a script and prompt to enter the network address, such as 192.168.0.0, determine the online status of the host in the input network segment

#!/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
loop body
done
? CONDITION: loop control condition; before entering the loop, first Make a judgment; after each loop
will make a judgment again; the condition is "true", the loop will be executed once; until the condition test status is "false"
terminate the loop
? Therefore: CONDTION Generally, there should be a loop control variable; and the value of this variable will be continuously updated in the loop body

? Entry condition: CONDITION is true
? Exit condition: CONDITION is false

until loop

?until CONDITION; do
loop body
?done
? Entry condition: CONDITION is false
? Exit condition: CONDITION is true

Loop control statement continue

? Used in the loop body
?continue [N]: End the current loop of the Nth layer early, and go directly to the next Round judgment; the innermost layer is the first layer
while CONDTIITON1; do
CMD1
···
if CONDITION2; then
continue
fi
CMDn
...
done

Loop control statement break

? Used in the loop body
?break [ N]: End the Nth layer loop early, the innermost layer is the first layer
while CONDTIITON1; do
CMD1
...
if CONDITION2; then
break
fi
CMDn
...
done

Cycle control shift command

shift [n]
? is used to shift the list of parameters to the left a specified number of times, the default is to shift left once.
? Once the parameter list is moved, the leftmost parameter is deleted from the list. While looping
When traversing the position parameter list, shift
?./doit.sh abcdefgh
?./shfit.sh abcdefgh
Example: 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
Example: 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
Loop body
?done
?until false; do
Loop body
?Done

Leave a Comment

Your email address will not be published.