Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a single-line command that does a curl request to check server status and then output the result to a log file. But the problem is the returned result has no line break and so the log file messes up when the command writes to the log file the next time around.

curl -s "http://myserver.com/api?param=1&param=2" | sudo tee -a /var/log/myserver.log

I like to keep this to a single-line command. Any suggestion is welcome. Thank you for your time.

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

Use echo, it will automatically append a newline:

echo "`curl -s "http://myserver.com/api?param=1&param=2"`" | sudo tee -a /var/log/myserver.log

Alternatively, you could try the -w option, but I found that it somehow prints funny characters to the console (but not to the file, luckily):

curl -s "http://myserver.com/api?param=1&param=2" -w "\n" | sudo tee -a /var/log/myserver.log
share|improve this answer
    
I tried both, but they didn't work. Interestingly, the first one actually removes all line breaks from the result. Could it be due to the use of ` in the command? Thanks anyway. –  GooDoo May 13 '13 at 8:39
1  
Yeah, I assumed the output or curl would not contain line breaks. I added double quotes around the ` invocation, so that should do. The second option should work fine, though, as it only instructs curl to output a newline additionally (see man curl). –  Stefan Seidel May 13 '13 at 8:48
    
After adding the double quotes around the ` it works! Thanks! –  GooDoo May 13 '13 at 8:53
add comment

Simplest is just to append a newline with echo

curl -s "http://myserver.com/api?param=1&param=2" | sudo tee -a /var/log/myserver.log && echo "" >> /var/log/myserver.log

share|improve this answer
    
This worked like a charm. Thanks! –  GooDoo May 13 '13 at 8:36
    
Hmmm...when I tried this on my local machine, it works fine. But when used in the server, it says, "bash: /var/log/myserver.log: Permission denied". Adding sudo before echo didn't work either. –  GooDoo May 13 '13 at 8:48
1  
But it's not using sudo to echo into your logfile, so at least it'll need to be ... && echo | sudo tee -a ... unless sudo isn't needed? –  Stefan Seidel May 13 '13 at 8:49
3  
try (curl -s "http://www.nexon.org.uk" && echo "") | sudo tee -a ... –  suspectus May 13 '13 at 8:53
    
@suspectus It works! Thanks! –  GooDoo May 13 '13 at 9:02
add comment

I use awk 1 for that (where 1 is just something that evaluates to true):

$ printf a|awk 1
a
$ printf a\\n|awk 1
a
$ 

It should work with gawk, BWK awk / nawk (that comes with OS X), and mawk (that comes with Debian). sed -n p works with OS X's sed but not with GNU sed.

A Bash-only alternative:

printf %s\\n "$(cat)"

Note that $() removes all linefeeds from the end, so for example echo $'a\n\n'|printf %s\\n "$(cat)" only prints one linefeed.

You could also replace printf %s\\n with echo, but for example x=-nene;echo "$x" doesn't print anything in Bash (unless xpg_echo and POSIX mode are enabled).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.