Daedalus8
Daedalus8

Reputation: 30

Remote ssh command, LF missing on STDOUT

This is my first question here :)

I'm running into an issue while running a remote SSH command. The issue is that the output of the command comes back in a string without LF to delimit the lines. I checked the output of Hive by itself and it outputs the LFs normally, they are missing when I run the command from a remote host. Is there any way to maintain the output of the remote host?

Here's a snippet of the code:

#!/bin/bash
partition="2013-04-02"
query=$(cat <<EOF
set mapred.job.name="Unique IP - $partition";
select
distinct
src
,dst
,service
,proto
,action
from
logs
where
src rlike "^10\."
and src not rlike "^10\.90\."
and src not rlike "^10[0-9]\."
and src not rlike "\.5[2-8]$"
and dst <> ""
and day="$partition";
EOF)

ssh=`ssh user@hadoop "hive -S -e '$query' 2>&1"
echo $ssh > output.tsv

Upvotes: 1

Views: 1936

Answers (2)

Mark Reed
Mark Reed

Reputation: 95242

It's got the newline(s) in there, but you're not seeing them because you don't have double-quotes around the parameter expansion in your echo command.

echo "$ssh" >output.tsv

ETA: The $(...) syntax for command substitution is generally much better than the `...` syntax, as it nests and works sanely with nested quotation marks. And while it doesn't matter in this case, it's good to get in the habit of quoting command substitutions as well:

ssh="$(ssh user@hadoop "hive -S -e '$query' 2>&1")"

Note that the double-quotes inside the $(..) don't cause any problems with the outer quotation marks.

Finally, if all you're doing with this value is echoing it into a file, you can skip the variable and just redirect it there in the first place:

ssh user@hadoop "hive -S -e '$query' 2>&1" >output.tsv

or just

ssh user@hadoop "hive -S -e '$query' " >&output.tsv

Upvotes: 2

msw
msw

Reputation: 43487

The backticks around ssh … are converting your newlines to spaces because they are supposed to.

Try

ssh user@hadoop "hive -S -e '$query' 2>&1" > output.tsv

Upvotes: 0

Related Questions