如何解决问题
shell(或测试命令)使用=表示字符串相等,使用-eq表示数字相等.某些版本的shell支持==作为=的同义词(但是=由POSIX test命令定义).相比之下,Perl使用==表示数字相等,eq表示字符串相等.
您还需要使用以下测试命令之一:
if [ "$a" = "AC adapter : online" ]
then echo "ONLINE"
else echo "OFFLINE"
fi
要么:
if [[ "$a" = "AC adapter : online" ]]
then echo "ONLINE"
else echo "OFFLINE"
fi
使用[[运算符,您可以将引号放在“$a”附近.
为什么你收到错误信息
你写的时候:
if $a -eq "AC adapter : online"
shell将其扩展为:
if AC adapter : online -eq "AC adapter : online"
这是一个执行命令AC的请求,显示5个参数,并将命令的退出状态与0进行比较(考虑0 – 成功 – 为真,任何非零为假).显然,您的系统上没有名为AC的命令(这并不奇怪).
这意味着你可以写:
if grep -q -e 'some.complex*pattern' $files
then echo The pattern was found in some of the files
else echo The pattern was not found in any of the files
fi
如果要测试字符串,则必须使用test命令或[[…]]运算符. test命令与[命令相同,但命令名称为[,最后一个参数必须为]时除外).