LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   tcsh if/then blocks always return true (https://www.linuxquestions.org/questions/programming-9/tcsh-if-then-blocks-always-return-true-4175577377/)

sharky 04-13-2016 12:37 PM

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)

lazardo 04-14-2016 12:33 AM

post example

pan64 04-14-2016 02:09 AM

no, that is not true

rtmistler 04-14-2016 06:09 AM

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"

sharky 04-14-2016 08:50 AM

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.

rtmistler 04-14-2016 08:53 AM

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.

sharky 04-14-2016 11:19 AM

Quote:

Originally Posted by rtmistler (Post 5531014)
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.

Cheers to all,

ntubski 04-14-2016 12:20 PM

Quote:

Originally Posted by rtmistler (Post 5531014)
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.

sharky 04-16-2016 06:02 PM

Quote:

Originally Posted by ntubski (Post 5531115)
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.

pan64 04-17-2016 02:25 AM

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.

sharky 04-17-2016 12:00 PM

Quote:

Originally Posted by pan64 (Post 5532340)
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.

Thanks to all for the feedback.
Cheers,


All times are GMT -5. The time now is 02:24 PM.