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 Code:
if ( -x `which nonexisting` ) then 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 Code:
if ( `which nonexisting | cut -c1` == '/' ) then Code:
which hostname 2> /dev/null || echo "error" Code:
which nonexisting 2> /dev/null || echo 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. |
What version are you using? In my tcsh
Code:
if(-x `which $1`) then |
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.
|
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. |
Quote:
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. |