简体   繁体   English

JSON和Shell脚本错误

[英]Error with JSON and Shell Script

I have tried to make a slack notification with a shell script. 我试图用shell脚本发出松弛通知。

The JSON parameters are formed by variables what they are obtained by MySql querys. JSON参数由变量构成,这些变量由MySql查询获得。

#!/bin/sh
#MySQL RO Access
host='mysqlserver.com'
userdb='slackro'
password='password'
db='db'
#Slack information
hook='https://hook.slack'
user='slackusr'
channel='o_channel'
emoji='slackusr'

#Query
id=`mysql -D $db -u $userdb -p$password -e 'SELECT id FROM ticket WHERE tn ='$1'' -h $host | sed -e '1d'`
tn=`mysql -D $db -u $userdb -p$password -e 'SELECT tn FROM ticket WHERE tn ='$1'' -h $host | sed -e '1d'`
title=`mysql -D $db -u $userdb -p$password -e 'SELECT title FROM ticket WHERE tn ='$1'' -h $host | sed -e '1d' | sed "s/'/ /g" | sed "s/°//g" | sed "s/ /_/g" `
customer=`mysql -D $db -u $userdb -p$password -e 'SELECT customer_id FROM ticket WHERE tn ='$1'' -h $host | sed -e '1d'`
msj=`mysql -D $db -u $userdb -p$password -e 'SELECT a_body FROM article WHERE ticket_id ='$id' ORDER BY id DESC LIMIT 1' -h $host | sed -e '1d'`
url='http://iiabox.infra.ultra.sur.top/otrs/index.pl?Action=AgentTicketZoom;TicketID'$1

#Message
curl -X POST -H 'Content-type: application/json' --data '{"username": "slackusr","icon_emoji": ":slackusr:","attachments": [{"fallback": "New Ticket","pretext": "New ticket from '$customer'","title": "'$title'","title_link": "'$url'","text": "'$msj'","color": "#006495"}]}' $hook

When I execute this script I obtain something like that 当我执行此脚本时,我会得到类似的东西

curl -X POST -H 'Content-type: application/json' --data '{"username": "OTRS","icon_emoji": ":slackusr:","attachments": [{"fallback": "New Ticket","pretext": "New ticket from my@email.com","title": "Prueba' de Notificación '6" ,"title_link": " http://site/otrs/index.pl?Action=AgentTicketZoom;TicketID2016110472000067 ","text": "Cerrado","color": "#006495"}]}' https://hooks.slack.com/ curl: (6) Could not resolve host: de curl: (6) Could not resolve host: xn--notificacin-zeb curl: (3) [globbing] unmatched close brace/bracket in column 152 curl -X POST -H'内容类型:application / json'--data'{“用户名”:“ OTRS”,“ icon_emoji”:“:slackusr:”,“附件”:[{“ fallback”:“新票证”,“前缀”:“来自my@email.com的新票证”,“标题”: “ Prueba'deNotificación'6” ,“ title_link”:“ http://site/otrs/index.pl?Action = AgentTicketZoom; TicketID2016110472000067 “,” text“:” Cerrado“,” color“:”#006495“}]}' https://hooks.slack.com/ curl:(6)无法解析主机:de curl:(6 )无法解析主机:xn--notificacin-zeb curl:(3)第152列中的[globbing]不匹配的紧括号/括号

I don't understand why the result of the variable $title shows that "Prueba' de Notificación '6" 我不明白为什么变量$ title的结果显示“ Prueba'deNotificación'6”

If I print $title variable with echo I obtain: "Prueba de Notificación 6 " without simple quotes before the first space and after the last space. 如果我用echo打印$ title变量, 则会得到: “ Prueba deNotificación6 ”在第一个空格之前和最后一个空格之后没有简单的引号。

What can I do? 我能做什么?

First: This code is, as a whole, broken beyond repair. 第一:整体而言,此代码无法修复。 Do not use it in production. 不要在生产中使用它。 Rewrite it in a language you're actually good in (and that has SQL libraries that support bind variables so you can fix your security bugs, and JSON libraries that will ensure that content is always correctly quoted), not shell. 用您实际上最擅长的语言(具有支持绑定变量的SQL库,以便您可以修复安全性错误,以及可以确保始终正确引用内容的JSON库)重写该语言,而不是shell。


That said, as for your immediate problem -- 话虽如此,至于您眼前的问题-

Whenever you do this in a single-quoted context: 每当您在单引号中执行此操作时:

"title": "'$title'",

...you're expanding $customer unquoted , meaning that spaces inside the expanded value are used for word-splitting and glob-expansion by the shell. ...您正在扩展$customer unquoted ,这意味着扩展值内的空格将用于shell的单词拆分和glob扩展。

Instead, make it: 相反,使其:

"title": "'"$title"'"

...opening a double-quoted context after terminating the single-quoted context. ...在终止单引号上下文之后打开双引号上下文。

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

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