android - shell脚本中的if []还有其他选项吗?

标签 android linux shell

Possible Duplicate:
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 在 if [ $C == 1 ] 方面存在一些问题它给了我像

这样的错误
[: not found

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

最佳答案

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

另一种选择是使用测试

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

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

关于android - shell脚本中的if []还有其他选项吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12066311/

相关文章:

linux - 如何在 shell 脚本中为结果集附加一列

linux - Bash 脚本 : difference in minutes between two times

android - 使用 Android SurfaceView 拍照不刷新使用 ICS 及以上

android - 覆盖库android中的资源

android - flutter中解析json数据时出现Null Error

java - 将字符串转换为对象名称。 IE。 R.string.对象名

python - epoll 中如何通知 connect() 失败?

c - 为什么 gcc 在其所有错误消息中都有 "â"?

linux - 如何将命令输出回显到 Linux 中现有行的末尾?

linux - 无法从另一个 shell 脚本执行脚本