The best way to write the original test
-based solution would be
if test -d "$dir_test" && test –x "$dir_test";
then
cd $dir_test
fi
although what will you do if the test fails and you don't change directories? The remainder of the script will probably not work as expected.
You can shorten this by using the [
synonym for test
:
if [ -d "$dir_test" ] && [ -x "$dir_test" ]; then
or you can use the conditional command provided by bash
:
if [[ -d "$dir_test" && -x "$dir_test" ]]; then
The best solution, since you are going to change directories if the tests succeed, is to simply try it, and abort if it fails:
cd "$dir_test" || {
# Take the appropriate action; one option is to just exit with
# an error.
exit 1
}