0

I have a shell-script which extract details from a log file between two dates and executes a command on the output to generate some report.The log files are on different server and scripts are executed on different server. the Script looks like :

#!/bin/sh
time1=$1
time2=$2
echo `ssh -i key  user@ip -yes cat file | awk '{if(substr($4,2)>="'$time1'" && substr($4,2)<="'$time2'") print $0;}'` > tmp.log

`cat tmp.log | some operation to get detail`

The output expected to be like :

ip1 date1 - - request1 file1 method1 status1 
ip2 date2 - - request2 file2 method2 status2

i.e. multiple lines but the output generated is a single line containing all the details like:

 ip1 date1 - - request1 file1 method1 status1 ip2 date2 - - request2 file2 method2 status2

When running the same command directly on the server its generate desired output but not when executed remotely.
So my question is how would I get the correct output and is this a good way to do this ?

anand
  • 152
  • 6
  • echo eats your newlines, see here http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2007-05/msg00584.html – Dan Feb 23 '15 at 09:26
  • Thank you it really worked. I just added the command inside **""** without removing backticks and it worked. – anand Feb 23 '15 at 09:53
  • @anand As you have your answer, would you be willing to add it as an answer to your own question? Else this question will get a lot of attention from people looking to answer it, only to find out it already has. – Reaces Feb 23 '15 at 10:12

1 Answers1

1

As explained in various places (like this one http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2007-05/msg00584.html) echo outputs its arguments separated by spaces

Therefore you either have to pass the result of the command in the backticks as one argument

echo "`some command that outputs multiple lines here`"

Or not use echo but for instance printf

Dan
  • 641
  • 4
  • 7