LinuxQuestions.org
Help answer threads with 0 replies.
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 10-11-2019, 02:10 AM   #1
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Rep: Reputation: 16
Tcsh - if with '0' number


Hi,

I'm writing in tcsh and try to understand why if can't handle number with '0' in the beginning?

Code:
if ( 09 > 24 ) then...
 
Old 10-11-2019, 02:39 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,862

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
why? Can you give us a better example?
 
Old 10-11-2019, 02:40 AM   #3
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,341

Rep: Reputation: Disabled
Seems to work fine here. This:
Code:
if ( 09 > 24 ) then
  echo First number is greater
else
  echo Second number is greater
endif
...returns "Second number is greater", while this:
Code:
if ( 24 > 09 ) then
  echo First number is greater
else
  echo Second number is greater
endif
..returns "First number is greater".

I also tried comparing the numbers "9" and "010" to rule out the possibility that numbers with leading zeros might be interpreted as octal, which is what bash would do.
 
1 members found this post helpful.
Old 10-11-2019, 02:43 AM   #4
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Ser Olmy View Post
Seems to work fine here. This:
Code:
if ( 09 > 24 ) then
  echo First number is greater
else
  echo Second number is greater
endif
...returns "Second number is greater", while this:
Code:
if ( 24 > 09 ) then
  echo First number is greater
else
  echo Second number is greater
endif
..returns "First number is greater".

I also tried comparing the numbers "9" and "010" to rule out the possibility that numbers with leading zeros might be interpreted as octal, which is what bash would do.

while running in terminal I get the below error:
Code:
if: Badly formed number.
 
Old 10-11-2019, 02:52 AM   #5
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,341

Rep: Reputation: Disabled
What does tcsh --version report on your system? And just out of curiosity, does is still complain if only digits 0-7 are used ("if ( 07 > 24 ) then ...")?

If indeed your version of tsch can't handle leading zeroes, it's not a gargantuan task to strip them out before the comparison.

Edit: It seems tcsh versions between 6.14 and 6.17.01 did indeed treat numbers with leading zeroes as octal, just like bash. This broke a few scripts, and the change was rolled back in 6.17.02.

Last edited by Ser Olmy; 10-11-2019 at 03:01 AM.
 
2 members found this post helpful.
Old 10-11-2019, 03:12 AM   #6
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Ser Olmy View Post
What does tcsh --version report on your system? And just out of curiosity, does is still complain if only digits 0-7 are used ("if ( 07 > 24 ) then ...")?

If indeed your version of tsch can't handle leading zeroes, it's not a gargantuan task to strip them out before the comparison.

Edit: It seems tcsh versions between 6.14 and 6.17.01 did indeed treat numbers with leading zeroes as octal, just like bash. This broke a few scripts, and the change was rolled back in 6.17.02.

Version: tcsh 6.17.00
and 07 instead of 09 work for me.
I guess it's octal number, no?
 
Old 10-11-2019, 03:14 AM   #7
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,341

Rep: Reputation: Disabled
Quote:
Originally Posted by DoME69 View Post
Version: tcsh 6.17.00
and 07 instead of 09 work for me.
I guess it's octal number, no?
Indeed. You need to strip leading zeroes before doing the comparison. That'll work in any version of tsch.
 
2 members found this post helpful.
Old 10-11-2019, 03:39 AM   #8
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
Thanks @Ser Olmy
 
  


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
Identify and explain the major number, minor number, and revision number in Linux... turbomen Linux - Newbie 1 11-16-2010 02:48 AM
gawk/awk/tcsh - how to convert large number to human readable (Mb,Gb, etc)? BrianK Programming 23 10-30-2010 04:20 AM
How to find the TCSH shell version number kailash19 Other *NIX 6 04-09-2010 04:07 AM
How to find the TCSH shell and gzip version number? kailash19 Linux - Newbie 5 04-07-2010 06:42 AM
How to set tcsh prompt: # for root and % for user? robson Linux - Newbie 1 10-12-2003 03:13 PM

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

All times are GMT -5. The time now is 09:25 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