bash - 最后一个函数的 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
解决方案
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
推荐阅读
- python - sp-api - 卖家合作伙伴 api python
- javascript - 多选 get Selected options 按顺序选择
- ios - 使用 segues 从另一个 ViewController 更新 tableView
- javascript - Ajax 与 Javascript 将文本发送到 html
- c# - 如何在 ASP.NET MVC 中制作假视频源并使其不可下载
- c++ - 如何在 OpenGL 中更新窗口
- user-interface - 为什么我的 SwiftUI 列表分隔符没有正确排列?
- multithreading - 循环如何以块、扭曲和线程的形式映射到 GPU 中?
- javascript - 如何在反应中使用第三个 js 库?
- javascript - 我可以将按钮的文本与JS中的数组元素进行比较吗