Linked Questions

Popular Questions

echo "enter filename"

read filename

fileData=$(file $filename)

if [ $fileData == *"PNG image"* ]
then
echo "this is a PNG file"

elif [ $fileData == *"JPEG image"* ]
then
echo "this is a JPEG file"

elif [ $fileData == *"GIF image"* ]
then
echo "this is a GIF file"

else
echo "error"

fi

So I can't figure out what is wrong with this exactly but it returns the following output with bef0e2e9181980f0d6d23e6c057a47d2 being the file name:

0.sh: 7: [: bef0e2e9181980f0d6d23e6c057a47d2:: unexpected operator
0.sh: 11: [: bef0e2e9181980f0d6d23e6c057a47d2:: unexpected operator
0.sh: 15: [: bef0e2e9181980f0d6d23e6c057a47d2:: unexpected operator
error

but when I run "file bef0e2e9181980f0d6d23e6c057a47d2" I get

bef0e2e9181980f0d6d23e6c057a47d2: JPEG image data, JFIF standard 1.01, resolution (DPCM), density 37x37, segment length 16, baseline, precision 8, 1365x767, frames 3

So to make a long story short, my ultimate goal here is to make a script to help me identify image file types because I have many images without extensions. I will eventually replace the echo commands under the if and elifs with actions to rename the files with the correct extensions. But anyways, it looks to me as though the output of the command which I am attempting to store in variable is for some reason not being stored as a string causing these issues, though I could be wrong.

Related Questions