LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Error handling in csh/tcsh (https://www.linuxquestions.org/questions/programming-9/error-handling-in-csh-tcsh-814821/)

lucmove 06-17-2010 06:22 PM

Error handling in csh/tcsh
 
I want to check the hostname of my box. Maybe there are better ways to do it, but here is my attempt. Even if I eventually do it some other way, I still think I need to learn how to cope with the errors:

Code:

if  ( -x `which hostname` ) then
        setenv HOSTNAME `hostname`
else if  ( -e /etc/HOSTNAME ) then
        setenv HOSTNAME `cat /etc/HOSTNAME`
else if    ( -e /etc/hostname ) then
        setenv HOSTNAME `cat /etc/hostname`
endif

That has been working fine because the 'hostname' application has always been present. But what if it isn't? So I look for 'nonexisting' to simulate the error condition:

Code:

if  ( -x `which nonexisting` ) then
        setenv HOSTNAME `hostname`
else if  ( -e /etc/HOSTNAME ) then
        setenv HOSTNAME `cat /etc/HOSTNAME`
else if    ( -e /etc/hostname ) then
        setenv HOSTNAME `cat /etc/hostname`
endif

As a whole, that code still works in my test machine because it's Linux and I have /etc/hostname. But I don't think that BSD machines have it.

Either way, since there is no 'nonexisting' executable in my PATH, my shell starts with a line that says "Exit 1". It's the error message of the 'which' built-in available in tcsh. I want to get rid of that error message. That's the whole point of this post. I want to make 'which' exit with 0 or redirect stderr and get cleaner output. My clumsy attempts:

Code:

if  ( `which hostname | cut -c1` == '/' ) then
        echo found
else
        echo notfound
endif

Prints "found". Great.

Code:

if  ( `which nonexisting | cut -c1` == '/' ) then
        echo found
else
        echo notfound
endif

Prints "notfound" as expected, but first it prints "Exit 1". Not what I want. I want to get rid of the error. Another try:

Code:

which hostname 2> /dev/null || echo "error"
Prints "error".

Code:

which nonexisting 2> /dev/null || echo error
Prints "error".

Dammit. Now I don't see stderr anymore, but whether the executable exists or not, 'which' returns some kind of error. How am I supposed to test the output of 'which'? Please advise.

TIA.

hda7 06-18-2010 08:01 AM

What version are you using? In my tcsh
Code:

if(-x `which $1`) then
    echo Y
else
    echo N
endif

prints Y for "./test.csh ls" and N for "./test.csh notanywhere" I'm not getting an Exit 1 error message.

hda7 06-18-2010 08:07 AM

Just thought of this: run your script as tcsh, not as csh. which writes errors to stdout in tcsh mode but to stderr in csh mode.

lucmove 06-18-2010 09:54 AM

tcsh --version

tcsh 6.17.00 (Astron) 2009-07-10 (i486-intel-linux) options wide,nls,dl,al,kan,rh,color,filec

I don't quite understand what you mean by that switching mode idea, but I don't think that will be possible anyway. That script happens to be my .tcshrc file. Unless I am wrong, of course. I don't have a lot of experience with tcsh.

hda7 06-18-2010 07:07 PM

Quote:

Originally Posted by lucmove (Post 4007617)
tcsh --version

tcsh 6.17.00 (Astron) 2009-07-10 (i486-intel-linux) options wide,nls,dl,al,kan,rh,color,filec

I don't quite understand what you mean by that switching mode idea, but I don't think that will be possible anyway. That script happens to be my .tcshrc file. Unless I am wrong, of course. I don't have a lot of experience with tcsh.

I have version "tcsh 6.14.07 (Astron) 2006-08-25 (i386-sun-solaris) options wide,nls,dl,al,kan,rh,color,filec" I tried putting my code in my .tcshrc file and didn't have any problems. Maybe it's version specific.
That mode idea ment to say #!/path/to/tcsh instead of #!/path/to/csh, but of course your .tcshrc file should always be read in tcsh mode.


All times are GMT -5. The time now is 01:36 AM.