oxidworks
oxidworks

Reputation: 1642

Verify folder name in a bash script

How can I veify a folder name in a bash script.

I am in the folder named "test". Now I want to check the is really "text".

I have tried the following:

cd tmp
mkdir testfolder
cd testfolder
if [["${PWD##*/}"]] == "testfolder"
then echo "ok"
fi
done

But always get the error, that testfolder not found, it tries to run if as a command.

Thanks

Upvotes: 0

Views: 58

Answers (2)

choroba
choroba

Reputation: 241868

You need to include the whole comparison into [[ ... ]]:

if [[ ${PWD##*/} == testfolder ]]

Also note that spaces around [[ aren't optional. Double quotes aren't needed in [[ ... ]], but they are needed if you switch to single [ ... ].

Upvotes: 1

Inian
Inian

Reputation: 85590

The actual check condition should have been

if [ "${PWD##*/}" == "testfolder" ];

Or you can use the test operator [[]] with the return code of the comparison performed, something like:-

 [[ "${PWD##*/}" == "testfolder" ]] && echo "Match"

Upvotes: 1

Related Questions