Frank IJskes wrote:
> -0-diasrv03:~$ mkdir -p /a/b/c/d
> -0-diasrv03:~$ mkdir -p /a/b/c/d
> -0-diasrv03:~$ mkdir /a/b/c/d
> mkdir: cannot create directory `/a/b/c/d': File exists
> -1-diasrv03:~$
>
> I would like to test in a script if anything goes wrong creating this
> directory structure, for this I need to have an error returned.
It does. Try this:
touch /tmp/afile
mkdir -p /tmp/afile
mkdir: `/tmp/afile' exists but is not a directory
echo $?
1
touch /tmp/afile
mkdir -p /tmp/afile/adir/bdir
mkdir: `/tmp/afile' exists but is not a directory
echo $?
1
If mkdir -p fails then an error is returned.
The mkdir -p option ensures that the directory exists. A preexisting
directory is not an error, as documented.
Using mkdir without an option requires mkdir to either create the
directory or to return an error, as shown in your test case. Without
any options mkdir fails if it cannot create the directory such as
because the directory already exists.
If you require that your process is the creator of the directory then
you will need to make each directory individually without the -p
option or to ensure that the directory does not exist prior to the
mkdir -p such as by removing the targets first.
touch /tmp/a
rm -rf /tmp/a || { echo Error 1>&2 ; exit 1 ;}
mkdir -p /tmp/a/b/c || { echo Error 1>&2 ; exit 1 ;}
echo all good
Bob
--
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]