echo"The # here does not begin a comment." echo'The # here does not begin a comment.' echo The \# here does not begin a comment. echo The # 这里开始一个注释 echo $(( 2#101011 )) # 数制转换(使用二进制表示),不是一个注释,双括号表示对于数字的处理
# 欢迎来到实验楼参观学习 #########################
2. 分号(;)
1. 命令分隔符
使用分号(;)可以在同一行上写两个或两个以上的命令。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
$vim test2.sh
# test2.sh # #!/bin/bash echo hello; echo there filename=ttt.sh if [ -e "$filename" ]; then# 注意: "if"和"then"需要分隔,-e用于判断文件是否存在 echo"File $filename exists."; cp$filename$filename.bak else echo"File $filename not found."; touch$filename fi; echo"File test complete."
if condition then command1 command2 ... commandN fi
2.if else
if else 语法格式:
1 2 3 4 5 6 7 8 9
if condition then command1 command2 ... commandN else command fi
if-elif-else 语法格式:
1 2 3 4 5 6 7 8 9
if condition1 then command1 elif condition2 then command2 else commandN fi
以下实例判断两个变量是否相等:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
a=10 b=20 if [ $a == $b ] then echo"a == b" elif [ $a -gt $b ] then echo"a > b" elif [ $a -lt $b ] then echo"a < b" else echo"Ineligible" fi
输出结果:
1
a < b
if else语句经常与test命令结合使用
1 2 3 4 5 6 7 8
num1=$[2*3] num2=$[1+5] iftest $[num1] -eq $[num2] then echo'Two numbers are equal!' else echo'The two numbers are not equal!' fi
输出结果:
1
Two numbers are equal!
2. for循环
for循环一般格式为:
1 2 3 4 5 6 7
for var in item1 item2 ... itemN do command1 command2 ... commandN done
例如,顺序输出当前列表中的数字:
1 2 3 4
for loop in 1 2 3 4 5 do echo"The value is: $loop" done
输出结果:
1 2 3 4 5
The value is: 1 The value is: 2 The value is: 3 The value is: 4 The value is: 5
顺序输出字符串中的字符:
1 2 3 4
for str in This is a string do echo$str done
输出结果:
1 2 3 4
This is a string
for循环高级用法
1 2 3 4 5 6 7 8 9 10 11 12
#!/bin/bash PREFIX=192.168.1. for i in `seq 100 110` do echo -n "${PREFIX}$i " ping -c5 ${PREFIX}${i} >/dev/null 2>&1 if [ "$?" -eq 0 ];then echo"OK" else echo"Failed" fi done
3. while语句
while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。其格式为:
1 2 3 4 5 6 7 8 9 10 11
while condition do command done #!/bin/bash int=1 while(( $int<=5 )) do echo$int let"int++" done
echo'Enter a number between 1 and 4:' echo'The number you entered is:' read aNum case$aNumin 1) echo'You have chosen 1' ;; 2) echo'You have chosen 2' ;; 3) echo'You have chosen 3' ;; 4) echo'You have chosen 4' ;; *) echo'You did not enter a number between 1 and 4' ;; esac
输入不同的内容,会有不同的结果,例如:
1 2 3 4
Enter a number between 1 and 4: The number you entered is: 3 You have chosen 3
#!/bin/bash while : do echo -n "Enter a number between 1 and 5:" read aNum case$aNumin 1|2|3|4|5) echo"The number you entered is $aNum!" ;; *) echo"The number you entered is not between 1 and 5! game over!" break ;; esac done
执行以上代码,输出结果为:
1 2 3 4
Enter a number between 1 and 5:3 The number you entered is 3! Enter a number between 1 and 5:7 The number you entered is not between 1 and 5! game over!
#!/bin/bash while : do echo -n "Enter a number between 1 and 5: " read aNum case$aNumin 1|2|3|4|5) echo"The number you entered is $aNum!" ;; *) echo"The number you entered is not between 1 and 5!" continue echo"game over" ;; esac done
运行代码发现,当输入大于5的数字时,该例中的循环不会结束,语句 echo "Game is over!" 永远不会被执行。
demoFun(){ echo"This is my first shell function!" } echo"-----Execution-----" demoFun echo"-----Finished-----"
Output the result: -----Execution----- This is my first shell function! -----Finished-----
下面定义一个带有return语句的函数:
1 2 3 4 5 6 7 8 9 10 11 12
#!/bin/bash funWithReturn(){ echo"This function will add the two numbers of the input..." echo"Enter the first number: " read aNum echo"Enter the second number: " read anotherNum echo"The two numbers are $aNum and $anotherNum !" return $(($aNum+$anotherNum)) } funWithReturn echo"The sum of the two numbers entered is $? !"
输出类似下面:
1 2 3 4 5 6 7
This function will add the two numbers of the input... Enter the first number: 1 Enter the second number: 2 The two numbers are 1 and 2 ! The sum of the two numbers entered is 3 !
函数返回值在调用该函数后通过 $? 来获得
所有函数在使用前必须定义。
2. 函数参数
在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 n 的形式来获取参数的值,例如,n的形式来获取参数的值,例如,1表示第一个参数,$2表示第二个参数… 带参数的函数示例:
1 2 3 4 5 6 7 8 9 10 11
#!/bin/bash funWithParam(){ echo"The first parameter is $1 !" echo"The second parameter is $2 !" echo"The tenth parameter is $10 !" echo"The tenth parameter is ${10} !" echo"The eleventh parameter is ${11} !" echo"The total number of parameters is $# !" echo"Outputs all parameters as a string $* !" } funWithParam 1 2 3 4 5 6 7 8 9 34 73
输出结果:
1 2 3 4 5 6 7
The first parameter is 1 ! The second parameter is 2 ! The tenth parameter is 10 ! The tenth parameter is 34 ! The eleventh parameter is 73 ! The total number of parameters is 11 ! Outputs all parameters as a string 1 2 3 4 5 6 7 8 9 34 73 !