Pages

jeudi 1 août 2013

@: Badly formed number, octal problem in shell

In a csh script, I have :

set a = "08"
printf "%03i" $a
printf: 008: value not completely converted


set c = "08"
@ c ++
@: Badly formed number.

This happens when the variable a is equal to 018,..., 098, as well as 09, 019 .... 099.
This is because: 1) while shell treats numbers starting with 0 ("08") as octal, but not as integer number; 2) 8 and 9 do not exist in octal; 3) my tcsh version: tcsh --version is tcsh 6.17.00 (Astron) . (In fact in an older version tcsh 6.13.00 (Astron), the same error does not happen !)

The solution is to remove the leading 0s from the variable before we apply the commands such as printf or @. for example:

set a = "008"
set a  =  `echo $a   |   sed   's/^0*//g`
@ a ++







Aucun commentaire:

Enregistrer un commentaire