shell脚本系列:while语句

格式

while condition
do
    statements
done

示例一

#!/bin/bash

i=1
sum=0

while ((i <= 100))
do
    ((sum += i))
    ((i++))
done

echo "The sum is: $sum"

示例二

#!/bin/bash

read m
read n
sum=0

while ((m <= n))
do
    ((sum += m))
    ((m++))
done

echo "The sum is: $sum"

示例三

#!/bin/bash

sum=0
echo "请输入您要计算的数字,按 Ctrl+D 组合键结束读取"

while read n
do
    ((sum += n))
done

echo "The sum is: $sum"

示例四

#!/bin/bash

i=5

while [ $i -ge 0 ]; do
    printf "
%s" "$i"
    # i=`expr $i - 1`
    ((i--))
    sleep 1
done

示例五

#!/bin/bash

while true; do
    echo -e "33[31m请输入MOS平台所使用的域名和端口(格式:mos.zhizhangyi.com:9070):33[0m"
    read -p "" Server_Domain
    echo -e "33[31m请输入RPM包安装方式:33[0m"
    read -p "" Rpm_Way
    echo -e "33[31m请确认您输入的域名和端口是否正确:33[0m 33[34m${Server_Domain}33[0m"
    echo -e "33[31m请确认您输入的RPM安装方式是否正确:33[0m 33[34m${Rpm_Way}33[0m"
    sleep 1
    echo -e "33[31m输入y/n:y确定,n返回(强制退出Ctrl+c):33[0m"
    read -p "" yn
    if [ ${yn} = 'y' ];then
        break
    else
        continue
    fi
done
原文地址:https://www.cnblogs.com/iuskye/p/shell-while.html