LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   line that checks if dir "TEST" exists. If so, nothing, of not mkdir (https://www.linuxquestions.org/questions/programming-9/line-that-checks-if-dir-test-exists-if-so-nothing-of-not-mkdir-828435/)

Mike_V 08-25-2010 01:14 PM

line that checks if dir "TEST" exists. If so, nothing, of not mkdir
 
Hi there,

I have a line of code from a csh script:

Code:

if ( ! -e TEST ) mkdir TEST
it checks if the directory "TEST" exists and if it DOES exist then nothing happens, if it DOES NOT exist then directory TEST will be made.


Now I want to use this in a sh (or bash) script.

Does anybody know the equivalent of the line above for sh?

Thanks!

bluebox 08-25-2010 01:21 PM

Try
Quote:


if [ ! -d $TEST ]; then mkdir $TEST; fi

Edit: okay, better use
Quote:

if [ ! -e $TEST ]; then mkdir $TEST; fi
-d checks for directories only.

Mike_V 08-25-2010 01:40 PM

solved
 
I did have to remove the $ signs... I don't know why.

So this works like a charm:

Code:

if [ ! -e TEST ]; then mkdir TEST; fi

Thanks a lot!

catkin 08-25-2010 01:54 PM

Alternatively, using the more robust [[ ]] instead of [ ] and the more concise but less legible || instead of if-then-else
Code:

[[ -d $TEST ]] || mkdir "$TEST"
EDIT: seeing your reply above, better
Code:

[[ -e TEST ]] || mkdir TEST
EDIT 2:

You had to change $TEST to TEST because $TEST means "the value of variable TEST" whereas you want the string TEST.

On reflection it might be worth handling the error condition of TEST existing but not being a directory.
Code:

if [[ ! -d TEST ]]; then
    if [[ -e TEST ]]; then
        echo 'Cannot create directory TEST; TEST already exists:' >&2
        /bin/ls -l TEST >&2
        exit 1
    fi
    mkdir TEST || exit 1
fi

Notes:
  1. >&2 means "redirect stdout to stderr". It is conventional and useful to write error messages to stderr. It allows error messages to be easily separated from informational messages. By default both stdout and stderr are displayed on the screen.
  2. /bin/ls is used instead of ls because ls is commonly aliased and might give unexpected results.
  3. If mkdir TEST fails it will write an error message to stderr and set its return code to non-zero. The shell, on detecting a non-zero return code, will execute the command after the || thus terminating the script.

Mike_V 08-26-2010 08:26 AM

This is why I love this forum... SO useful!
Bluebox came with a quick solution and then catkin's help teaches me things I can apply elsewhere.
Thanks to both bluebox and catkin!

David the H. 08-26-2010 08:44 AM

By the way, a quick and dirty technique is to simply use mkdir -p. If the directory (including any of its parents) doesn't exist, then it will be created, and if it does exist it will exit without error.

On the other hand, using a bash test first means you can avoid opening up an unnecessary external process, and so is probably better if efficiency is a goal.


All times are GMT -5. The time now is 05:38 AM.