tcsh: how do you get the exit status of a program?
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.