条件 if-then-elif-then-fi if的条件部分经常使用test EXPRESSION或[ EXPRESSION ]实现,test的用法可以参见test if 条件1 #if 条件1;then then 执行语句1 elif 条件2 #elif 条件2;then then 执行语句2 else 执行语句3 fi #条件结束标识,即将if反过来 举个栗子 #!/bin/sh var="a test string" if [ '$var' = 'a test string'…
centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课 return用在函数中exit用在shell当中 直接退出整个脚本,整个子shell或当前shellbreak退出循环 上半节课 if 判断case判断shell脚本中的循环 下半节课 for whileshell中的函数breakcontinue 课程大纲(继续上节课的) 7. if…
总结下shell中的函数用法 #!/bin/bash function add_v1() { echo "call function add" } function add_v2() { echo "number of params: $#" echo "first param: $1" echo "second param: $2" local for item in $@ do sum=$(($sum + $item))…
函数是被赋予名称的脚本代码块,可以在代码的任意位置重用.每当需要在脚本中使用这样的代码块时,只需引用该代码块被赋予的函数名称. 创建函数 格式 function name { commands } name 属性定义了该函数的唯一名称.name 后面要有空格. commands 是组成函数的一条或多条 bash shell 命令. 另一种格式 name() { commands } 示例 #!/bin/bash# using a function in a script function fun…
脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 #!/bin/bash function Check() # 使用function定义函数 { Say # 通过函数名直接调用函数 if test $1 then return 0 # 使用return语句返回值: else echo "Command not implemented for that parameter!" exit 2 #…
示例脚本及注释 #!/bin/bash function Check() # 使用function定义函数 { Say # 通过函数名直接调用函数 if test $1 then return 0 # 使用return语句返回值: else echo "Command not implemented for that parameter!" exit 2 # 退出当前shell,并设置退出码为2: fi } Say() # 直接定义函数 { echo "This is a t…