Error code checking

I'm trying to create a directory from my Perl script. Only if the there was an error I want to let the user know about it. So if the folder exists is ok.

This is what I think should work:

`mkdir log 2>/dev/null`;
if($? == 0 || $? == errorCodeForFileExists)
{ everyting is fine }
else
{ print "Error creating log directory. Check you file permissions\n";
exit(0) }

The problem is that I'm getting the same error code for both File exists and Permission Denied.

Any ideas?

I am not clear about your requirement, but i guess this is what you are looking for.


mkdir foldername
if [[ -d foldername ]]; then

echo "The folder Exists and everything is fine"
else

echo "ERROR"
fi

Sorry if I wasn't clear.
I need to create a folder and if there is any errors like permission errors I want to let the user know about it. However I don't wish to overwrite the folder if it already exists. Also I need to do it inside a Perl script.

This is the workaround I came up with:

`ls log 2> /dev/null`; #check if the folder exists
if($?) { #if it doesn't create it
`mkdir log`;
$dirFlag = 1; #flag to know that dir was created
}

The bad thing about this is that it wont let the user know if there were any permission errors. In other words, if the folder wasn't created for some reason the program will crash later when I try to write to that folder.

So what I want to do is to somehow check, in my Perl code, the error code that UNIX spits out. The problem is that I'm getting the same error code number for both "File Permission" and "File exists" errors.

I just found a tutorial on manipulating files and directories and I got my answers. Thanks.