check if a directory exists if not make it

Hey guys im trying to check if the directory exists
i get a syntax error on the elif statement iv tried using else and still same result im not sure. If the directory does not exist can i just insert mkdir /tmp/old under the elif once that part gets working

if [ -d $/tmp/old ]; then
#do nothing
elif
echo "Error: '$check' does not exist!!"
echo "EXITING"
exit 1
fi
 

i think you need a then after elif...

this will check for file and dir exisistance..

is there a way to check just for directory rather then file?

thanks for your fast reply

exclude the first if statement..

iv done

[code]
if [-d /tmp/old]
then
#do nothing
elif
echo "directory exists"
exit 1
fi

i get some weird errors all about syntax where am i going wrong

please put then after elif also...

i get syntax error near unexpected token then

thats because there is no test condition with elif..

ok try this..

that still does not work for some reason i get more syntax errors with the then

post the errors..

ok that works thanks very much

oh great... :cool:

[quote=musicmancanora;302228340]
iv done

if [ -d /tmp/old ]
then
#do nothing
elif
echo "directory exists"
exit 1
fi

i get some weird errors all about syntax where am i going  wrong
Replace elif by else.
A comment is not evaluated as an statement, she shell will signal a syntax error on the else line. A null statement is specified with ':' :if [ -d /tmp/old ]
then
   : #do nothing
else
   echo "directory exists"
   exit 1
fi

You can also invert your test :

if [! -d /tmp/old]
then
   echo "directory exists"
   exit 1
fi

Jean-Pierre.

If i do that i get syntax error unexpected token 'then'

ahh this is driving me nuts it was working before!

if [ -d /tmp/old ]
then
echo "DIR exist"
else
echo "Error: '$check' does not exist!!"
echo "EXITING"
exit 1
fi 

[ -d /tmp/old ] || echo "not found"

Sorry, There are missing spaces in the if tests :

if [ -d /tmp/old ]
then
   : #do nothing
else
   echo "directory does not exist"
   exit 1
fi
if [ ! -d /tmp/old ]
then
   echo "directory does not exist"
   exit 1
fi

Jean-Pierre.

"If not directory /tmp/old then echo directory exists"? I think you got that backwards.

If the topic title of this thread is any indication then mkdir -p /tmp/old is all that's needed. If the directory exists, nothing happens. If it doesn't exist, it's created.

mkdir /tmp/old 2> /dev/null

if is there. it will do nothing
if its not there. it will creat it.

if [ -d /tmp/old ];
then
echo "DIR exist"
else
echo "Error: '$check' does not exist!!"
echo "EXITING"
exit 1
fi

Above will work try now