I am using a shell script to telnet into a remote unix server and execute certain commands. But if any command being executed remotely, throws an error, the script just hangs. And the telnet session does not get closed.
I want to be able to close the session and complete the script execution in case of an error or otherwise.
Below is the script. Any help will be appreciated.
#!/bin/sh
( echo open host
sleep 1
echo user
sleep 5
echo pswd
sleep 4
sourceDir=$1 ; filename=$3 ; destDir=$2
echo "cd ${sourceDir}"
sleep 1
echo "cp -rf ${filename} ${destDir} && \
cd ${destDir} && \
$cmd ; exit"
) | telnet
I also want to be sure the previous command got executed before proceeding to execute the next command, which is why I'm using '&&'. This, not by using "sleep", but some other way of ensuring the earlier command did get executed successfully.
The above script cribs at the echo of the last command. Snippet of the error is
uctvd201:/opt/epds/EPDSDV01/epdata/SPONSOR/0000_071/test [EPDSDV01]# d && \ <
sh: : Execute permission denied.
uctvd201:/opt/epds/EPDSDV01/epdata/SPONSOR/0000_071/test [EPDSDV01]# ad && \ <
sh: : not found.