0

我开始学习如何编写 bash 脚本,但我遇到了 echo 命令和变量的问题。

#!/bin/bash
LOGINOUTPUT = "`wget --no-check-certificate --post-data 'login=redacted&password=redacted' https://nessusserver:8834/login -O -`"
echo $LOGINOUTPUT

运行此脚本将返回以下内容:

--2013-08-15 15:07:32--  https://nessusserver:8834/login
Resolving nessussserver (nessusserver)... 172.23.80.88
Connecting to nessusserver (nessusserver)|172.23.80.88|:8834... connected.
WARNING: cannot verify nessusserver's certificate, issued by ‘/C=FR/ST=none/L=Paris/O=Nessus Users United/OU=Certification Authority for nessusserver.healthds.com/CN=nessusserver.healthds.com/emailAddress=ca@besecmisc1.healthds.com’:
  Unable to locally verify the issuer's authority.
    WARNING: certificate common name ‘nessusserver.healthds.com’ doesn't match requested host name ‘nessusserver’.
HTTP request sent, awaiting response... 200 OK
Length: 461 [text/xml]
Saving to: ‘STDOUT’

100%[=============================================================================================================================================================>] 461         --.-K/s   in 0s      

2013-08-15 15:07:33 (90.4 MB/s) - written to stdout [461/461]

./nessus-output.sh: line 2: LOGINOUTPUT: command not found

为什么它认为 LOGINOUTPUT 是一个命令?提前感谢您的帮助!

编辑:更新的脚本

#!/bin/bash
LOGINOUTPUT=$(wget --no-check-certificate --post-data 'login=redacted&password=redacted' https://nessusserver:8834/login -O -)
echo $LOGINOUTPUT

如果我将 $(...) 保留为反引号,仍然会产生相同的错误。

4

1 回答 1

3

=发生这种情况是因为在变量赋值之前和之后都有空格。正确的分配是:

LOGINOUTPUT="....

没有空格。

如果添加空格,则 shell 将解释LOGINOUTPUT为命令,并尝试将两个参数传递给它:“=”和带引号的字符串。这当然失败,错误LOGINOUTPUT: command not found

作为旁注,$(command)在进行进程替换时,最好使用此语法而不是反引号。

于 2013-08-15T19:19:02.603 回答