LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-30-2007, 07:10 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
tcsh: how do you get the exit status of a program?


from the manual:
Quote:
Command exit status
Commands can be executed in expressions and their exit status returned by enclosing them in
braces ('{}'). Remember that the braces should be separated from the words of the command by
spaces. Command executions succeed, returning true, i.e., '1', if the command exits with status
0, otherwise they fail, returning false, i.e., '0'. If more detailed status information is
required then the command should be executed outside of an expression and the status shell vari-
able examined.
... but none of these work:

Code:
{ls}
{ ls }
'{ ls }'
am I reading the manual wrong?
 
Old 10-30-2007, 07:54 PM   #2
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
I use BASH in my civilian life, and I am not a tcsh-shelll specialist, but I tried following.
1. /bin/tcsh
It brought me to the C-shell
2. ls
3. echo $?
It returned 0, which means good command
4. kmya
This non existent command.
5. echo $?
It returned 1.

I didn't use any brackets, maybe it worthwhile to try. BTW: in BASH if you use command in braces, such as ( ls ), it will 'fork' it out as a separate process.
 
Old 10-30-2007, 08:10 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by rsashok View Post
I use BASH in my civilian life, and I am not a tcsh-shelll specialist, but I tried following.
Looking at the status var (I believe that's what $? is called, right?) does give me the exist status, but it does it after the exit, at which point stderr has been filled with [useful] garbage.

what I was hoping to accomplish was to stop a failing command from running before it actually runs by testing its exit status first, then running the command if the exit status is 0.
 
Old 10-31-2007, 07:08 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 948

Rep: Reputation: 216Reputation: 216Reputation: 216
Quote:
... running before it actually runs ...
Hows that?

Anyway, sometimes it may be required, particularly before you commit any specific action.

Assuming that you can set the exit status in your program (if it isn't a standard command), then:

myCommand [args ..]
a=$?
if [$a -eq "0" ] then
myCommand [fresh set of args maybe ...]
fi

end
 
Old 10-31-2007, 12:59 PM   #5
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by AnanthaP View Post
Hows that?
... sorry "test" before it actually runs...

the problem is I have a script that looks for a glob of files & does things with them. Sometimes the glob has no matches. When it has no matches, an error is reported (even if you "set nonomatch"). This is a problem when the script is run in a cron job because then you get mail every time the glob matches nothing... and this is the behavior I'm trying to avoid.

Currently, I just dump all output from the script to /dev/null which solves the problem, but sometimes I want the script to error out on other things... just not the glob - hence my wanting to test the glob before using with something like

Code:
if ( {ls *foo} == 0 ) then
 do something with *foo
endif
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Exit status vedanu Programming 4 06-23-2007 06:13 PM
get exit status from a C-program in Bash Yoko Programming 1 07-12-2005 05:33 PM
exit status naflan Programming 2 10-22-2004 01:22 PM
exit status 17 kppp joshy Mandriva 3 10-08-2004 08:07 AM
error exit status 30 debian_dummy Debian 2 09-21-2004 11:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:17 PM.

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
Open Source Consulting | Domain Registration