Article Directory
for loop statement
- The for statement needs to define a variable and a list of values, execute the same command according to different values, and know that the variable value is used up.
- The value list contains multiple objects with the same attributes, such as IP address, address book, etc.
#for语句结构
for 变量名 in 取值列表
do
命令序列
done
Example: Find the sum of 1~100.
vim he.sh
#!/bin/bash
sum=0
for i in {1..100}
do
sum=$[$i+$sum]
done
echo "0-100的和为:" $sum
Test Results:
Example: Prompt the user to enter an integer less than 100, and calculate the sum of all integers from 1 to this number
vim 3.sh
#!/bin/bash
read -p "请输入一个小于100的整数: " n
sum=0
for ((i=1;i<=n;i++))
do
sum=$[$i+$sum]
done
echo "从1到到该数之间所有整数的和为: " $sum
while loop statement
- A sequence of commands is repeatedly executed according to a specific condition until the condition is not met.
- There may be an infinite loop when writing, to avoid this from happening
- true (true) and false (false) are special condition test operations, and can also be used in the condition test of the if statement.
When true is used as a condition, it means that the condition is always established and executed infinitely, that is, an endless loop. When
false is required to be forced to terminate as a condition, it means that the condition is not established and the command sequence is not executed.
#while语句结构
while 条件测试操作
do
命令序列
done
Example: Calculate the sum of all integers from 1 to 100
vim w1.sh
#!/bin/bash
sum=0
shu=1
while [ $shu -le 100 ]
do
sum=$[$sum+$shu]
let shu++
done
echo "和为: " $sum
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
vim c.sh
price=$[$RANDOM % 1000]
a=0
num=0
echo "猜猜商品价格是多少"
while [ $a -eq 0 ]
do
let num++
read -p "请输入你猜的价格:" b
if [ $b -eq $price ];then
echo "恭喜,你猜对了!"
let a++
elif [ $b -gt $price ];then
echo "你猜大了!"
elif [ $b -lt $price ];then
echo "你猜小了!"
fi
done
echo "你总共猜了 $num 次。"
until loop statement
- Contrary to while, test a certain condition repeatedly, and execute it repeatedly as long as the condition is not established.
- Generally you can use while skillfully, this is rarely used.
Example: Calculate the sum of all integers from 1 to 100
End the loop----break and continue
break out of a single loop
vim break.sh
#!/bin/bash
for i in $(seq 1 10)
do
if [[ $i == 3 ]];then
echo "yes"
break
else
echo "no"
sleep 1
fi
done
The continue command will not jump out of all loops, just jump out of the current loop.
vim con.sh
#!/bin/bash
for i in $(seq 1 10)
do
if [[ $i == 3 ]];then
echo "yes"
continue
else
sleep 1
fi
echo $i
done
IFS field separator
默认包含 空格,制表符,换行符
查看命令:set | grep IFS
IFS=$' \t\n'
修改成只换行
IFS=$'\n'
IFS=:
IFS=','
IFS.OLD=$IFS
IFS=$'\n'
...
IFS=$IFS.OLD
输出环境变量PATH所包含的所有目录以及其中的所有可执行文件
Escape character
echo -n 表示不换行输出
echo -e 输出转义字符,将转义后的内容输出到屏幕上
The commonly used escape characters are as follows:
\b 转义后相当于按退格键(backspace),但前提是"\b"后面存在字符;"\b"表示删除前一个字符,"\b\b"表示删除前两个字符。
\c 不换行输出,在"\c"后面不存在字符的情况下,作用相当于 echo -n; 但是当"\c"后面仍然存在字符时,"\c"后面的字符将不会被输出。
\n 换行,被输出的字符从"\n"处开始另起一行。
\f 换行,但是换行后的新行的开头位置连接着上一行的行尾;
\v 与\f相同;
\t 转以后表示插入tab,即横向制表符;
\r 光标移至行首,但不换行,相当于使用"\r"以后的字符覆盖"\r"之前同等长度的字符;但是当"\r"后面不存在任何字符时,"\r"前面的字符不会被覆盖
\\ 表示插入"\"本身;
Example: