引用其他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
这篇博客详细记录了Shell脚本的使用,包括引用其他shell文件、if判断语句、字符串正则匹配、运算符、while和case的使用,还介绍了如何实现类hashmap功能、设置默认参数及重定向输出等核心概念。文章以实例为主,帮助读者深入理解Shell编程。
754

被折叠的 条评论
为什么被折叠?



