首页 > 解决方案 > 最后一个函数的 Bash 测试值等于零

问题描述

使用 blueutil 尝试连接或断开我的耳机,当您使用 --is-connected 进行测试时,它返回 0 或 1。我想用它来连接或断开耳机,但我不断收到“0:找不到命令”错误。

blueutil --is-connected ac-90-85-3e-0d-04
if $? -eq 0
  then
    blueutil --connect ac-90-85-3e-0d-04
  else
    blueutil --disconnect ac-90-85-3e-0d-04
fi

标签: bash

解决方案


shell 本身不进行比较;这就是test命令的工作:

if test $? -eq 0

尽管if所做的只是查看其条件列表的退出状态;您可以blueutil直接从该列表中调用。

if blueutil --is-connected ac-90-85-3e-0d-04
  then
    blueutil --connect ac-90-85-3e-0d-04
  else
    blueutil --disconnect ac-90-85-3e-0d-04
fi

推荐阅读