简体   繁体   English

将输出转换为字符串

[英]Convert an output to string

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".变量$a ia 不与字符串“AC 适配器:在线”进行比较。 How to convert the output of command acpitool -a to a string?如何将命令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!此代码可用于服务器,以在电源出现故障时发出警报!

How to fix the problem如何解决问题

The shell (or the test command) uses = for string equality and -eq for numeric equality. shell(或test命令)使用=表示字符串相等,使用-eq表示数字相等。 Some versions of the shell support == as a synonym for = (but = is defined by the POSIX test command).某些版本的 shell 支持==作为=的同义词(但=由 POSIX test命令定义)。 By contrast, Perl uses == for numeric equality and eq for string equality.相比之下,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).这是一个请求,使用显示的 5 个参数执行命令AC ,并将命令的退出状态与 0 进行比较(考虑 0 — 成功 — 为真,任何非零为假)。 Clearly, you don't have a command called AC on your system (which is not very surprising).显然,您的系统上没有名为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.如果要测试字符串,则必须使用test命令或[[ ... ]]运算符。 The test command is the same as the [ command except that when the command name is [ , the last argument must be ] . test命令与[命令相同,只是当命令名称为[ ,最后一个参数必须为]

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)"

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.说明: -eq主要用于整数表达式的等价。 So, == is the way to go!所以,==是要走的路! And, use double quotes across $a or $(acpitool -a) to prevent word splitting.并且,在 $a 或 $(acpitool -a) 之间使用双引号以防止分词。 An argument enclosed within double quotes presents itself as a single word, even if it contains whitespace separators.括在双引号内的参数将自身显示为单个单词,即使它包含空格分隔符。

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!此代码可用于服务器,以在电源出现故障时发出警报!

I found we can use git commit --amend to trigger the workflow without changing any code.我发现我们可以使用git commit --amend来触发工作流程,而无需更改任何代码。 Example: build.yml示例:build.yml

on: 
  push:
    branches:
      - cicd

git command: git 命令:

# cd cicd branch
git commit --amend
git push --force

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM