如果文件名作为变量传递,如何使用diff命令比较两个tar文件的内容

时间:2017-09-26 11:54:08

标签: linux bash shell

我试图在shell脚本中使用diff来比较两个tar文件内容,但是我收到了错误:

"syntax error near unexpected token `('
final_comp.sh: line 20: `diff <(tar -tvf /tmp/All_Configs/prac/$file1 | sort) <(tar -tvf /tmp/All_Configs/22-Sep/$file2 | sort) > output.txt ' " .

脚本:

#!usr/bin/bash
ls -p /tmp/All_Configs/prac|grep -v /| while read -r file1
do
    ls -p /tmp/All_Configs/22-Sep|grep -v /| while read -r file2
    do
        if [ "$file1" = "$file2" ]; then
            diff <(tar -tvf /tmp/All_Configs/prac/$file1 | sort) <(tar -tvf /tmp/All_Configs/22-Sep/$file2 | sort) > output.txt
        fi;
    done
done

1 个答案:

答案 0 :(得分:1)

在shebang中的拼写错误应为#!/usr/bin/bash,这可能会阻止流程替换<( .. )

避免文件名扩展在$ file1和$ file2

附近添加双引号
diff <(tar -tvf /tmp/All_Configs/prac/"$file1" | sort) <(tar -tvf /tmp/All_Configs/22-Sep/"$file2" | sort) > output.txt
然而,整个过程可能会得到改善。

dir1=/tmp/All_Configs/prac
dir2=/tmp/All_Configs/22-Sep
for pathfile1 in "$dir1"/*.tar; do
    file=${pathfile1##*/}  # removes longest prefix */
    # for each *.tar in dir1 check if that file exists in dir2
    if [[ -f $dir2/$file ]]; then
        diff <( tar -tvf "$dir1/$file" |sort ) <( tar -tvf "$dir2/$file" |sort )
    fi
done

请注意,右右括号之间不需要双引号。