0

I'm extracting the icon path from the .desktop files.

Sometimes the files contain the absolute path to the file i.e

Icon=/snap/spotify/41/usr/share/spotify/icons/spotify-linux-128.png

Other times its just the name of the file no ext

Icon=spotify-linux-128

I'm currently searching the /usr/share/icons/hicolor folders for the icon name. But when the icon path is absolute and points to a file i'm getting no results as find is looking in the wrong location.

Is there a way to tell in my bash script if a string is an image?

I'm trying to check with:

file $iconpath 

But obviously it errors if the path does not exist.

Is there a better way?

What would you do?

2
  • 1
    Just check if the string begins with a /? – muru Jun 13 '20 at 5:49
  • My brains gone. Its 6am here. Thanks – Lewis Morris Jun 13 '20 at 5:52
1

As @muru pointed out.

The obvious solution is to check if the string starts /

i.e

if [[ $icon == /* ]] 
then
    echo "is a file path"
else
    echo "is not a file path"
fi

EDIT:

you can also do this to check if a file exists

if [ -a $icon ];
then
    echo "File exists"
else
    echo "File does not exist"
fi
0

Or just chop off any path elements up to and including the last /

Icon=spotify-linux-128
echo ${Icon##*/}
spotify-linux-128

Icon=/snap/spotify/41/usr/share/spotify/icons/spotify-linux-128.png
echo ${Icon##*/}
spotify-linux-128.png

Then just throw a * on the end and feed the output to find with your specified search root

ls -1 sp*
spotify-linux-128
spotify-linux-128.png
spotify-linux-128.png.banana
spotify-linux-128.png.png

Icon=/snap/spotify/41/usr/share/spotify/icons/spotify-linux-128.png
fname=${Icon##*/}
find . -maxdepth 1 -iname "$fname*" 

./spotify-linux-128.png.banana
./spotify-linux-128.png
./spotify-linux-128.png.png

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.