简体   繁体   中英

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

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 [ ... ] .

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM