Bash:在 if 语句中使用差异的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3611846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:38:00  来源:igfitidea点击:

Bash: using the result of a diff in a if statement

bashdiff

提问by Jary

I am writing a simple Bash script to detect when a folder has been modified.

我正在编写一个简单的 Bash 脚本来检测文件夹何时被修改。

It is something very close to:

它非常接近:

ls -lR $dir > a
ls -lR $dir > b

DIFF=$(diff a b) 
if [ $DIFF -ne 0 ] 
then
    echo "The directory was modified"

Unfortunately, the if statement prints an error: [: -ne: unary operator expected

不幸的是,if 语句打印了一个错误:[: -ne: unary operator expected

I am not sure what is wrong with my script, would anyone please be able to help me?

我不确定我的脚本有什么问题,有人可以帮助我吗?

Thank you very much!

非常感谢!

Jary

杰瑞

采纳答案by Lou Franco

ls -lR $dir > a
ls -lR $dir > b

DIFF=$(diff a b) 
if [ "$DIFF" != "" ] 
then
    echo "The directory was modified"
fi

回答by Paul Tomblin

if ! diff -q a b &>/dev/null; then
  >&2 echo "different"
fi

回答by tangens

You are looking for the return value of diffand not the output of diffthat you are using in your example code.

您正在寻找您在示例代码中使用的返回值diff而不是输出diff

Try this:

尝试这个:

diff a b
if [ $? -ne 0 ]; then
    echo "The directory was modified";
fi

回答by Christophe Priieur

If you don't need to know what the changes are, cmpis enough. Plus you can play with the syntactical trick provided by and ||:

如果您不需要知道更改是什么,cmp就足够了。另外,您可以使用 and 提供的语法技巧||

cmp a b || echo 'The directory was modified'

The instruction may be interpreted as: "either a and b are equal, or i echo the message".

该指令可以解释为:“要么 a 和 b 相等,要么我回显消息”。

(The semantic of &&and ||must be handled with care, but here it's intuitive).

(语义的&&||必须小心处理,但在这里它的直观)。

Just for the sake of readability, i actually prefer to put it on two lines:

只是为了可读性,我实际上更喜欢把它放在两行:

cmp a b \
  || echo 'The directory was modified'

回答by Aaron D. Marasco

DIFF=$(diff -u <(find dir1/ -type f -printf '%P\n' | sort) <(find dir2/ -type f -printf '%P\n' | sort))
if [ "$DIFF" ]; then
  echo "Directories differ"
  # Do other stuff here
fi

This uses one of my favorite bashisms, the <()process substitution.

这使用了我最喜欢的 bashism 之一,即<()过程替换。

The $DIFFvariable holds a printable difference. If you want to show it to the end user, be sure to double-quote it, e.g. echo "$DIFF".

$DIFF变量包含可打印的差异。如果您想将其显示给最终用户,请务必将其双引号,例如echo "$DIFF".

If you want to only tell the user there was anydifference, if can be shortened to something like [ "$(diff ...)" ] && echo "Difference found"

如果你只想告诉用户有什么不同,如果可以缩短为类似[ "$(diff ...)" ] && echo "Difference found"

Note: I'm assuming the original question meant to have dir1and dir2to make a little more sense. If it was dirat time 0 and then dirat time 1, this approach obviously wouldn't work.

:我假设意味着有原来的问题dir1,并dir2做出一点更有意义。如果它是dir在时间 0 然后dir在时间 1,这种方法显然行不通。