引用其他shell文件
- source xx.sh
- . xx.sh
- 区别:留坑回填
判断语句if
- if关键字后要留空格
- 注意[]与[[]]的区别,尽量使用[[]]
字符串正则匹配("=~")
pattern='a*b$'
if [[ "aaabbbb" =~ $pattern ]];then
echo true
fi
也可以作字符串字串匹配
运算符
- ‘-0’、’-a’ 或、与
- && 、|| 与、或
while使用
while ((cond))
do
xxxx
break
done
case使用
case $var in
str1) xxxx
;;
str2) xxxx
;;
*) xxxx;
esac
shell中类hashmap实现
declare -A animals=( ["moo"]="cow" ["woof"]="dog")
for sound in "${!animals[@]}"; do
echo "$sound - ${animals[$sound]}"
done
传参——默认参数
shell通过$数字来传递参数
在开发过程中会遇到这种case:
不保证执行脚本时会传入该参数
这时应适应默认参数
if [ ! $1 ];then
$1=true
fi
重定向输出
> /dev/null 2>&1
其中,>
是重定向符号,代表将输出结果重定向到后面文件
/dev/null代表不输出任何结果
2>&1
代表将error输出重定向到标准输出
这里0代表stdin,1代表sdtout,2代表stderror