lundi 17 juin 2019

Use value of variable literally in if statement (bash)

I need to check if the value of a variable is a path that exists. This is being read from a text file.

Basically, the point I'm stuck at, the line variable is as follows: location_of_folder=~/Desktop/folder\ with\ spaces

I need to check if the path after location_of_folder= exists.

Here's what I've tried:

foo="${line#'location_of_folder='}"
if ! [[ -d "${foo}" ]]
then
  echo 'This path exists.'
else
  echo 'This path does not exist.'
fi

if ! [[ -d "${line#'location_of_folder='}" ]]
then
  echo 'This path exists.'
else
  echo 'This path does not exist.'
fi

However both say the path is nonexistant, which is indeed not true.

And yes, inside of the text file I'm reading from looks like:

location_of_folder=~/Desktop/folder\ with\ spaces

Using bash 3.2.57(1)-release under OSX El Capitan 10.11.6.

Thank you.

Aucun commentaire:

Enregistrer un commentaire