5-3-shell:流程控制--判断循环

一、判断

语法 

if condition1
then 
    command1
elif condition1
then
    command2
else
    command3
fi

合并成一行

if [ condition1 ]; then command1; elif [ condition1 ]; then command2; else command3; fi

demo

value=3;if [ $value == 1 ]; then  echo "if"; elif [ $value == 2 ]; then echo "elif"; else echo "else"; fi

二、循环

for 循环

数值遍历
#!/bin/bash 
for((i=1; i<=10; i++)); 
do 
    echo $i; 
done
矩阵index遍历 
#!/bin/bash 
for i in {1..10} 
do 
    echo $i
done
awk 'BEGIN{for(i=1; i<=10; i++) print i}'
列表遍历
#!/bin/bash  
list="aa bb cc dd"  
for i in $list;  
do  
    echo $i is appoint ;  
done  
 命令结果遍历
#!/bin/bash  
for i in `ls`;  
do   
    echo $i is file name\! ;  
done 
#!/bin/bash  
for file in $(ls *.sh)  
do  
echo $file is file path \! ;  
done 

 while 循环

判断为true时,开始执行

#!/bin/bash
value=1
while(( $value<=5 ))
do
    echo $value
    let "value++"
done

until循环

直接执行,判断为true是停止

#!/bin/bash
value=10;
until (( $value<=5 ))
do     
    echo $value;     
    let "value--" 
done

无限循环

for((;;)); do echo "do for."; sleep 5; done
while : ; do echo "do while."; sleep 5; done
while true ; do echo "do while."; sleep 5; done
until false; do echo "do until."; sleep 5; done

循环跳出/继续

break
continue

三、多选择语句

case ... esac

value=1
case $value in
    1) echo "1";;
    2) echo "2";;
esac

 

2021-07-13

posted @ 2021-07-13 18:51  繁忙的蜜蜂  阅读(19)  评论(0编辑  收藏  举报