使用diff bash将文件与程序输出进行比较

时间:2018-03-26 20:22:38

标签: bash

我正在尝试将文件的内容与程序($1)输出进行比较,但是脚本没有做任何事情(只是继续运行)。如果我使用另一个文件代替<($1 $file),它可以正常工作。我不知道我在这里做错了什么。

for file in $2/*.in
do
    if diff ${file%in}out <($1 $file) >/dev/null; then echo "something."; else echo "something2."
    fi
done

还有一种方法可以将($1 $file)的输出存储在变量中吗?

当我比较相同的文件(${file%in}out)时,我得到:

something
something
something

(我有3个文件和3个文件)

1 个答案:

答案 0 :(得分:0)

您的$1命令是否打印出任何标准输出,或只写一个新的.out文件?

这是我的小测试来说明这一点:

## sanity check, have a diff
date > date.out
sleep 1
diff date.out <(date) 

## no diff 
writedate() { date > date.out; sleep 1; }
writedate
diff date.out <(writedate) # no diff

## overwrites the files, but still get a diff
writedate() { date | tee date.out; sleep 1; }
writedate
diff date.out <(writedate) 

如果是这种情况,您可以在运行命令之前复制原始文件并进行比较

cmd=$1
indir=$2
for file in $indir/*.in; do
    outfile=${file%in}out
    prevoutfile=$outfile.old
    mv $outfile $prevoutfile
    $cmd $file # creates $outfile
    if diff $prevout $outfile >/dev/null; then 
      echo "something.";
    else
      echo "something2."
    fi
    mv $prevoutfile $outfile
done