加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell下变量比较最佳实践

发布时间:2020-12-15 23:04:04 所属栏目:安全 来源:网络整理
导读:https://stackoverflow.com/questions/13617843/unary-operator-expected ? If you know you‘re always going to use bash,it‘s much easier to always use the double bracket conditional compound command [[ ... ]] ,instead of the Posix-compatible?

https://stackoverflow.com/questions/13617843/unary-operator-expected

?

If you know you‘re always going to use bash,it‘s much easier to always use the double bracket conditional compound command [[ ... ]],instead of the Posix-compatible? single bracket version [ ... ]. Inside a [[ ... ]] compound,word-splitting and pathname expansion are not applied to words,so you can rely on

if [[ $aug1 == "and" ]];

to compare the value of $aug1 with the string and.

If you use [ ... ],you always need to remember to double quote variables like this:

if [ "$aug1" = "and" ];

If you don‘t quote the variable expansion and the variable is undefined or empty,it vanishes from the scene of the crime,leaving only

if [ = "and" ];

which is not a valid syntax. (It would also fail with a different error message if $aug1 included white space or shell metacharacters.)

The modern [[ operator has lots of other nice features,including regular expression matching.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读