Problem with script

Script works fine on the server on which it is running, but fails to run the command when it runs on a remote host using the ssh.Here's the script:

#!/bin/bash

fdate=$(date "+%Y-%m-%d")
export curr_host="`hostname`.we.com"
if [ ! -s ${HOME}/hosts.txt ]
  then
  echo "Hosts file is missing or a zero-byte file. Exiting the process" >> log.txt
  exit 1
  else
 IFS=$'\n'
    for i in `dsmc q fi |sed '0,/----/d'| awk '{print $2,$3,$5}'`
  do
  echo "$curr_host $i" >> tsm_bkp_$fdate.txt
   done
 while read line <&3
  do
ssh -t $line "IFS=$'$\n';for i in `dsmc q fi |sed '0,/----/d'| awk '{print $2,$3,$5}'`; do echo "$line" $i >> ${HOME}/tsm_bkp_$fdate.txt; done"
 done 3<${HOME}/hosts.txt
fi

I am expecting the script to do the following.

  1. Run the command on current host, add hostname to each line from the command output.
  2. Run the command on remote host, add hostname to each line from the command output.
  3. Consolidate all the information into one file located on the current server.

Below is the output file created by the script. From the log file, it is evident that the output from the command executed on the local host is written to the log file, but errors out while running on the remote host.

infadev.we.com 09/11/15 02:01:30 /
infadev.we.com 09/11/15 02:02:19 /app/infa
infadev.we.com 09/11/15 02:02:16 /app/ca
infadev.we.com 09/11/15 02:01:30 /boot
infadev.we.com 09/11/15 02:01:30 /home
infadev.we.com 09/11/15 02:01:30 /stg/bin
infadev.we.com 09/11/15 02:01:38 /opt
infadev.we.com 09/11/15 02:01:41 /usr
infadev.we.com 09/11/15 02:01:46 /var
bash: -c: line 1: syntax error near unexpected token `09/11/15'
bash: -c: line 1: `09/11/15 02:02:19 /app/infa'
Connection to infait.we.com closed.

bash: -c: line 1: syntax error near unexpected token `09/11/15'
bash: -c: line 1: `09/11/15 02:02:19 /app/infa'
Connection to infast.we.com closed.

Just wanted to check if someone can help.

Thank you.

There's a $ char too many in the IFS definition for the remote execution. I'd propose to move the redirection to after the while loop to untangle what's done where.

Looks like the bash shell wants (and fails) to expand/execute/... 09/11/15 02:02:19 /app/infa which is the output of your dsmc |sed | awk command substitution and thus the contents of $i which in turn is ONLY referenced in the echo after the expanded $line - so I guess sth is wrong with $line - what's the structure of your {HOME}/hosts.txt file? Any unusual line terminators, for instance?

---------- Post updated at 23:31 ---------- Previous update was at 23:16 ----------

And, would replacing the entire for loop with

dsmc q fi | awk -vL=$line 'NR==1,/----/ {next} {print L, $2, $3, $5}'

work?

The problem with ssh arguments is that they are processed by the local shell then again by the shell on the remote host. So one often needs to escape two times.
Therefore I recommend a script that runs on one host, plus another script that loops over all hosts, each time passing the one-host script for remote execution.

  ssh "$host" /bin/bash < local_script