Shell Variable Issue When Trying to Mkdir

Shell variable issue when trying to mkdir

The quotes prevent the expansion of ~.

Use:

CLIENT_BUILD_DIR=~/Desktop/TempDir/

if [ ! -d "$CLIENT_BUILD_DIR" ]
then mkdir "$CLIENT_BUILD_DIR"
fi

Use of variable in mkdir command

The problem isn't the mkdir command, but the variable assignment. ~ is only expanded to your home directory if you leave it unquoted. If you quote it you get a literal tilde character. Leave out the double quotes.

$ K=~/a/`date +%Y%m%d`
$ echo $K
/home/kurs/a/20190926
$ mkdir $K

It's a good idea to quote variable expansions or else file names with spaces and other unusual characters will mess you up. I recommend you write:

$ mkdir "$K"

mkdir error in bash script

Change:

mkdir -p $deploydir

to

mkdir -p "$deployDir"

Like most Unix shells (maybe even all of them), Bourne (Again) Shell (sh/bash) is case-sensitive. The dir var is called deployDir (mixed-case) everywhere except for the mkdir command, where it is called deploydir (all lowercase). Since deploydir (all lowercase) is a considered distinct variable from deployDir (mixed-case) and deplydir (all lowercase) has never had a value assigned to it, the value of deploydir (all lowercase) is empty string ("").

Without the quotes (mkdir $deploydir), the line effectively becomes mkdir (just the command without the required operand), thus the error mkdir: missing operand.

With the quotes (mkdir "$deploydir"), the line effectively becomes mkdir "" (the command to make a directory with the illegal directory name of empty string), thus the error mkdir: cannot create directory'.

Using the form with quotes (mkdir "$deployDir") is recommended in case the target directory name includes spaces.

Assign output of mkdir command to variable

When you run the mkdir command by itself, look how much output it produces:

$ mkdir foo
$

None!

When you use a command substitution to generate the argument to mkdir, look how much extra output you get:

$ mkdir tmpbkp.`date +%F`
$

None!

When you put it inside $() it still produces no output.

There is a -v option for mkdir (in the GNU version at least) which produces some output, but it's probably not what you want.

You want the name of the directory in a variable? Put it in a variable first, then call mkdir.

$ thedir=tmpbkp.`date +%F`
$ mkdir $thedir
$ echo $thedir
tmpbkp.2017-04-06
$

How to mkdir only if a directory does not already exist?

Try mkdir -p:

mkdir -p foo

Note that this will also create any intermediate directories that don't exist; for instance,

mkdir -p foo/bar/baz

will create directories foo, foo/bar, and foo/bar/baz if they don't exist.

Some implementation like GNU mkdir include mkdir --parents as a more readable alias, but this is not specified in POSIX/Single Unix Specification and not available on many common platforms like macOS, various BSDs, and various commercial Unixes, so it should be avoided.

If you want an error when parent directories don't exist, and want to create the directory if it doesn't exist, then you can test for the existence of the directory first:

[ -d foo ] || mkdir foo

mkdir permission denied in shell script

In your script, problem is ~/test, the ~ is not getting expanded. So, if you will check again, you will find a ~ directory in the current-directory where you executed the script.

Here is an example where you expand ~ using eval:

$ cat dirtest.sh
#!/bin/bash
echo "Enter path: "
read my_dir
eval my_dir="$my_dir"
echo "Enter title: "
read title
mkdir -pv "${my_dir}/$title"

Trying compile with makefile but say mkdir command not found

The problem is you have reset the PATH variable inside your makefile:

PATH            =   mkdir $(@D)

Now when you run a shell, your $PATH environment variable contains the text mkdir foo and so the shell will dutifully try to find the programs you want to run in the directory named mkdir foo, just as it would try to find them in /usr/bin if you had PATH = /usr/bin.

Since there is no directory mkdir foo, the shell cannot find any programs that it wants to run, including mkdir.

You should not modify standard system environment variables inside your makefile. Maybe you want to use something like:

MKPATH          =   mkdir $(@D)

instead.



Related Topics



Leave a reply



Submit