bsky
bsky

Reputation: 20222

Command is_dir not found

I'm trying to check if a folder exists. If it doesn't, I create it.

I have this code:

if [ $(is_dir "$contaniningdir/run") = "NO"]; then
  mkdir "$containingdir/run"
fi

However, I'm getting:

is_dir: command not found

So how what's the correct way of doing this?

Upvotes: 0

Views: 296

Answers (2)

trainoasis
trainoasis

Reputation: 6720

You should use

if [ ! -d "$DIRECTORY" ]; then
  # your mkdir and other stuff ... 
fi

as per this question/answer.. Another relevant question/answer is here.

One of the comments also mentions an important notice:

One thing to keep in mind: [ ! -d "$DIRECTORY" ] will be true either if $DIRECTORY doesn't exist, or if does exist but isn't a directory.

For more you should probably check that other question's page.

is_dir is a PHP function that you probably mixed with bash unintentionally :)

Upvotes: 3

user594138
user594138

Reputation:

bash is capable of checking for the existence of a directory without external commands:

if [ ! -d "${containingdir}/run" ]; then
  mkdir "${containingdir}/run"
fi

! is negation, -d checks if the argument exists and is a directory

Upvotes: 1

Related Questions