[: : integer expression expected

简介:

错误如下,但是传给此脚本值时,能够正常运行,而不报错!

 
  1. ./classid.sh  
  2. ./classid.sh: line 8: [: : integer expression expected 
  3. ./classid.sh: line 13: [: : integer expression expected 
  4. ./classid.sh: line 17: [: : integer expression expected
  5.  
  6. ./classid.sh 6 2
  7. classid 1266


脚本如下

 
  1. lie=253 
  2. a=$1 
  3. b=$2 
  4.  
  5. if [ "$a" -ge "9" -a "$a" -le "9" ];then 
  6.    classid=$(((a-1)*lie+(b-1))) 
  7.    echo "classid $classid" 
  8. fi

 

google

test快捷方式[ ],改成使用"[[]]",逻辑运算符  "-a" 改成 "&&" 即可!

 
  1. if [[ "a" -ge "1" && "$a" -le "9" ]];then 
  2.    classid=$(((a-1)*lie+(b-1))) 
  3.    echo "classid $classid" 
  4. fi 

总结

1 所有字符 与逻辑运算符直接用“空格”分开,不能连到一起。

2 [[]] 运算符只是[]运算符的扩充。能够支持<,>符号运算不需要转义符,它还是以字符串比较大小。里面支持逻辑运算符:|| &&

3 另一种情况

 
  1. Shell脚本里,在用 test 的参数作比较的时候,如“ [ $temp -gt "100" ]”,
  2. 可能会出现的“ integer expression expected”错误。这是因为test 的这几个参数:
  3.  -gt ,-lt,-eq 比较的都是数字,因此变量temp如果是一个字符串的话就会出现上述错误了
  4. 将原句改为“ [ $temp -gt 100 ]” 结果也是一样的。  

更多参考这里


本文转自 dongnan 51CTO博客,原文链接:http://blog.51cto.com/dngood/675325


相关文章
|
4月前
|
Python
完美解决丨2. `TypeError: list indices must be integers or slices, not str`
完美解决丨2. `TypeError: list indices must be integers or slices, not str`
ValueError: not enough values to unpack (expected 3, got 2)
这个错误通常是因为在解包(unpacking)元组(tuple)时,元组中的元素数量与期望不符,导致无法将所有元素正确解包。 例如,在以下代码中,元组中只有两个元素,但我们尝试将其解包为三个变量:
352 0
|
机器学习/深度学习 PyTorch 算法框架/工具
解决Pytorch中RuntimeError: expected scalar type Double but found Float
解决Pytorch中RuntimeError: expected scalar type Double but found Float
2430 0
|
Python
解决only integer scalar arrays can be converted to a scalar index
解决only integer scalar arrays can be converted to a scalar index
289 0
解决only integer scalar arrays can be converted to a scalar index
|
Python
解决ValueError: `validation_split` is only supported for Tensors or NumPy arrays, found following
解决ValueError: `validation_split` is only supported for Tensors or NumPy arrays, found following
554 0
解决ValueError: `validation_split` is only supported for Tensors or NumPy arrays, found following
TypeError: sequence item 0: expected string, int found
TypeError: sequence item 0: expected string, int found
|
Linux Python
ValueError: empty range for randrange() (0, 0, 0)
ValueError: empty range for randrange() (0, 0, 0)