Moving To a Correct Known Location in a Bash Script

Sometimes you need to make sure that a bash script is run from a specific location. One way to do that is to keep doing a cd .. until getting to a known good spot in a tree. This BASH code will iteratively move up a folder until a folder containing a specific subfolder is reached and stop. while [[ $PWD != / ]] ; do [ -d "$PWD/invoca_ctf_2022" ] && echo "$PWD" && break cd .. done if [[ $PWD == / ]] ; then exit 0; fi;

No comments

Post a Comment