【发布时间】:2013-07-27 17:18:33
【问题描述】:
我正在尝试执行一个脚本来检查 CA 电源状态
这是我的代码:
#!/bin/bash
a=$(acpitool -a)
echo "$a"
if $a -eq "AC adapter : online"
then
echo "ONLINE"
else
echo "OFFLINE"
fi
它不工作;变量$a 不能与字符串“AC 适配器:在线”进行比较。如何将命令acpitool -a的输出转换为字符串?
会发生这样的事情:
AC adapter : online
./acpower.sh: linha 7: AC: comando não encontrado
OFFLINE
问题解决了!
这是新代码,感谢大家的帮助。
#!/bin/bash
# set the variable
a=$(acpitool -a)
# remove white spaces
a=${a// /}
# echo for test
echo $a
# compare the result
if [[ "$a" == 'ACadapter:online' ]]
then
# then that the imagination is the limit
echo "ONLINE"
else
# else if you have no imagination you're lost
echo "OFFLINE"
fi
此代码可以在服务器上使用,以在电源故障时发出警报!
【问题讨论】:
-
比较的时候把$a放在引号里怎么样?比如:if "$a" -eq "AC adapter : online"?
-
欢迎来到 Stack Overflow。请尽快阅读About 页面。你在你的问题上做得很好;你展示了代码,你解释了你想要什么以及你得到了什么错误——这些都是很好的技术。