LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2016, 12:37 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
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)
 
Old 04-14-2016, 12:33 AM   #2
lazardo
Member
 
Registered: Feb 2010
Location: SD Bay Area
Posts: 270

Rep: Reputation: Disabled
post example
 
Old 04-14-2016, 02:09 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
no, that is not true
 
1 members found this post helpful.
Old 04-14-2016, 06:09 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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"

Last edited by rtmistler; 04-14-2016 at 06:14 AM.
 
Old 04-14-2016, 08:50 AM   #5
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
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.
 
Old 04-14-2016, 08:53 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 04-14-2016, 11:19 AM   #7
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by rtmistler View Post
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,
 
Old 04-14-2016, 12:20 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by rtmistler View Post
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.
 
1 members found this post helpful.
Old 04-16-2016, 06:02 PM   #9
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by ntubski View Post
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.

Last edited by sharky; 04-17-2016 at 12:08 AM.
 
Old 04-17-2016, 02:25 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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.
 
Old 04-17-2016, 12:00 PM   #11
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by pan64 View Post
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,
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
find command return true if a file was found catalint Programming 12 05-19-2011 06:52 AM
dpkg return error :post installation script return an error code (1) grimfold Debian 2 09-10-2009 01:55 PM
Return true or false if I have ping reply Menestrel Programming 4 11-29-2004 12:40 AM
why does if [ ${var:0:2}='AA' ] always return true blish_blash Programming 1 12-31-2003 08:51 AM
tcsh hhegab Linux - Newbie 1 07-30-2002 05:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:58 AM.

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