简体   繁体   中英

Executing command remotely on another linux server from a shell script

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 in multiple lines like :

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

But the output generated (by my command) is getting concatenated into a single line, containing all the details, like:

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

When the command is executed directly on the server it generates 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 it ?

From your comments above you are still using "echo". @tripleee is correct. Don't. Also don't use the pointless cat at the start - simply redirect std in to your next command.

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

some operation to get detail <tmp.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM