Shell script loop statements and escape characters (for, while, until usage and examples)

One, escape character

echo -n 表示不换行输出

echo -e 输出转义字符,将转义后的内容输出到屏幕上

常用的转义字符如下:
\b 转义后相当于按退格键(backspace),但前提是"\b"后面存在字符;"\b"表示删除前一个字符,"\b\b"表示删除前两个字符。

\c 不换行输出,在"\c"后面不存在字符的情况下,作用相当于 echo -n; 但是当"\c"后面仍然存在字符时,"\c"后面的字符将不会被输出。

\n 换行,被输出的字符从"\n"处开始另起一行。

\f 换行,但是换行后的新行的开头位置连接着上一行的行尾;

\v 与\f相同;

\t 转以后表示插入tab,即横向制表符;

\r 光标移至行首,但不换行,相当于使用"\r"以后的字符覆盖"\r"之前同等长度的字符;但是当"\r"后面不存在任何字符时,"\r"前面的字符不会被覆盖

\\ 表示插入"\"本身;

Two, for loop statement

Insert picture description here

for 变量 in 取值列表
do
命令
done

Example: the sum of integers from 1 to 100

Insert picture description here
Insert picture description here

Example: Filter the odd and even numbers of all integers from 1 to 100

Insert picture description here
Insert picture description here

Example: Prompt the user to enter an integer less than 100, and calculate the sum of all integers from 1 to this number

Insert picture description here
Insert picture description here
④ Find the even and odd sums of all integers from 1 to 100
Insert picture description here
Insert picture description here

Example: Detect whether the hosts in the specified range are communicating, and output the communicating host ip to the file host_ip

Insert picture description here
Insert picture description here

Example: output all executable files in the /dev directory

Insert picture description here
Insert picture description here

Three, while loop statement

Insert picture description here

while 条件
do
命令
done

Example: Calculate the sum of all integers from 1 to 100

Insert picture description here
Insert picture description here

Example: Filter out the odd and even numbers of all integers from 1 to 100

Insert picture description here
Insert picture description here

Example: Prompt the user to enter an integer less than 100, and calculate the sum of all integers from 1 to this number

Insert picture description here
Insert picture description here

Example: Find the even and odd sums of all integers from 1 to 100

Insert picture description hereInsert picture description here

Example: Detect whether the hosts in the specified range are communicating, and output the communicating host ip to the file host_ip

Insert picture description here

Insert picture description here

Example: Add users in batches; User names start with stu and are numbered in numerical order;

A total of 20 users are added, that is, the
initial passwords of stu1, stu2,..., stu20 are all set to 123456
Insert picture description here

Example: Guess the commodity price game; obtain random numbers through the variable RANDOM; prompt the user to guess and record the number of times, and exit the loop after the guess is successful

Insert picture description here
Insert picture description here

Four, until loop statement

Insert picture description here

Execute the do statement until the condition is false

until 不成立的条件
	do
	done

Example: Calculate the sum of 1-50

Insert picture description here
Insert picture description here

Five, end the loop break and continue

for ((a=1; a<=5;a++))
do
       echo "外循环第$i次"
       if [  $a -eq 3 ];then
            break
        fi
        for ((b=1; b<=5;b++))
        do
        echo "内循环第$b次"
        if [$b -eq 3 ];then
            break
        fi
       done
done

Insert picture description here
Insert picture description here
break 2 break out of the 2-layer loop

continue: abort a command in a loop, but not completely abort the entire command

#!/bin/bash
for ((a=1; a<=15; a++))
do
	if [ $a -gt 5 ] && [ $a -lt 10 ]
	then
		continue
	fi
	echo "current value: $a"
done

Insert picture description here
Insert picture description here

Six, IFS field separator

默认包含 空格,制表符,换行符
查看命令:set | grep IFS
IFS=$' \t\n'

修改成只换行
IFS=$'\n'

IFS=:
IFS=','

IFS.OLD=$IFS
IFS=$'\n'
...
IFS=$IFS.OLD

输出环境变量PATH所包含的所有目录以及其中的所有可执行文件

Guess you like

Origin blog.csdn.net/IHBOS/article/details/114392898