1, for loop
The for loop format:
for the variable in the list do Command1 Command2 ... Commandn DONE
Note:
1, such as an alternative command list can ls` `
2, braces {1..6}, two intermediate or middle # 123 is a space, not a comma like
3, for ((i = 11 ; i <= 15; i ++)) # double quotes, no spaces, no description int
2, while circulation
Command the while do the Statement (S) BE to IS Command Executed to true IF DONE
Note:
1, I = $ ((I + 1)) # expressions, double brackets for $
2, let I ++ # 1 may increase since let
3, if elif else fi statement
Shell has three if ... else statement:
- if ... fi statement;
- if ... else ... fi statement;
- if ... elif ... else ... fi statement.
-
if ... elif ... fi statement can be determined for a plurality of conditions, the syntax is:
if [ expression 1 ] then Statement(s) to be executed if expression 1 is true elif [ expression 2 ] then Statement(s) to be executed if expression 2 is true elif [ expression 3 ] then Statement(s) to be executed if expression 3 is true else Statement(s) to be executed if no expression is true fi
note:
1, read -p "Enter your user name:" userName # save to input variables userName
4, case statements
case statement format is as follows:
case 值 in 模式1) command1 command2 command3 ;; 模式2) command1 command2 command3 ;; *) command1 command2 command3 ;; esac
注意:
1、模式1) 半边括号
2、;;模式判断介绍符
3、*)表示其他模式
4、case语句结束标记为反写esac
5、for while语句实例与运行结果
#!/bin/bash echo "for ========" for i in 1 2 3 #中间是空格,不是逗号之类的 do echo "i:$i" done for i in {5..10} #花括号,中间两点 do echo "i:$i" done for((i=11;i<=15;i++)) #双括号,没空格,没int说明 do echo "i:$i" done echo “while ======” i=1 total=0 while((i<=100)) do total=$((total+i)) i=$((i+1)) #表达式,得双括号加$ done echo "total:$total" i=1 total=0 while((i<=100)) do total=$((total+i)) let i++ #自加1可以用let done echo "total:$total"
[app@VM_4_53_centos cfltest]$ ./forwhile.sh for ======== i:1 i:2 i:3 i:5 i:6 i:7 i:8 i:9 i:10 i:11 i:12 i:13 i:14 i:15 “while ======” total:5050 total:5050
6、if elif else fi语句实例与运行结果
#!/bin/bash echo "if语句=======" read -p "请输入用户名:" userName #保存输入的变量到userName if [ $userName = root ] then echo "welcome root..." elif [ $userName = app ] #elif的写法注意啦 then echo "welcome app ..." else echo "please go out..." fi
[app@VM_4_53_centos cfltest]$ ./contorlshell.sh if语句======= 请输入用户名:root welcome root... [app@VM_4_53_centos cfltest]$ ./contorlshell.sh if语句======= 请输入用户名:app welcome app ... [app@VM_4_53_centos cfltest]$ ./contorlshell.sh if语句======= 请输入用户名:you please go out...
7、case语句实例与运行结果
#!/bin/bash echo "case语句==========" case $1 in #case in start) #半边括号 echo "starting ..." ;; stop) echo "stopping ..." ;; restart) echo "restart ..." ;; *) #其他情况用 * 表示 echo "argument error ..." ;; #每个分支结束加两次分号 esac #case 反过来写
[app@VM_4_53_centos cfltest]$ ./caseesac.sh case语句========== argument error ... [app@VM_4_53_centos cfltest]$ ./caseesac.sh start case语句========== starting ... [app@VM_4_53_centos cfltest]$ ./caseesac.sh restart case语句========== restart ... [app@VM_4_53_centos cfltest]$ ./caseesac.sh stop case语句========== stopping ...