PHP的变量也不是随意能转化的及bash的算术运算 (PHP心得)
发布于 2008-12-30 20:47 阅读:12,094 评论:0 标签: array bash scalar

      一直以为PHP的变量是很灵活的,类型想怎么变换就怎么变换。

      但实际上并不是这样,当一个变量被赋值为数字时,是变换不了数组的,比如:

以下是代码片段:

[root@localhost shengting]# cat test.php
<?php
$a = 454;
$b = 6;
$a['gfgo'] = $b;

var_dump($a);
?>

[root@localhost shengting]# php test.php
PHP Warning:  Cannot use a scalar value as an array in /usr/home/shengting/test.php on line 4

Warning: Cannot use a scalar value as an array in /usr/home/shengting/test.php on line 4
int(454)

      从var_dump的结果看,不仅有Warning级的错误,赋值也没有成功。而,如果变量一开始是字符串的话,就没这回事情了:

以下是引用片段:

[root@localhost shengting]# cat test.php
<?php
$a = `454`;
$b = 6;
$a['gfgo'] = $b;

var_dump($a);
?>
[root@localhost shengting]# php test.php
sh: 454: command not found
array(1) {
  ["gfgo"]=>
  int(6)
}

      可以看出,赋值是成功的。

      应该,这个是和存储的类型是有联系的。同样,在Shell的Bash中,所有的变量的值都是以字符串方式存储的。尽管这个特性使得bash的数据处理起来很容易,但是也使得数字运算比较困难。原因就是数字也是以字符串的形式存储的,

      在Bash中,如果要对数字进行算术运算和逻辑操作,必须先转化为整数(可视为中间值),得到运算结果后再转化为字符串,以便正确的保存在Bash变量中。

      Bash提供了三种方式的数字数据进行算术变量:

以下是引用片段:

1:使用let命令。
2:使用shell扩展 $((expression))
3:使用expr命令

      具体用法就不说了,哈哈。偶比较喜欢使用第二种。

展开全文  
收起全文  
Shell进制运算错误:08: value too great for base (Linux/Unix)
发布于 2008-12-19 12:18 阅读:33,574 评论:0 标签: bash shell 进制

      昨天发现一个很奇怪的bug,一个分析log的程序,一个小时产生一个log文件,文件名中含有当时的小时数以作标识,其中小时为有前导零的24 小时格式。bug是第08和09小时的log为空,甚为诧异。

      分析程序后,定位于08和09这两个数字上。程序中有一步需要对小时数进行数学运算,问题就在于此,例如:

以下是代码片段:
[root@login yayu]# echo $((08 -2))
bash: 08: value too great for base (error token is "08")

      Google了一下找到了答案:原文点这![建议新窗口打开]

      原因:

以下是代码片段:
Numbers starting with leading 0 are Octal numbers  (base 8) in many programming
languages including C, Perl and shell. Valid octal digits are
0,1,2,3,4,5,6,7 so it barfs if it sees an 8 or a 9. You probably want
to work in straight numbers and make a leading 0 in your output
format with a sprintf("%02d") kind of formatting thing.
Anything starting with 0x or 0X is a hex number.

So the error message means exactly as it says- it's an error from
the let function complaining about the value being too big for the base.

Have fun,
Stuart.

      解决方案:

以下是代码片段:
You can explicitly state the base of a number using base#number
Code:
if [ $((10#$item)) -eq 0 ] ; then
That will have trouble if the number starts with a minus sign.
The '-' needs to be in front of the base like -10#009 for -9.

      原来是进制的问题,C, Perl 和 shell中以0开头的数字是八进制了,而在运算中也是严格如此,不会做自动转化,于是就报错了。如下解决:

以下是引用片段:
[root@login shengting]# echo $((10#08 -2))
6

      纠结问题的根源,还是我的shell写得太烂了......如果命令用得好就不会遇到这个bug了,不过写得好了,又怎么会在以后避免进制导致的问题呢?到底是先有鸡还是先有蛋呢?

展开全文  
收起全文