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 "\r%s" "$i"
    # i=`expr $i - 1`
    ((i--))
    sleep 1
done

示例五

#!/bin/bash

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

shell脚本系列:while语句

上一篇:克里斯坦森的破坏性创新—《可以量化的管理学》


下一篇:linux命令-sort