Error code checking | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Error code checking

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-20-2007
jepombar jepombar is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 24 August 2007, 4:18 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Sponsored Links
    #2  
Old 08-21-2007
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 14 September 2014, 10:08 PM EDT
Location: Bangalore
Posts: 288
Thanks: 8
Thanked 1 Time in 1 Post
I am not clear about your requirement, but i guess this is what you are looking for.


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

echo "The folder Exists and everything is fine"
else

echo "ERROR"
fi

Sponsored Links
    #3  
Old 08-21-2007
jepombar jepombar is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 24 August 2007, 4:18 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
I'm doing it in perl

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.
    #4  
Old 08-21-2007
jepombar jepombar is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 24 August 2007, 4:18 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Got it

I just found a tutorial on manipulating files and directories and I got my answers. Thanks.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
code checking bshell_1214 Shell Programming and Scripting 3 09-23-2011 05:05 AM
Error checking help. Boltftw Shell Programming and Scripting 1 04-19-2011 01:09 AM
Error Checking gregarion Programming 1 02-10-2010 05:31 PM
checking the return code Satyak Shell Programming and Scripting 4 10-16-2008 10:39 AM
Need help with error checking xianoth Shell Programming and Scripting 0 08-31-2007 11:47 AM



All times are GMT -4. The time now is 07:46 PM.