shell循环结构

for循环:

#! /bin/bash
#定义数组
array=(Monday Tuesday Wednesday Thursday Friday Saturday Sunday)
for day in ${array[*]}
do
    echo "$day"
done

九九乘法表:

#! /bin/bash

for ((i=1;i<=9;i++)) do for ((j=1;j<=i;j++)) do let "product=i*j" printf "$i*$j=$product" if [ "$product" -gt 9 ]; then printf " " else printf " " fi done echo done

until循环:

#! /bin/bash

i=1
until [ "$i" -gt 9 ]
do
    let "square=i*i"
    echo "$i*$i=$square"
    let "i=i+1"
done

while循环

#! /bin/bash 

echo "please enter a number between 1 and 10 ,Enter 0 to exit."
read var
while [ "$var" !=  0 ]
do
    if [ "$var" -lt 5 ]; then
        echo "To small,Try again"
        read var
    elif [ "$var" -gt 5 ]; then
        echo "Too big, Try again"
        read var
    else
        echo "Congratulation!! you are right"
        exit 0;
    fi
done



你可能感兴趣的:(shell)