古越剑箫

学习是一种习惯

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

 

 

#!/bin/bash
#shell 流程控制语句

echo "====================if 语句===================="

if test $(ps -ef | grep -c "ssh") -gt 1 
then
    echo "true";
else
    echo "false";
fi;


if [ $(ps -ef | grep -c "ssh") -gt 1 ]
then 
    echo "true";
else
    echo "false";
fi

a=10
b=20
if [ $a == $b ];then
    echo "a == b"
elif [[ $a -gt $b ]]; then
    echo "a > b";
elif [ $a -lt $b ];then
    echo "a < b";
else
    echo "没有符合的条件"        
fi

echo "====================for 语句===================="

for (( i = 0; i < 10; i++ )); do
    echo "${i}";
done


for i in a b c d e; do
    echo "${i}";
done

echo "====================while 语句===================="

int=1

while [[ "${int}" -le 5 ]]; do
    echo "${int}";
    # let int+=1;
    # let "int++";
    # let int=int+1;
    int=`expr $int + 1`;


done

#三种无限循环方式
echo "无限循环1"
# while :
# do
#     echo "无限循环1";
# done;

echo "无限循环2"
# while true
# do
#     echo "无限循环2";
# done;

echo "无限循环3"
# for ((; ;));do
#     echo "无限循环3";
# done;


echo "until 循环####################################" 
# until 循环执行一系列命令直至条件为 true 时停止。
# until 循环与 while 循环在处理方式上刚好相反。
# 一般 while 循环优于 until 循环,但在某些时候—也只是极少数情况下,until 循环更加有用。
# 语法格式
a=0
until [[ $a -gt 10 ]]; do
    echo "${a}";
    a=`expr $a + 1`;
done


echo "case 语句##################################"

read -p "请输入1~4之间的数字" num;
echo "您输入数字:${num}";
case $num in
    1 ) echo "您输入了 ${num}"
        ;;
    2 ) echo "您输入了 ${num}"
        ;;
    3 ) echo "您输入了 ${num}"
        ;;
    4 ) echo "您输入了 ${num}"
        ;;
    * ) echo "您输入的数字不在1~4之间"
        ;;                
esac

echo "演示无线循环,break,终止循环##################################"
while :
do
    read -p "请输入1~4之间的数字" p;
    echo "echo您输入的是:${p}";
    case "${p}" in
        1|2|3|4 ) echo "您输入的是${p}"
        ;;
        *) echo "您输入的是${p},不在1~4之间,break终止循环"
            break;
        ;;
    esac
done

echo "演示无线循环,continue跳出当前循环##################################"


while :
do
    read -p "请输入1~4之间的数字" p;
    echo "echo您输入的是:${p}";
    case "${p}" in
        1|2|3|4 ) echo "您输入的是${p}"
        ;;
        *) echo "您输入的是${p},不在1~4之间,continue跳出循环"
            continue;
            echo "game over!"
        ;;
    esac
done
posted on 2020-08-26 15:44  古越剑箫  阅读(186)  评论(0编辑  收藏  举报