Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 failing:

DEBUG="TRUE"

[[ ${DEBUG} == "TRUE" && -d /debug ]] && cp /etc/apache2/httpd.conf /debug/httpd.BEFORE.conf

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading