Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl -F line break not interpreted correctly

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.

like image 860
Touff Avatar asked May 29 '14 12:05

Touff


1 Answers

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
like image 65
rici Avatar answered Sep 23 '22 04:09

rici