curl -F换行不能正确解释 [英] curl -F line break not interpreted correctly

查看:4539
本文介绍了curl -F换行不能正确解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过pushover在bash脚本中使用curl发送通知。
我不能得到 curl -F 来正确解释换行符。

  curl -s \ 
-Ftoken = TOKEN\
-Fuser = USER\
-Fmessage = HOST上的根Shell访问\\\
`date` \\\
`who`\
https://api.pushover.net/1/messages.json> NUL

我试过:

  \\\

\\\\\

%A0

$ b $

解决方案

<$> c $ c> curl 不解释反斜杠转义,因此必须在 curl 看到的参数中插入一个实际的换行符。换句话说,你必须得到shell( bash )来解释 \\\
您需要插入一个真正的换行符。



Posix标准shell不会解释 \\\
尽管标准实用程序命令 printf 会。但是, bash 确实提供了一种方法: $'...' C风格反斜杠转义将是解释器。否则, $'...'的行为就像'...',所以参数和命令替换



然而,任何shell - 包括 bash - 允许换行符出现在引号内,换行符只是按原样传递。所以你可以这样写:

  curl -s \ 
-Ftoken = $ TOKEN\
-Fuser = $ USER\
-Fmessage = $ HOST上的根Shell访问
$(日期)
$(who)
\
https://api.pushover.net/1/messages.json> / dev / null

(注意:我插入了参数扩展, curl 命令,并将已弃用的反引号命令替换为推荐的 $(...) >

如上所述,包含文字换行符的唯一问题是,如果你关心外观,它会混淆缩进。所以你可能更喜欢bash的 $'...'表单:

  curl -s \ 
-Ftoken = $ TOKEN\
-Fuser = $ USER\
-Fmessage = $ HOST上的根Shell访问$ '\\\
'$(date)$'\\\
'$(who)\
https://api.pushover.net/1/messages.json> / dev / null

这也有点难以阅读,但它是完全合法的。 shell允许单个参数(word)由任何数量的引用或不引用段组成,只要段之间没有空格。但您可以通过预定义一个变量来避免多重引用语法,这样一些人会更容易理解:

  NL = $'\ n'
curl -s \
-Ftoken = $ TOKEN\
-Fuser = $ USER\
-Fmessage =访问$ HOST $ NL $(日期)$ NL $(who)\
https://api.pushover.net/1/messages.json> / dev / null



最后,可以使用标准实用程序 printf ,如果你更习惯那种风格:

  curl -s \ 
-F token = $ TOKEN\
-Fuser = $ USER\
-F$(printfmessage =%Shell上的root Shell访问%s \\\
% s \\\
\
$ HOST$(date)$(who))\
https://api.pushover.net/1/messages.json > / dev / null


I'm trying to send a notification via pushover using curl in a bash script. I cannot get curl -F to interpret the line break correctly though.

curl -s \
-F "token=TOKEN" \
-F "user=USER" \
-F "message=Root Shell Access on HOST \n `date` \n `who` " \
https://api.pushover.net/1/messages.json > NUL

I've tried:

\n 
\\\n 
%A0

I'd rather push the message out directly, not through a file.

解决方案

curl doesn't interpret backslash escapes, so you have to insert an actual newline into the argument which curl sees. In other words, you have to get the shell (bash in this case) to interpret the \n, or you need to insert a real newline.

A Posix standard shell does not interpret C escapes like \n, although the standard utility command printf does. However, bash does provide a way to do it: in the quotation form $'...' C-style backslash escapes will be interpreter. Otherwise, $'...' acts just like '...', so that parameter and command substitutions do not take place.

However, any shell -- including bash -- allows newlines to appear inside quotes, and the newline is just passed through as-is. So you could write:

curl -s \
     -F "token=$TOKEN" \
     -F "user=$USER" \
     -F "message=Root Shell Access on $HOST
$(date)
$(who)
" \
     https://api.pushover.net/1/messages.json > /dev/null

(Note: I inserted parameter expansions where it seemed like they were missing from the original curl command and changed the deprecated backtick command substitutions to the recommended $(...) form.)

The only problem with including literal newlines, as above, is that it messes up indentation, if you care about appearances. So you might prefer bash's $'...' form:

curl -s \
     -F "token=$TOKEN" \
     -F "user=$USER" \
     -F "message=Root Shell Access on $HOST"$'\n'"$(date)"$'\n'"$(who)" \
     https://api.pushover.net/1/messages.json > /dev/null

That's also a little hard to read, but it is completely legal. The shell allows a single argument ("word") to be composed of any number of quoted or unquoted segments, as long as there is no whitespace between the segments. But you can avoid the multiple quote syntax by predefining a variable, which some people find more readable:

NL=$'\n'
curl -s \
     -F "token=$TOKEN" \
     -F "user=$USER" \
     -F "message=Root Shell Access on $HOST$NL$(date)$NL$(who)" \
     https://api.pushover.net/1/messages.json > /dev/null

Finally, you could use the standard utility printf, if you are more used to that style:

curl -s \
     -F "token=$TOKEN" \
     -F "user=$USER" \
     -F "$(printf "message=Root Shell Access on %s\n%s\n%s\n" \
                  "$HOST" "$(date)" "$(who)")" \
     https://api.pushover.net/1/messages.json > /dev/null

这篇关于curl -F换行不能正确解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆