庆典如果-a VS -e选项 [英] bash if -a vs -e option

查看:95
本文介绍了庆典如果-a VS -e选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个有关-a和Bash文档中的-e选项(<一个href=\"http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Ex$p$pssions\">http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Ex$p$pssions)说:

  -a文件
    如果文件存在属实。
-e文件
    如果文件存在属实。

试图让不同的是什么,我跑到下面的脚本:

  resin_dir = /测试/ Resin_wheleph /结果如果[-e $ {} resin_dir]。然后
    回声-e
科幻如果[! -e $ {} resin_dir]。然后
    回声-e!;
科幻如果[-a $ {} resin_dir]。然后
    回声-a
科幻如果[! -a $ {} resin_dir]。然后
    回声-a!;
科幻

/测试/ Resin_wheleph /结果存在且是一个目录。这是我所得到的:

  -e
-一个
! -一个

这似乎有些奇怪(通知-a和!-a)。但是,当我在类似的脚本使用双括号(。例如如果[[-e $ {} resin_dir] ),它提供了合理的输出:

  -e
-一个

所以:


  1. 之间有什么-a和一个差-e选项?

  2. 为什么单括号内使用时-a会产生一个奇怪的结果?


解决方案

我的研究,这是相当毛毛:

-a 是pcated德$ P $,所以没有在联机帮助页的/ usr / bin中/测试了,但还是在一个舞会。使用 -e 。对于单'[',在bash内置的行为等同于测试 bash的内建命令,其行为一样的/ usr / bin中/ [的/ usr / bin中/测试(一个是符号链接,其他)。注意 -a 的效果取决于自己的立场:如果它在一开始,它意味着文件存在。如果是在两个前pressions中间,这意味着逻辑

[! -a /路径]放大器;&安培;回声存在不起作用,如bash的手册指出, -a 被认为是一个二元运算符那里,所以上面没有解析为否定-a .. 但作为一个如果'!和/路径是真的(非空)。因此,你的脚本始终输出 - 一个(实际上为文件的测试),而 -a!这实际上是一个二进制在这里。

有关 [ -a 不作为二进制了(&放大器;&安培; 使用有),所以其独特的目的是来检查文件存在(虽然是德precated) 。因此,否定实际上做你所期望的。

Both about "-a" and "-e" options in Bash documentation (http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions) is said:

-a file
    True if file exists.
-e file
    True if file exists.

Trying to get what the difference is I ran the following script:

resin_dir=/Test/Resin_wheleph/Results

if [ -e ${resin_dir} ] ; then
    echo "-e ";
fi

if [ ! -e ${resin_dir} ] ; then
    echo "! -e";
fi

if [ -a ${resin_dir} ] ; then
    echo "-a";
fi

if [ ! -a ${resin_dir} ] ; then
    echo "! -a";
fi

/Test/Resin_wheleph/Results exists and is a directory. And this is what I get:

-e
-a
! -a

which seems to be a little strange (notice "-a" and "! -a"). But when I use double brackets (e. g. if [[ -e ${resin_dir} ]]) in the similar script it gives reasonable output:

-e
-a

So:

  1. What is a difference between "-a" and "-e" options?
  2. Why "-a" produces a strange result when used inside single brackets?

解决方案

I researched, and this is quite hairy:

-a is deprecated, thus isn't listed in the manpage for /usr/bin/test anymore, but still in the one for bash. Use -e . For single '[', the bash builtin behaves the same as the test bash builtin, which behaves the same as /usr/bin/[ and /usr/bin/test (the one is a symlink to the other). Note the effect of -a depends on its position: If it's at the start, it means file exists. If it's in the middle of two expressions, it means logical and.

[ ! -a /path ] && echo exists doesn't work, as the bash manual points out that -a is considered a binary operator there, and so the above isn't parsed as a negate -a .. but as a if '!' and '/path' is true (non-empty). Thus, your script always outputs "-a" (which actually tests for files), and "! -a" which actually is a binary and here.

For [[, -a isn't used as a binary and anymore (&& is used there), so its unique purpose is to check for a file there (although being deprecated). So, negation actually does what you expect.

这篇关于庆典如果-a VS -e选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆