CSH Can't Handle Leading Zeroes

Latest response

No action needed on this, but I thought RHEL users would want to know: Unlike Unix (e.g. Mac OS X), RHEL's implementation of CSH barfs on leading zeros when reading a number from a text file into a numeric CSH variable. For example, if a file contains the text "00123", then the following statement:

@ myNum = cat $data_dir/mynum.txt

will give you the number 123 if you are running the CSH file under Unix, but Red Hat will generate a "Badly formed number" error. You can make it work in Red Hat by trimming leading zeroes, like so:

@ myNum = cat $data_dir/mynum.txt | sed 's/^0*//'

(However, that might fail if the number is actually zero, so watch out for that.)

Responses

OK, the web form formatter ruined my examples. there are supposed to be backward accents around the other part. Trying again now:

@ myNum = cat $data_dir/mynum.txt

@ myNum = cat $data_dir/mynum.txt | sed 's/^0*//'

Nope, trying again:

@ myNum = `cat $data_dir/mynum.txt`

@ myNum = `cat $data_dir/mynum.txt | sed 's/^0*//'`

Yes, that time it worked.

As far as I can see, octal 0123 is converted to 83 decimal:

cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 6.6 (Santiago)
cat a
00123
@ z = `cat a`
echo $z
83

That's interesting. Is something telling your code to interpret the file "a" as octal numbers? What would happen if the file contained the ASCII (or UTF-8) string "00918" instead of "00123"? I am running 6.5, BTW.

A leading zero indicates a number in octal representation, refer to Wikipedia

Using 00918 leads to bad format:

$ cat a
00918
$ @ z = `cat a`
@: Badly formed number.

Siem, I have done some additional tests based on what you just told me, and I found out that you are indeed correct. This is what I now know:
* In RHEL, a number without leading zeroes will be interpreted as a decimal number.
* In RHEL, a number with any leading zeroes will be interpreted as an octal number, and will generate "Badly formed number" if the octal-inappropriate digits "8" and/or "9" are included in that number.
* In Unix (e.g. Mac OS X), the number will be interpreted as a decimal number regardless of the presence or absence of any number of leading zeroes.

I'm reminded of the old gem, CSH PROGRAMMING CONSIDERED HARMFUL

It seems you can't ask a question about CSH without someone thinking it helpful to respond, "don't use CSH." As if I had a choice. (Same with Red Hat, FYI.) But hey, it's all working now, so who's complaining? Have a good weekend. :)

Sorry, wasn't saying "don't use CSH", was just reminded of why I abandoned CSH a loooooong time ago. It was great as an interactive shell in the early 90s, but was always painful to use as a programming framework.

No prob! :) Plus I think I just found out that I'm actually using BASH. How about that.

Ah. Yes. BASH: it likes to emulate other shells' modes. You don't often notice that it's happening until you use some lesser-used functionality and the emulation isn't quite there (happened to me when I was transitioning from KSH).

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.