#!/bin/sh echo"Please input a Number:" read Num if [ "$Num" -lt 15 ] then echo"$Num <= 15" fi
IF/IF-ELSE结构: 一个双分支结构,如下执行删除文件,成功返回then是否返回else.
#!/bin/sh echo"Please input the file which you want to delete:" read file ifrm -f "$file" then echo"Delete the file $file sucessfully!" else echo"Delete the file $file failed!" fi
#!/bin/bash #提示用户输入分数(0-100) 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
#!/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)." elif [ "$score" -ge 90 ] then echo"The grade is A!" elif [ "$score" -ge 80 ] then echo"The grade is B!" elif [ "$score" -ge 70 ] then echo"The grade is C!" elif [ "$score" -ge 60 ] then echo"The grade is D!" else echo"The grade is E!" fi
实例1: 通过单分支判断磁盘是否已满.
[root@localhost ~]# cat if.sh
#!/bin/bash
ret=$( df -h | grep "/dev/sda1" | awk '{printf $5}' | cut -d "%" -f 1 ) if [ $ret -ge 80 ] then echo -e "/dev/sda1 is full !!" fi
实例2: 通过多分支判断磁盘是否已满.
[root@localhost ~]# cat if.sh #!/bin/bash
ret=$( df -h | grep "/dev/sda1" | awk '{printf $5}' | cut -d "%" -f 1 ) if [ $ret -ge 80 ] then echo -e "/dev/sda1 is full !" else echo -e "/dev/sda1 is not full !" fi
实例3: 多分支判断小例子.
[root@localhost ~]# cat if.sh #!/bin/bash
read -p "输入一个数字:" num
if [ "$num" -gt "100" ] then echo -e "这个数大于100" elif [ "$num" -lt "100" ] then echo -e "这个数小于100" elif [ "$num" -eq "100" ] then echo -e "这个数等于100" fi
#!/bin/bash sum=0 i=1 while(( i <= 100 )) do let"sum+=i" let"i += 2" done echo"sum=$sum"
结束标记控制的while循环: 设置一个特殊的数据值(结束标记)来结束while循环.
#!/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"To high. Try again" read num else exit 0 fi done echo"Congratulation, you are right! "
标志控制的while循环: 使用用户输入的标志值来控制循环的结束(避免不知道循环结束标志的条件).
#!/bin/bash echo"Please input the num " read num sum=0 i=1 signal=0 while [[ "$signal" -ne 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
#!/bin/bash m=1 for (( i=1; i < 100; i++ )) do let"temp1=i%7"#被7整除 if [ "$temp1" -ne 0 ] then continue fi echo -n "$i " let"temp2=m%7"#7个数字换一行 if [ "$temp2" -eq 0 ] then echo"" fi let"m++" done
select var in"Linux""GNU HURD""FreeBSD""Other"; do if [ $var == "Linux" ] then echo -e "您的系统是Linux" elif [ $var == "FreeBSD" ] then echo -e "您的系统是FreeBSD" fi break done [root@localhost ~]# bash select.sh 请选择系统类型? 1) Linux 2) GNU HURD 3) FreeBSD 4) Other
#? 1 您的系统是Linux
#!/bin/bash
declare -a serial serial=(1 2 3 4) PS3="Enter a number: "
select var in"a""b""c""d" do if ! echo${serial[@]} | grep -q $REPLY then echo"please enter [1-4]." continue fi
echo"your anwser is: $var" break done
[root@localhost ~]# bash select.sh 1) a 2) b 3) c 4) d Enter a number: 1 your anwser is: a
[root@localhost ~]# declare -a array_name=(Jerry Alice David LyShark) #声明测试数组 [root@localhost ~]# echo ${array_name[*]} #打印全部数组元素 Jerry Alice David LyShark
#!/bin/sh echo"Please input a Number:" read Num if [ "$Num" -lt 15 ] then echo"$Num <= 15" fi
IF/IF-ELSE结构: 一个双分支结构,如下执行删除文件,成功返回then是否返回else.
#!/bin/sh echo"Please input the file which you want to delete:" read file ifrm -f "$file" then echo"Delete the file $file sucessfully!" else echo"Delete the file $file failed!" fi
#!/bin/bash #提示用户输入分数(0-100) 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
#!/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)." elif [ "$score" -ge 90 ] then echo"The grade is A!" elif [ "$score" -ge 80 ] then echo"The grade is B!" elif [ "$score" -ge 70 ] then echo"The grade is C!" elif [ "$score" -ge 60 ] then echo"The grade is D!" else echo"The grade is E!" fi
select var in"Linux""GNU HURD""FreeBSD""Other"; do if [ $var == "Linux" ] then echo -e "您的系统是Linux" elif [ $var == "FreeBSD" ] then echo -e "您的系统是FreeBSD" fi break done [root@localhost ~]# bash select.sh 请选择系统类型? 1) Linux 2) GNU HURD 3) FreeBSD 4) Other
#? 1 您的系统是Linux
#!/bin/bash
declare -a serial serial=(1 2 3 4) PS3="Enter a number: "
select var in"a""b""c""d" do if ! echo${serial[@]} | grep -q $REPLY then echo"please enter [1-4]." continue fi
echo"your anwser is: $var" break done
[root@localhost ~]# bash select.sh 1) a 2) b 3) c 4) d Enter a number: 1 your anwser is: a
#!/bin/bash sum=0 i=1 while(( i <= 100 )) do let"sum+=i" let"i += 2" done echo"sum=$sum"
结束标记控制的while循环: 设置一个特殊的数据值(结束标记)来结束while循环.
#!/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"To high. Try again" read num else exit 0 fi done echo"Congratulation, you are right! "
标志控制的while循环: 使用用户输入的标志值来控制循环的结束(避免不知道循环结束标志的条件).
#!/bin/bash echo"Please input the num " read num sum=0 i=1 signal=0 while [[ "$signal" -ne 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
#!/bin/bash m=1 for (( i=1; i < 100; i++ )) do let"temp1=i%7"#被7整除 if [ "$temp1" -ne 0 ] then continue fi echo -n "$i " let"temp2=m%7"#7个数字换一行 if [ "$temp2" -eq 0 ] then echo"" fi let"m++" done