格式:
格式一、while COMMANDS; do COMMANDS; done
格式二、while CONDITION; do
循环体
done
无限循环:
while true; do
循环体
done
for写发:
for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done
相当于while的:
(( EXP1 ))
while (( EXP2 )); do
COMMANDS
(( EXP3 ))
done
范例:硬盘
#!/bin/bash
while : ; do
disk=df -h | sed -rn '/^\/dev\/sd/s/.* ([0-9]+).*/\1/p' | sort -nr | head -n1
if [ $disk -gt 80 ] ;then
echo "warning disk hostname -I " | mail -s "disk" [email protected]
fi
sleep 10
done
范例:三角形
#!/bin/bash
read -p "请输入行数:" line
i=1
while ((i<=line)) ;do
k=1
while ((k<=line-i));do
echo -e ' \c'
((k++))
done
j=1
while ((j<=2*i-1)) ;do
echo -e '*\c'
((j++))
done
((i++))
echo
done
格式:until(一般情况下使用while)
until COMMANDS; do COMMANDS; done
until CONDITION; do
循环体
done
说明:
进入条件: CONDITION 为false
退出条件: CONDITION 为true
无限循环:
until false; do
循环体
Done