user3185460
user3185460

Reputation: 57

how to compare two tar files's content using diff command if file name is passed as variable

I am trying to compare two tar files content using diff inside shell script , but I am getting error :

"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 ' " .

script :

#!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

Upvotes: 0

Views: 1173

Answers (1)

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

there's a typo in shebang should be #!/usr/bin/bash which may prevent process substitution <( .. )

to avoid filename expansion add double quote around $file1 and $file2

diff <(tar -tvf /tmp/All_Configs/prac/"$file1" | sort) <(tar -tvf /tmp/All_Configs/22-Sep/"$file2" | sort) > output.txt

however the whole process may be improved.

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

Note that double quotes are not needed between double right brackets.

Upvotes: 1

Related Questions