shell loop
One, for loop
(A) for loop
For loop, also called conditional loop, or for i in, is actually the characteristic of for loop. The number of times and given conditions are proportional to the
for statement structure.
Different variable values are read and used to execute the same set of commands one by one.
for 变量名 in 取值列表
do
命令序列
done
Traverse
for i in {
1..10} 迭代加1 | {
1..10..2} 迭代,每次循环加2
或 $(seq 1 10) | $(seq 1 2 10)
或for ((i=1; i<=10; i++)) | ((i=1; i<=10; i+=2))
do
echo $i
done
#/bin/bash
#1-10
for ((i=1; i<=10; i++)) 或$(seq 1 10)或者for i in {
1..10}
do
echo $i
done
Example 1 (Create users in batches)
1. Create a file containing the username
2. Write the script
#/bin/bash
#passwd
a=$(cat name.txt)
for i in a
do
useradd $i
echo "1234" | passwd --stdin $i
或echo -e "1234\n1234\n" | passwd $i // 或这使用\n代表回车键
done
3. Verification
Example 2 (testing host connectivity)
#!/bin/bash
#ping范围主机
for i in 172.16.100.{
1..20}
do
ping -c 3 -i 0.5 -W 2 $i &> /dev/null
if [ $? = 0 ]
then
echo "$i online"
else
echo "$i offline"
fi
done
There are multiple for example questions introduced in the previous articlelink
Two, while loop
(1) First look at the content of the sample questions
Guess the price game
方法一:不使用break循环
price=$[$RANDOM % 1000]
a=0
times=0
echo "猜猜商品价格是多少"
while [ $a -eq 0 ]
do
let times++
read -p "请输入你猜的价格:" b
if [ $b -eq $price ];then
echo "yes!"
let a++
elif [ $b -gt $price ];then
echo "你猜大了!"
elif [ $b -lt $price ];then
echo "你猜小了!"
fi
done
echo "你总共猜了 $times 次"
方法二:
#!/bin/bash
#猜价格
let a=$RANDOM%1000
b=0
d=0
while yes!
do
read -p "输入价格" c
if [ $c = $a ]
then
echo "yes"
break
elif [ $c -lt $a ]
then
echo "太低了"
else
echo "太高了"
fi
let d++
done
echo "你输入多少次 $d"
(Two) while loop
As long as the condition is established, the loop will be repeated, and it will stop immediately if it fails.
1, while statement structure
while 条件测试操作
do
命令序列
done
#/bin/bash #/bin/bash
#example #example
i=0 注 i=0
while (( $i <= 10 )) 意 while [ $i -eq 10 ]
do 区 do
echo "$i" 别 let i++
let i++ echo "$i"
done done
The first script outputs the contents of 0-10, the second script outputs the contents of 1-11
(Note the location of the iteration)
(3) Example 2
Add 5 users starting with stu in batch; stu1—stu5, initial password 1234
#/bin/bash
#useradd
i=0
while [ $i -le 4 ]
do
let i++
useradd stu$i
echo "1234" | passwd --stdin stu$i
done
Three, until (anti-while loop)
(1) until
Repeatedly test a certain condition, as long as the condition is not established, repeat the loop
1, until statement structure
until 条件测试操作
do
命令序列
done
Display an integer from 1 to 10
i=1
until [ $i -gt 10 ] //i的值大于等10 不满足i>10的情况下执行循环
do
echo "$i"
let i++
done
(2) Example 1
Calculate the sum of integers from 1 to 50
#/bin/bash
#sum 1-50
i=1
sum=0
until [ $i -gt 50 ]
do
sum=$(($sum+$i))
let i++
done
echo "$sum"
Four, two kinds of interruption (break and continue)
break jumps out of a single loop; continue aborts a command in a loop, but does not completely abort the entire command
for i in {
1..5}
do
echo "外层循环 $i"
for b in {
1..5}
do
if [ $b -eq 3 ]
then
break //break2则也停止外层循环
fi
echo "内层循环 $b"
done
done
continue
for i in {
1..5}
do
echo "外层循环 $i"
for b in {
1..5}
do
if [ $b -eq 3 ]
then
continue
fi
echo "内层循环 $b"
done
done
Five, common conversion to characters
cho -n means output without line
breaks. echo -e output escape characters and output the escaped content to the screen.
Commonly used escape characters are as follows:
- After \b is escaped, it is equivalent to pressing the backspace key (backspace), but the premise is that there are characters after "\b"; "\b" means to delete the previous character, and "\b\b" means to delete the first two characters
- \c does not wrap output. When there are no characters after "\c", it is equivalent to echo -n; but when there are still characters after "\c", the characters after "\c" will not be output
- \n Newline, the characters to be output start on a new line from "\n"
- \f Newline, but the beginning of the new line after the newline is connected to the end of the previous line
- \v is the same as \f
- \t After the turn, it means to insert a tab, that is, a horizontal tab
- \r The cursor moves to the beginning of the line, but does not wrap, which is equivalent to using the characters after "\r" to overwrite the characters of the same length before "\r"; but when there is no character after "\r", "\r" The previous character will not be overwritten
- \ Means to insert "" itself;
Six, IFS field separator
Contains spaces, tabs, and newlines by default set | grep IFS
1.修改
IFS=$'\t\n'
修改成只换行
IFS=$'\n'
IFS=':'
IFS=','
2.备份
IFS. OLD=$IFS
IFS=$'\n'
...
IFS=$IFS.OLD
(1) Sample questions
Output all directories contained in the environment variable PATH and all executable files in them
for
#/bin/bash
OLDIFS=$IFS ///备份IFS参数
IFS=':' ///以:分割的字段输出,这样i就可以按字段取值
for i in $PATH
do
for a in $i/*
do
if [ -x $a -a -f $a ];then
echo "$a 文件有执行权限"
fi
done
done
IFS=$OLDIFS ///还原IFS默认值