LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2010, 07:22 PM   #1
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian Jessie
Posts: 809

Rep: Reputation: 76
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.
 
Old 06-18-2010, 09:01 AM   #2
hda7
Member
 
Registered: May 2009
Distribution: Debian wheezy
Posts: 252

Rep: Reputation: 31
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.
 
Old 06-18-2010, 09:07 AM   #3
hda7
Member
 
Registered: May 2009
Distribution: Debian wheezy
Posts: 252

Rep: Reputation: 31
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.
 
Old 06-18-2010, 10:54 AM   #4
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian Jessie
Posts: 809

Original Poster
Rep: Reputation: 76
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.
 
Old 06-18-2010, 08:07 PM   #5
hda7
Member
 
Registered: May 2009
Distribution: Debian wheezy
Posts: 252

Rep: Reputation: 31
Quote:
Originally Posted by lucmove View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripts run fine on *NIX/csh, but fail on Linux/tcsh? b1f30 Programming 3 12-09-2008 09:44 AM
Error message on opening up a csh terminal in Fedora 9 Unknown colorls variable `ca'. mrground Linux - Software 3 08-01-2008 12:37 PM
shell confusion..what is diff between bash, ksh, csh, tcsh..?? servnov Linux - Newbie 7 11-18-2004 09:28 PM
looking for csh or tcsh help 1ernie Linux - Software 1 04-12-2004 06:43 PM
tcsh/csh problem kicker Linux - Newbie 0 05-01-2002 04:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration