bash diff commnad 不工作“'diff' 后缺少操作数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29633449/
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-18 12:47:42  来源:igfitidea点击:

diff commnad not working "missing operand after `diff'"

linuxbashshelldiff

提问by ranjith

In diff command am getting following error. Kindly assist how can I specify I want to see difference in two files:

在 diff 命令中出现以下错误。请协助我如何指定我想查看两个文件的差异:

#current_unavail=ranjith

root@iitmserver1 tmp]# cat /tmp/ran
ranjith
[root@iitmserver1 tmp]#

#test=$(cat /tmp/ran)

[root@iitmserver1 tmp]# diff `$current_unavail` `$test`
diff: missing operand after `diff'
diff: Try `diff --help' for more information.
[root@iitmserver1 tmp]#

回答by Sammitch

difftakes two filenamesas arguments, where you appear to be passing in file contentsas the first argument. You will want to change your script/commands to look more like:

diff将两个文件名作为参数,您似乎将文件内容作为第一个参数传递。您需要将脚本/命令更改为更像:

current_unavail=/tmp/unavail_cn.out
result=$(diff $current_unavail /moes/home/pharthiphan/scripts/monitoring/unavail_cn/$last_unavail)

Alternatively, you can use Process Substitutionto pass the output of a command into another command that is expecting a file. eg:

或者,您可以使用进程替换将命令的输出传递到另一个需要文件的命令中。例如:

diff <(echo -e "foo\nbar") <(echo -e "foo\nbaz")

However, while good to know about, this would seem to be a needless level of complexity for your current problem.

然而,虽然很高兴知道,但对于您当前的问题来说,这似乎是不必要的复杂程度。