Shell脚本基础:变量使用全解析
1. 长行规则例外与代码可读性
在编写Shell脚本时,长行规则通常是有益的,但也存在例外情况。当创建要通过Secure Shell (SSH) 传递的行或在某些特定情况下,拆分长行可能会带来更多的代码问题。不过在大多数情况下,拆分长行是有意义的。
在拆分长行时,应尽量将最有意义的部分放在左边,因为我们是从左到右阅读Shell代码的,这样续行的特殊情况会更突出,也便于在代码左侧查找续行。以下是不同代码格式的示例及分析:
# Good
[ $results ] \
&& echo "Got a good result in $results" \
|| echo 'Got an empty result, something is wrong'
# Also good
[ $results ] && echo "Got a good result in $results" \
|| echo 'Got an empty result, something is wrong'
# OK, but not ideal
[ $results ] && echo "Got a good result in $results" \
|| echo 'Got an empty result, something is wrong'
# Bad
[ $results ] && echo "Got a good result in $results" || echo 'Got an empty re
超级会员免费看
订阅专栏 解锁全文
840

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



