【问题标题】:Any other option for if [] in shell script? [duplicate]shell脚本中if []的任何其他选项? [复制]
【发布时间】:2012-08-17 11:23:14
【问题描述】:

可能重复:
shell script in android gives [: not found

我需要在 shell 脚本中写一个条件语句。

我用过这种方式

#!/bin/sh
c=1
if [ $c == 1 ]
then
  echo c is 1
else
  echo c is 0
fi

这在 linux 机器上运行良好,但 android shell 有一些问题,如果 [ $C == 1 ] 它给了我类似的错误

[: not found

那么在 shell 脚本中是否还有其他条件语句,所以我可以用这种方式转换我的逻辑?

【问题讨论】:

  • 您已经询问过same question。请编辑或评论原始问题。

标签: android linux shell


【解决方案1】:

通常 [ 是 test 的别名,在 bash 中它是内置的。 type [ 看看它是什么。

另一种选择是使用测试

#!/bin/sh
c=1
if test "$c" = 1
then
  echo c is 1
else
  echo c is 0
fi

= 用于字符串比较,-eq 用于数字比较

【讨论】:

  • android 没有测试命令..!!!!所以现在不可能
  • 我将交叉编译测试命令,然后我会尝试 [] 是否应该工作
猜你喜欢
  • 2011-08-01
  • 2014-06-25
  • 2016-05-27
  • 1970-01-01
  • 2023-03-09
  • 2015-07-22
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
相关资源
最近更新 更多