tcsh if/then blocks always return true
A co-worker tells me that if/then blocks in csh and/or tcsh always return true.
Is that true? (no pun intended) |
post example
|
no, that is not true
|
And the standard statement in pan64's signature is the most apt answer here:
Quote:
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" |
I have not been able to prove that my co-worker is wrong.
To be clear, I am not referring to the conditional within an if/then block. I am referring to the entire block. After the 'endif', $? is always 0. I can have 'statements' within the if, or 'statements' within the else than can return false but after the endif the status is always true. |
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.
|
Quote:
Since this was just a question and not actually an issue to be resolved I'll mark this as solved. Cheers to all, |
Quote:
|
Quote:
Code:
#!/bin/tcsh 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 so it looks like the command if itself cannot fail. |
Quote:
Thanks to all for the feedback. Cheers, |
All times are GMT -5. The time now is 02:24 PM. |