Test语句
$[ -condition express ] && operation1 || operation2
当条件判断为true
时,执行结果为
operation1
当条件判断为false
时,执行结果为
operation2
数组遍历
$test_list=(1 2 3 4)
$for i in ${test_list[*]};do echo $i;done
1
2
3
4
注意数组遍历时一定要用${arrayname[*]}
或${arrayname[@]}
字符串遍历
$test_string='i am noob'
$for i in $test_string;do echo $i;done
i
am
noob
以\n区分效果相同
以语句的输出赋值
$test_value=$(awk -F "/" '{print $1}' filename)
shell函数
定义
function(){
}
调用
function parameter1 parameter2 ...
通过$1
、$2
等特殊变量获取函数参数,当参数大于等于10个时,需要使用${n}
来获取参数