Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash脚本可以';不要在壳中运行_Bash_Shell_Zabbix_Sh - Fatal编程技术网

Bash脚本可以';不要在壳中运行

Bash脚本可以';不要在壳中运行,bash,shell,zabbix,sh,Bash,Shell,Zabbix,Sh,我一直在尝试为Zabbix实现一个警报脚本。由于某种原因,Zabbix试图在Shell中运行脚本,而脚本是用Bash编写的 #!/bin/bash # Slack incoming web-hook URL and user name url='https://hooks.slack.com/services/this/is/my/webhook/' # example: https://hooks.slack.com/services/QW3R7Y/D34DC0D3/

我一直在尝试为Zabbix实现一个警报脚本。由于某种原因,Zabbix试图在Shell中运行脚本,而脚本是用Bash编写的

#!/bin/bash

# Slack incoming web-hook URL and user name
url='https://hooks.slack.com/services/this/is/my/webhook/'             # example: https://hooks.slack.com/services/QW3R7Y/D34DC0D3/BCADFGabcDEF123
username='Zabbix Notification System'

## Values received by this script:
# To = $1 (Slack channel or user to send the message to, specified in the Zabbix web interface; "@username" or "#channel")
# Subject = $2 (usually either PROBLEM or RECOVERY/OK)
# Message = $3 (whatever message the Zabbix action sends, preferably something like "Zabbix server is unreachable for 5 minutes - Zabbix server (127.0.0.1)")

# Get the Slack channel or user ($1) and Zabbix subject ($2 - hopefully either PROBLEM or RECOVERY/OK)
to="$1"
subject="$2"

# Change message emoji depending on the subject - smile (RECOVERY/OK), frowning (PROBLEM), or ghost (for everything else)
recoversub='^RECOVER(Y|ED)?$'
if [[ "$subject" =~ ${recoversub} ]]; then
        emoji=':smile:'
elif [ "$subject" == 'OK' ]; then
        emoji=':smile:'
elif [ "$subject" == 'PROBLEM' ]; then
        emoji=':frowning:'
else
        emoji=':ghost:'
fi

# The message that we want to send to Slack is the "subject" value ($2 / $subject - that we got earlier)
#  followed by the message that Zabbix actually sent us ($3)
message="${subject}: $3"

# Build our JSON payload and send it as a POST request to the Slack incoming web-hook URL
payload="payload={\"channel\": \"${to//\"/\\\"}\", \"username\": \"${username//\"/\\\"}\", \"text\": \"${message//\"/\\\"}\", \"icon_emoji\": \"${emoji}\"}"
curl -m 5 --data-urlencode "${payload}" $url -A "https://hooks.slack.com/services/this/is/my/web/hook"
~
当我使用“bash slack.sh”在本地运行脚本时,它会发送一个空通知,我在slack中接收到该通知。 当我使用“sh slack.sh”在本地运行脚本时,会出现以下错误

slack.sh: 19: slack.sh: [[: not found
slack.sh: 21: [: unexpected operator
slack.sh: 23: [: unexpected operator
slack.sh: 34: slack.sh: Bad substitution
谢谢你的帮助

你的谢邦错了

# !/bin/bash
删除第一个空格。

调用脚本时,您似乎使用了该空格

使用

注:
现在,你知道问题所在了。一种解决方案是将脚本更改为POSIX shell或搜索如何强制zabbix处理bash脚本

如果无法说服zabbix使用
bash
执行脚本,则必须放弃正则表达式匹配(奇怪的是,
expr
命令不支持任何形式的替换,这意味着其正则表达式只能识别正则语言的子集):


您可以通过添加

#! /bin/bash

if [ -z "$BASH" ]
then
    exec /bin/bash "$0" "$@"
fi
...

如果
Zabbix
需要一个POSIX shell脚本,那么您必须编写一个POSIX shell脚本,
[[
不是由POSIX定义的。您是否可以配置
Zabbix
使用不同的shell是另一个问题。(如果
Zabbix
只需要一个可执行文件,那么@bishop就有了答案。)(根据Zabbix网站,@bishop删除了他的评论,该评论建议使用
#!/bin/bash
而不是
#!/bin/bash
);Zabbix中的正则表达式支持已从POSIX扩展正则表达式切换到与Perl兼容的正则表达式(PCRE)对于增强的正则表达式和与前端的一致性,“我已经考虑(并尝试)删除'[[',并用单个''[”,但那是徒劳的。我认为Zabbix中的正则表达式支持与此无关;这是一个shell脚本,或者您正在使用
bash
,在这种情况下
=~
需要一个POSIX正则表达式,或者脚本由
/bin/sh
(一个兼容POSIX的shell)执行而且根本不支持正则表达式匹配。但请注意,在shell中进行正则表达式匹配非常简单:
if echo“$string_to_match”| grep“$pattern”>/dev/null;然后…
工作得很好。感谢您的注意。但这不是解决方案。实际上,原始的shebang语法需要空格。@GiacomoCatenazzi我认为这不正确。原始语法在
之后允许有空格,但是
#!
始终必须是文件的前两个字节。@chepner:你说得对,我的朋友眼睛看到了
后面的空格。我使用'sh'是因为Zabbix正试图使用它。当我使用'bash script.sh'时,它可以工作。但我找不到Zabbix也使用bash的方法。该链接似乎是一个伪sh库,由
bash
脚本使用,而不是由Zabbix执行的
bash
脚本。(我之所以说“pseudo”,是因为它似乎是在
bash
的子集中编写的,
bash
在作为
sh
调用时仍能识别。例如,使用
function
关键字定义函数。)或
case“${subject}”在..esac中
当然可以,但我不认为这比本例中的
if
语句有很大的改进。
RECOVER(Y | ED)
也不是一个有效的模式,因此您仍然必须使用
RECOVERY | RECOVERED
。您还有
OK
PROBLEM
default
chmod +x script.sh
/full/path/to/script.sh
# if RECOVER(Y|ED)$ were a valid POSIX basic regular expression,
# you could use
#
#   if expr "$subject" : "$recoverysub"; then
#
# but it is not, so you need...
if [ "$subject" = RECOVERY ] || [ "$subject" = RECOVERED ]; then
#! /bin/bash

if [ -z "$BASH" ]
then
    exec /bin/bash "$0" "$@"
fi
...