sequential SSH command execution not working in Ubuntu/Bash

Posted by kumar on Super User See other posts from Super User or by kumar
Published on 2014-05-20T08:56:31Z Indexed on 2014/05/27 3:33 UTC
Read the original article Hit count: 534

Filed under:
|
|
|

My requirement is I will have a set of commands that needs to be executed in a text file. My Shell script has to read each command, execute and store the results in a separate file.

Here is the snippet which does the above requirement.

while read command
do
    echo 'Command :' $command >> "$OUTPUT_FILE"
    redirect_pos=`expr index "$command" '>>'`       
    if [ `expr index "$command" '>>'` != 0 ];then
        redirect_fn "$redirect_pos" "$command";        
    else  
        $command 
        state=$?
        if [ $state != 0 ];then
                echo "command failed." >> "$OUTPUT_FILE"
        else
        echo "executed successfully." >> "$OUTPUT_FILE"
        fi
     fi
    echo  >> "$OUTPUT_FILE"
done < "$INPUT_FILE"

Sample Commands.txt will be like this ...

tar -rvf /var/tmp/logs.tar -C /var/tmp/ Commands_log.txt
gzip /var/tmp/logs.tar
rm -f /var/tmp/list.txt

This is working fine for commands which needs to be executed in local machine. But When I am trying to execute the following ssh commands only the 1st command getting executed.

Here are the some of the ssh commands added in my text file.

ssh uname@hostname1 tar -rvf /var/tmp/logs.tar -C /var/tmp/ Commands_log.txt
ssh uname@hostname2 gzip /var/tmp/logs.tar
ssh .. etc

When I am executing this in cli it is working fine. Could anybody help me in this?

© Super User or respective owner

Related posts about linux

Related posts about ubuntu