I am making a bash script where I want to find files that are equal to a variable. The equals will then be used.
I want to use “mogrify” to shrink a couple of image files that have the same name as the ones i gather from a list (similar to “dpkg -l”). It is not “dpkg -l” I am using but it is similar. My problem is that it prints all the files not just the equals. I am pretty sure this could be done with awk instead of a for-loop but I do not know how.
prog="`dpkg -l | awk '{print $1}'`"
for file in $dirone* $dirtwo*
do
if [ "basename ${file}" = "${prog}" ]; then
echo ${file} are equal
else
echo ${file} are not equal
fi
done
Could you please help me get this working?
First, I think there’s a small typo.
if [ "basename ${file}" =...should have backticks inside the double quotes, just like theprog=...line at the top does.Second, if
$progis a multi-line string (likedpkg -l) you can’t really compare a filename to the entire list. Instead you have to compare one item at a time to the filename.Here’s an example using
dpkgand/usr/bin