使用 diff 比较文件和变量

问题描述 投票:0回答:1

在 diff 命令中出现以下错误。请协助我如何指定我想看到文件

ran
和变量
current_unavail
之间的区别:

$ current_unavail=ranjith
$ cat /tmp/ran
ranjith
$ test=$(cat /tmp/ran)

我得到的错误

$ diff `$current_unavail` `$test`
diff: missing operand after `diff'
diff: Try `diff --help' for more information.
linux bash shell diff
1个回答
6
投票

你用错了引号。假设

$current_unavail
$test
是两个 shell 变量,每个都包含一个文件的名称,你应该这样做:

diff "$current_unavail" "$test"

反引号

`
用于命令替换(如
a=`cmd`
),尽管首选语法是
a=$(cmd)
.


要比较一个文件

/tmp/ran
和一个变量
$current_unavail
,你可以这样做:

diff /tmp/ran <(echo "$current_unavail")

diff
使用文件描述符,而不是变量。但是在 bash 中,您可以使用 process substitution
<( ... )
从执行命令的结果创建临时文件描述符。

© www.soinside.com 2019 - 2024. All rights reserved.