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.
- Run the command on current host, add hostname to each line from the command output.
- Run the command on remote host, add hostname to each line from the command output.
- 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.