I’m wanting to create a one-liner in a Bash script to check if a string variable matches as well as a directory exists?
Currently have this, but it’s failing:
DEBUG="TRUE"
[[ ${DEBUG} == "TRUE" && -d /debug ]] && cp /etc/apache2/httpd.conf /debug/httpd.BEFORE.conf
>Solution :
Here is one possible way to write a single-line Linux command that checks if a variable and directory exist:
if [[ -n $VAR && -d $DIR ]]; then echo "Variable and directory exist"; fi
This command uses the if statement to check if the variable VAR is non-empty (-n $VAR) and if the directory DIR exists (-d $DIR). If both conditions are true, the command will print "Variable and directory exist".