shell使用记录

引用其他shell文件

  1. source xx.sh
  2. . xx.sh
  3. 区别:留坑回填

判断语句if

  1. if关键字后要留空格
  2. 注意[]与[[]]的区别,尽量使用[[]]

字符串正则匹配("=~")

pattern='a*b$'
if [[ "aaabbbb" =~ $pattern ]];then
echo true
fi

也可以作字符串字串匹配

运算符

  1. ‘-0’、’-a’ 或、与
  2. && 、|| 与、或

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

未完待续

猜你喜欢

转载自blog.csdn.net/csdnxxm/article/details/107763729
今日推荐