Linux 将输出转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17900485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert an output to string
提问by Hedron Dantas
I'm trying do to a script to check the CA power status
我正在尝试使用脚本来检查 CA 电源状态
this is my code:
这是我的代码:
#!/bin/bash
a=$(acpitool -a)
echo "$a"
if $a -eq "AC adapter : online"
then
echo "ONLINE"
else
echo "OFFLINE"
fi
It's not working; the variable $a
ia not compare with the the string "AC adapter : online". How to convert the output of command acpitool -a
to a string?
它不起作用;变量$a
ia 不与字符串“AC 适配器:在线”进行比较。如何将命令的输出转换acpitool -a
为字符串?
This is what happens:
这是发生的事情:
AC adapter : online
./acpower.sh: linha 7: AC: comando n?o encontrado
OFFLINE
Problem solved!
问题解决了!
This is the new code, whith the help of all of you, thanks.
这是新代码,在大家的帮助下,谢谢。
#!/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
This code may be use on a server, to alert if the power fails!
此代码可用于服务器,以在电源出现故障时发出警报!
采纳答案by Jonathan Leffler
How to fix the problem
如何解决问题
The shell (or the test
command) uses =
for string equality and -eq
for numeric equality. Some versions of the shell support ==
as a synonym for =
(but =
is defined by the POSIX test
command). By contrast, Perl uses ==
for numeric equality and eq
for string equality.
shell(或test
命令)=
用于字符串相等和-eq
数字相等。某些版本的 shell 支持==
作为=
(但=
由 POSIXtest
命令定义)的同义词。相比之下,Perl==
用于数字相等和eq
字符串相等。
You also need to use one of the test commands:
您还需要使用以下测试命令之一:
if [ "$a" = "AC adapter : online" ]
then echo "ONLINE"
else echo "OFFLINE"
fi
Or:
或者:
if [[ "$a" = "AC adapter : online" ]]
then echo "ONLINE"
else echo "OFFLINE"
fi
With the [[
operator, you could drop the quotes around "$a"
.
使用[[
运算符,您可以删除"$a"
.
Why you got the error message
为什么会收到错误消息
When you wrote:
当你写道:
if $a -eq "AC adapter : online"
the shell expanded it to:
外壳将其扩展为:
if AC adapter : online -eq "AC adapter : online"
which is a request to execute the command AC
with the 5 arguments shown, and compare the exit status of the command with 0 (considering 0 — success — as true and anything non-zero as false). Clearly, you don't have a command called AC
on your system (which is not very surprising).
这是一个AC
使用显示的 5 个参数执行命令的请求,并将命令的退出状态与 0 进行比较(考虑 0 — 成功 — 为真,任何非零为假)。显然,您AC
的系统上没有调用命令(这并不奇怪)。
This means you can write:
这意味着你可以写:
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
If you want to test strings, you have to use the test
command or the [[ ... ]]
operator. The test
command is the same as the [
command except that when the command name is [
, the last argument must be ]
.
如果要测试字符串,则必须使用test
命令或[[ ... ]]
运算符。该test
命令是一样的[
,只是当命令名称是命令[
,最后一个参数必须是]
。
回答by SSaikia_JtheRocker
Please try this -
请试试这个 -
#!/bin/bash
a="$(acpitool -a)"
echo "$a"
if [ "$a" == 'AC adapter : online' ]
then
echo "ONLINE"
else
echo "OFFLINE"
fi
Explanation:-eq
is mainly used for equivalence for integer expressions. So, == is the way to go! And, use double quotes across $a or $(acpitool -a) to prevent word splitting. An argument enclosed within double quotes presents itself as a single word, even if it contains whitespace separators.
说明:-eq
主要用于整数表达式的等价。所以,==是要走的路!并且,在 $a 或 $(acpitool -a) 之间使用双引号以防止分词。括在双引号内的参数将自身显示为单个单词,即使它包含空格分隔符。
回答by Ansgar Wiechers
Put the comparision in square brackets and add double quotes around the $a
:
将比较放在方括号中并在 周围添加双引号$a
:
if [ "$a" == "AC adapter : online" ]; then
...
Without the square brackets bash
tries to execute the expression and evaluate the return value.
没有方括号bash
尝试执行表达式并评估返回值。
It may also be a good idea to put the command substitution in double quotes:
将命令替换放在双引号中也可能是一个好主意:
a="$(acpitool -a)"
回答by Hedron Dantas
Problem solved!
问题解决了!
This is the new code, whith the help of all of you, thanks.
这是新代码,在大家的帮助下,谢谢。
#!/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
This code may be use on a server, to alert if the power fails!
此代码可用于服务器,以在电源出现故障时发出警报!