#!/bin/bash # 定义 变量名和等号之间不能有空格 your_name="dong4j" # 使用 echo ${your_name} # 循环输出 for skill in Ada Coffe Action Java do echo "I am good at ${skill} Script" done
# 如果变量为空或被删除(unset),则返回 word,不改变 var的值 echo ${var:-"Variable is not set"} echo "1 - Value of var is ${var}"
# 如果变量为空或被删除(unset),则返回 word,改变 var的值为 word echo ${var:="Variable is not set"} echo "2 - Value of var is ${var}"
unset var # 如果变量 var 被定义,那么返回 word,但不改变 var 的值。 echo ${var:+"This is default value"} echo "3 - Value of var is ${var}"
var="Prefix" echo ${var:+"This is default value"} echo "4 - Value of var is ${var}"
# 如果变量 var 为空或已被删除(unset),那么将消息 message 送到标 准错误输出,可以用来检测变 量 var 是否可以被正常赋值。若此替换出现在 Shell 脚本中,那么脚本将停止运行。 echo ${var:?"Print this message"} echo "5 - Value of var is ${var}"
# format-string 为双引号 $ printf"%d %s\n" 1 "abc" 1 abc # 单引号与双引号效果一样 $ printf'%d %s\n' 1 "abc" 1 abc # 没有引号也可以输出 $ printf %s abcdef abcdef # 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用 $ printf %s abc def abcdef $ printf"%s\n" abc def abc def $ printf "%s %s %s\n" a b c d e f g h i j abc def ghi j # 如果没有 arguments,那么 %s 用 NULL 代替,%d 用0代替 $ printf"%s and %d \n" and 0 # 如果以 %d 的格式来显示字符串,那么会有警告,提示无效的数字,此时默认置为0 $ printf"The first program always prints'%s,%d\n'" Hello Shell -bash: printf: Shell: invalid number The first program always prints 'Hello,0' $
if else 语句
if .. eles
1 2 3 4
if [ expression ] then do something if
if .. else .. fi
1 2 3 4
if [ expression ] then Statement(s) to be executed if expression is true else Statement(s) to be executed if expression is not true fi
if .. elif .. fi
1 2 3 4 5 6 7 8 9 10 11 12
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
shell case esac (switch case)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
case 值 in 模式1) command1 command2 command3 ;; 模式2) command1 command2 command3 ;; *) command1 command2 command3 ;; esac
tomcat 命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/bin/bash # $0 代表 sh 文件名 # $1 第一个参数 如果为 start 则 case $1 in start) sh /Library/Tomcat/bin/startup.sh ;; stop) sh /Library/Tomcat/bin/shutdown.sh ;; restart) sh /Library/Tomcat/bin/shutdown.sh sh /Library/Tomcat/bin/startup.sh ;; *) echo “Usage: start|stop|restart” ;; esac exit 0
shell for
1 2 3 4 5 6
for 变量 in 列表 do command1 command2 ... done
列表可以是一组值 (数字 字符串等) 组成的序列,四通空格分隔
shell while
1 2 3 4
while command do Statement(s) to be executed if command is true done
1 2 3 4 5 6
COUNTER=0 while [ $COUNTER -lt 5 ] do COUNTER='expr $COUNTER+1' echo $COUNTER done
1 2 3 4 5
# 将从键盘读到的值给 FILM while read FILM do echo "Yeah! great film the $FILM" done
shell until 循环
与 while 相反
1 2 3 4
until command do Statement(s) to be executed until command is true done
while : do echo -n "Input a number between 1 to 5: " read aNum case $aNum in 1|2|3|4|5) echo "Your number is $aNum!" ;; *) echo "You do not select a number between 1 to 5, game is over!" break ;; esac done # 跳出第二层循环 从内往外数 for var1 in 1 2 3 do for var2 in 0 5 do if [ $var1 -eq 2 -a $var2 -eq 0 ] then break 2 else echo "$var1 $var2" fi done done
continue 与其他语言一样
shell 中的函数
先定义 后使用
1 2 3 4 5
(function) function_name () { list of commands # 如果不加 return 语句,默认将最后一条命令的结果作为返回值 [ return value ] }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/bin/bash
funWithReturn(){ echo "The function is to get the sum of two numbers..." echo -n "Input first number: " read aNum echo -n "Input another number: " read anotherNum echo "The two numbers are $aNum and $anotherNum !" return $(($aNum+$anotherNum))
}
funWithReturn # Capture value returnd by last command
funWithParam(){ echo "The value of the first parameter is $1 !" echo "The value of the second parameter is $2 !" echo "The value of the tenth parameter is $10 !" echo "The value of the tenth parameter is ${10} !" echo "The value of the eleventh parameter is ${11} !" echo "The amount of the parameters is $# !" # 参数个数 echo "The string of the parameters is $* !" # 传递给函数的所有参数