irpas技术客

shell中的条件控制语句_Robert Zhao_shell编程中可实现条件控制的命令是

大大的周 3965

shell中的条件控制语句 1、if语句 (1)、简单if结构

格式: if expression then command … fi

if expression;then command … fi

#!/bin/bash echo "Please input a string:" read str1 if [ -z "$str1" ];then echo "What you input is null!" exit 1 fi (2)、if/else结构

格式: if expression;then command … else command … fi

#!/bin/bash echo "Please Input a integer(0-100): " read score if [ "$score" -lt 0 -o "$score" -gt 100 ];then echo "The score what you input is not integer or the score is not in (0-100)." else if [ "$score" -ge 90 ];then echo "The grade is A!" else if [ "$score" -ge 80 ];then echo "The grade is B!" else if [ "$score" -ge 70 ];then echo "The grade is C!" else if [ "$score" -ge 60 ];then echo "The grade is D!" else echo "The grade is E!" fi fi fi fi fi (3)、if/elif/else结构

格式: if expression1;then command … elif expression2;then command … elif expressionN;then command … else command … fi

#!/bin/bash echo "Please input a year: " read year let "n1=$year % 4" let "n2=$year % 100" let "n3=$year % 400" if [ ! "$n1" -eq 0 ];then leap=0 elif [ ! "$n2" -eq 0 ];then leap=1 elif [ ! "$n3" -eq 0 ];then leap=0 else leap=1 fi if [ "$leap" -eq 1 ];then echo "$year is a leap year!" else echo "$year is not a leap year!" fi 2、case语句

格式: case variable in value1) command; value2) command; … valueN) command; *) command; esac

#!/bin/bash echo "Please input a month(1-12): " read month case "$month" in 1) echo "The month is January!";; 2) echo "The month is February!";; 3) echo "The month is March!";; 4) echo "The month is April!";; 5) echo "The month is May!";; 6) echo "The month is June!";; 7) echo "The month is July!";; 8) echo "The month is August!";; 9) echo "The month is September!";; 10) echo "The month is October!";; 11) echo "The month is November!";; 12) echo "The month is December!";; *) echo "The month is not in (1-12).";; esac 3、for语句 (1)、列表for循环

格式: for variable in {list} do command done

#!/bin/bash # {1..5}表示1~5 for variable1 in {1..5} do echo "Hello,welcome $variable1 times" done #!/bin/bash sum=0 # {1..100..2}表示1~100且步长为2 for variable2 in {1..100..2} do let "sum+=i" done echo "sum=$sum" #!/bin/bash sum=0 # (seq 1 2 100)表示1~100且步长为2 seq是Linux预设的外部命令 for i in (seq 1 2 100) do let "sum+=i" done echo "sum=$sum" #!/bin/bash for i in $(ls) do echo "file: $i" done (2)、不带列表的for循环

格式: for variable do command done

#!/bin/bash # $#表示参数的个数 $*或$@代表传递的所有参数 echo "number of arguments is $#" echo "what you input is: " for argument in "$*" do echo "$argument" done (3)、类C风格的for循环

格式: for((init;condition;setp)) do command done

#!/bin/bash for ((i=1;i<=5;i++)) do echo "$i" done #!/bin/bash LIMIT=5 for ((a=1,b=5;a<=LIMIT;a++,b--)) do let "temp=a-b" echo "$a-$b=$temp" done 4、while语句

通用格式: while expression do command done

(1)、计数器控制的while循环

格式: counter=1 while expression do command let command to operate counter done

#!/bin/bash int=1 while(("$int"<=5)) do echo "$int" let "int++" done (2)、结束标记控制的while循环

格式: read variable while [[ “$variable” !=sentinel ]] do read variable done

#!/bin/bash echo "Please input the num(1-10) " read num while [[ "$num" != 4 ]] do if [ "$num" -lt 4 ];then echo "Too small. Try again!" read num elif [ "$num" -gt 4 ];then echo "Too high. Try again" read num else exit 0 fi done echo "Congratulation, you are right! " (3)、标志控制的while循环

格式: signal=0 while((signal !=1)) 或 while[[ signal !=1 ]] do if expression;then signal=1 fi done

#!/bin/bash echo "Please input the num " read num sum=0 i=1 signal=0 while [[ "$signal" != 1 ]] do if [ "$i" -eq "$num" ];then let "signal=1" let "sum+=i" echo "1+2+...+$num=$sum" else let "sum=sum+i" let "i++" fi done (4)、命令行控制的while循环

格式: while [[ “$*” !="" ]] do echo “$1” shift done

#!/bin/bash echo "number of arguments is $#" echo "What you input is: " while [[ "$#" -ne 0 ]] do echo "$1" # shift命令使位置变量下移一位(即$2代替$1、$3代替$2),并且使$#变量递减 shift done 5、until语句

格式: until expression do command done

#!/bin/bash i=1 until [[ "$i" -gt 5 ]] do let "square=i*i" echo "$i*$i = $square" let "i++" done 6、break/continue语句

break说明: break 语句可以应用在for、while和until循环语句中,用于强行退出循环,也就是忽略循环体中任何其他语句和循环条件的限制

#!/bin/bash #使用break 循环跳出i=7的那一行 for ((i=1;i<=9;i++)) do #内层循环执行break循环控制符 for ((j=1; j<=i;j++)) do #将i*j的临时结果保存在temp中 let "temp=i*j" #break 语句跳出内层循环 if [ "$temp" -eq 7 ];then break fi echo -n "$i*$j=$temp " done echo "" done

continue说明: continue循环控制符应用在for、while和until语句中,用于让脚本跳过其后面的语句,执行下一次循环

#!/bin/bash for ((i=1;i<=9;i++)) do #内层循环执行continue循环控制符 for ((j=1; j<=i;j++)) do #将i*j的临时结果保存在temp中 let "temp=i*j" #continue 语句跳出一次内层循环,继续下一次内层循环 if [ "$temp" -eq 7 ];then continue fi echo -n "$i*$j=$temp " done echo "" done 7、select结构

格式: select variable in {list} do command … break done

#!/bin/bash echo "What is your favorite color?" select color in "red" "blue" "green" "white" "black" do break done echo "You have selected $color"


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #quotPlease #input #a #stringquotread