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

Bash,混淆不同文件测试的结果(test -f)

发布时间:2020-12-15 22:07:24 所属栏目:安全 来源:网络整理
导读:我在这个表达式中对bash感到困惑: $var="" # empty var$test -f $var; echo $? # test if such file exists0 # and this file exists,amazing!$test -f ""; echo $? # let's try doing it without var1 # and all ok 我无法理解这种bash行为,也许有人可以解
我在这个表达式中对bash感到困惑:

$var="" # empty var
$test -f $var; echo $? # test if such file exists
0 # and this file exists,amazing!
$test -f ""; echo $? # let's try doing it without var
1 # and all ok

我无法理解这种bash行为,也许有人可以解释一下吗?

解决方法

这是因为$var的空扩展在测试之前被删除了.你实际上正在运行test -f,因此只有一个arg要测试,即-f.根据POSIX,像-f这样的单个arg是真的,因为它不是空的.

从POSIX test(1) specification开始:

1 argument:
Exit true (0) if `$1` is not null; otherwise,exit false.

永远不会测试具有空文件名的文件.现在使用显式测试-f“”有两个args,-f被识别为“测试存在路径参数”的运算符.

(编辑:李大同)

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

    推荐文章
      热点阅读