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 Now I want to use this in a sh (or bash) script. Does anybody know the equivalent of the line above for sh? Thanks! |
Try
Quote:
Quote:
|
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! |
Alternatively, using the more robust [[ ]] instead of [ ] and the more concise but less legible || instead of if-then-else
Code:
[[ -d $TEST ]] || mkdir "$TEST" Code:
[[ -e TEST ]] || mkdir TEST 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
|
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! |
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. |