1

This question already has an answer here:

I currently have a directory in a string, as such: DESTDIR="/var/files/mydir/filedir/"

Note that this directory does exist. I made sure of that.

I need to check if this directory already exists, or if I need to create it. I am currently doing this using:

if [ -d  $DESTDIR ]; then
    echo the directory exists!
else
    echo the directory does not exist!
fi

and, even though the directory does exist - I copy and pasted the name into the variable in my script - it always goes to the else statement. Not only that, but when I do ls $DESTDIR within the script I get a no such file or directory error. However, when I run ls $DESTDIR from the command line, I get the expected result.

I've tried replacing $DESTDIR with "$DESTDIR", but I still don't get the expected result.

marked as duplicate by Gilles shell-script Jul 9 '15 at 23:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • What's the output of ls -ld "$DESTDIR" and printf %s "$DESTDIR" | od -t x1 ? – Gilles Jul 9 '15 at 22:52
  • @Gilles first one: ls: cannot access: No such file or directory. Second one: 0000000. – farid99 Jul 9 '15 at 22:57
  • 1
    You haven't set the DESTDIR variable. Run that in your script, after setting the variable. – Gilles Jul 9 '15 at 23:02
  • @Gilles the last value in the second one is 0d, so there is a carriage return key being hit there. Ok. Thank you. – farid99 Jul 9 '15 at 23:09
  • @Gilles I don't understand how a question with a completely different title can be a duplicate of another. the other question asks about Notepad++ and nano while I am asking a more general question that has nothing to do with the text editor used. Despite the solution being the same. I did my research and looked around stack looking for an answer to my question before asking, and did not find the question you marked and would not have looked at it even if I did find it, because it looks completely different. – farid99 Jul 16 '15 at 14:59
4

You can just do mkdir -p "$DESTDIR" -- the -p option will suppress errors if it already exists.

  • Man, i totally misunderstood this question - i thought it was about the sstring - not the path. But you didn't. – mikeserv Jul 9 '15 at 17:27
0

It looks from here like $DESTDIR (aka /var/files/mydir/file) is actually a /file/ instead of a /directory/. The -d test asks whether you have a /directory/. Check the output of ls -ld $DESTDIR to confirm. If $DESTDIR is indeed a file, then the -d test is doing the right thing -- just not what you expected.

  • that's terrible naming on my part. I'll change it for the example. But just a comment - if I run ll /var/files/mydir/ from the command line, I get that the directory I am interested in is indeed a directory. – farid99 Jul 9 '15 at 15:33
  • Is $DESTDIR actually set within the script? Or are you exporting it from your current shell? – Jeff Schaller Jul 9 '15 at 15:51
  • I have the line DESTDIR="/var/files/mydir/..." shown above within my script. The directory is not made within the script, though. And I am not exporting anything. All I did was pick a directory and copy/paste its path onto my script. Would that be the problem? How come? – farid99 Jul 9 '15 at 15:53
  • Can you add a: print '%s\n' "$DESTDIR" before your if [ -d ... line just to confirm that DESTDIR is being set as you expect? – Jeff Schaller Jul 9 '15 at 16:00
  • yup, if I do echo ls $DESTDIR, then from my console copy and paste this command, I get the expected result. Is there any chance this is a permissions issue? – farid99 Jul 9 '15 at 17:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.