Command is_dir not found - bash

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?

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

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

Related

Bash script - File directory does not exist

I'm creating a very simple bash script that will check to see if the directory exists, and if it doesn't, create one.
However, no matter what directory I put in it doesn't find it!
Please tell me what I'm doing wrong.
Here is my script.
#!/bin/bash
$1="/media/student/System"
if [ ! -d $1 ]
then
mkdir $1
fi
Here is the command line error:
./test1.sh: line 2: =/media/student/System: No such file or directory
Try this
#!/bin/bash
directory="/media/student/System"
if [ ! -d "${directory}" ]
then
mkdir "${directory}"
fi
or even shorter with the parent argument of mkdir (manpage of mkdir)
#!/bin/bash
directory="/media/student/System"
mkdir -p "${directory}"
In bash you are not allow to start a variable with a number or a symbol except for an underscore _. In your code you used $1 , what you did there was trying to assign "/media/student/System" to $1, i think maybe you misunderstood how arguments in bash work. I think this is what you want
#!/bin/bash
directory="$1" # you have to quote to avoid white space splitting
if [[ ! -d "${directory}" ]];then
mkdir "$directory"
fi
run the script like this
$ chmod +x create_dir.sh
$ ./create_dir.sh "/media/student/System"
What the piece of code does is to check if the "/media/student/System" is a directory, if it is not a directory it creates the directory

Reading a Directory and Verifying if it exists Bash

I have checked everywhere and tried many different "Solutions" on checking to see if the directory exists. Here's my code:
#!/bin/bash
echo -e "Where is the directory/file located?"
read $DIRECTORY
if [ -d "$DIRECTORY" ]; then
echo "Exists!"
else
echo "Does not exist!"
fi
What I am trying to do is have the user input a directory and for the script to check if it exists or not and return a result. This will ultimately tar/untar a directory. Regardless of whether the directory exists or not, it returns the answer "Does not exist!". (The input i'm trying is ~/Desktop, and from what I know that is 100% correct. Any concise answers are much appreciated :).
Your script can be refactored to this:
#!/bin/bash
read -p 'Where is the directory/file located?' dir
[[ -d "$dir" ]] && echo 'Exists!' || echo 'Does not exist!'
Basically use read var instead of read $var
Better not to use all caps variable names in BASH/shell
Use single quotes while using ! in BASH since it denotes a history event

bash - change directory inside a script wont work

Try to find a solution for a problem but stucked with the following :
I have a path of a folder (I got full path and partial path).
Im tying to cd to that folder, but it keep saying "No such file or directory".
Thats the partial code :
for var in "$#" ; do
if [[ -d $var ]] ; then
if [ "$(ls -A $var)" ]; then
cd $var
Would appericiate any help :)
Thanks
I think this line is your problem:
if [ "$(ls -A $var)" ]; then
Why do you need this test at all? Your previous check looks for the existence of the directory already.

writing a shell script if statement to check for directory

I need to write a script that will recreate my opt folder if it gets deleted when I remove a package from it. Here's a link to my previous post: dpkg remove to stop processes
Now, the issue I'm running into could be better described here: http://lists.debian.org/debian-devel/2006/03/msg00242.html
I was thinking of just adding a postrem script which checks if an opt directory exists, and if not, creates one. My experience with shell scripts is pretty limited though..
[ -d "$dir" ] || mkdir -p "$dir"
This could be written more verbosely / clearly as:
if ! test -d "$dir"; then
mkdir -p "$dir"
fi
See help test for more information.

How to use Bash to create a folder if it doesn't already exist?

#!/bin/bash
if [!-d /home/mlzboy/b2c2/shared/db]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi;
This doesn't seem to work. Can anyone help?
First, in Bash [ is just a command, which expects string ] as a last argument, so the whitespace before the closing bracket (as well as between ! and -d which need to be two separate arguments too) is important:
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi
Second, since you are using -p switch for mkdir, this check is useless, because this is what it does in the first place. Just write:
mkdir -p /home/mlzboy/b2c2/shared/db;
and that's it.
There is actually no need to check whether it exists or not. Since you already wants to create it if it exists , just mkdir will do
mkdir -p /home/mlzboy/b2c2/shared/db
Simply do:
mkdir /path/to/your/potentially/existing/folder
mkdir will throw an error if the folder already exists. To ignore the errors write:
mkdir -p /path/to/your/potentially/existing/folder
No need to do any checking or anything like that.
For reference:
-p, --parents no error if existing, make parent directories as needed http://man7.org/linux/man-pages/man1/mkdir.1.html
You need spaces inside the [ and ] brackets:
#!/bin/bash
if [ ! -d /home/mlzboy/b2c2/shared/db ]
then
mkdir -p /home/mlzboy/b2c2/shared/db
fi
Cleaner way, exploit shortcut evaluation of shell logical operators. Right side of the operator is executed only if left side is true.
[ ! -d /home/mlzboy/b2c2/shared/db ] && mkdir -p /home/mlzboy/b2c2/shared/db
I think you should re-format your code a bit:
#!/bin/bash
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
mkdir -p /home/mlzboy/b2c2/shared/db;
fi;
Create your directory wherever
OUTPUT_DIR=whatever
if [ ! -d ${OUTPUT_DIR} ]
then
mkdir -p ${OUTPUT_DIR}
fi

Resources