Single Line Linux Handler that Checks a Var and Directory Exists
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 fail开发者_开发技巧ing:
DEBUG="TRUE"
[[ ${DEBUG} == "TRUE" && -d /debug ]] && cp /etc/apache2/httpd.conf /debug/httpd.BEFORE.conf
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".
精彩评论