ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
And the standard statement in pan64's signature is the most apt answer here:
Quote:
A program will never do what you wish but what was implemented!
Your coworker has written bad code. Are you not capable of writing your own if-then statements using csh and/or tcsh and proving to them they are an idiot misinformed?
EDIT: Sorry for the implied challenge/insult. I really think you should try to prove that on your own, because it will teach you something, and teach you something definitive about that particular syntax for that particular shell, and honestly, when you've proven it beyond a shadow of a doubt in a script, I think you'll feel much more pleased with the knowledge and also have a fully confident and fully versed answer to provide to your coworker, however well or poorly that possible future exchange may go if/when you discuss it with them.
EDIT2: At the risk of self-promoting, follow the embodiment of the cheesy movie statement in my signature too. Also somewhat applicable, "do not give up on something merely because you don't know about a lot about it"
That's the status value, not the result of the if-else-endif statement. When performing an if test, you do not use $?. In that means, your coworker is likely correct, because a syntactically correct if statement will always result in a status value of 0, to imply that the last operation was a success. That has no relevance on the actual if test.
That's the status value, not the result of the if-else-endif statement. When performing an if test, you do not use $?. In that means, your coworker is likely correct, because a syntactically correct if statement will always result in a status value of 0, to imply that the last operation was a success. That has no relevance on the actual if test.
You're probably right regarding the relevance to the actual test. The topic came up between me and my co-worker in the context of regression test and how best to determine pass or failure of steps within a test or test within the overall regression.
Since this was just a question and not actually an issue to be resolved I'll mark this as solved.
That's the status value, not the result of the if-else-endif statement. When performing an if test, you do not use $?. In that means, your coworker is likely correct, because a syntactically correct if statement will always result in a status value of 0, to imply that the last operation was a success. That has no relevance on the actual if test.
In bourne-shell the last statement executed inside the if statement determines the status value of the whole thing, is it different on (t)csh? Another reason not to use to csh, I guess.
In bourne-shell the last statement executed inside the if statement determines the status value of the whole thing, is it different on (t)csh? Another reason not to use to csh, I guess.
Try this;
Code:
#!/bin/tcsh
if ( 1 == 2 ) then
echo "true"
else
ls -3
endif
echo $?
The ls -3 command should return an error but the $? status after the endif will be 0.
I much prefer bash over tcsh. As a matter of fact tcsh is my least favorite of the shells I've worked in.
Unfortunately, at my current job there is no choice. The engineering compute team mandates tcsh as the default shell for all users and there's tons of custom infrastructure built around tcsh that we have to deal with. So we deal with it.
in that case you need to save the exit code inside:
Code:
#!/bin/tcsh
if ( 1 == 2 ) then
echo "true"
RC=$?
else
ls -3
RC=$?
endif
echo $RC
based on the man page of tcsh (see status): Builtin commands which fail return exit status '1', all other builtin commands return status '0'.
so it looks like the command if itself cannot fail.
in that case you need to save the exit code inside:
Code:
#!/bin/tcsh
if ( 1 == 2 ) then
echo "true"
RC=$?
else
ls -3
RC=$?
endif
echo $RC
based on the man page of tcsh (see status): Builtin commands which fail return exit status '1', all other builtin commands return status '0'.
so it looks like the command if itself cannot fail.
Yes. That is generally what we do. It's actually not a problem at all. I just found it curious that if/else/then/endif did not catch errors. It kind of makes sense in a way. I guess.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.