Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
    echo 'Command :' $command >> "$OUTPUT_FILE"
    redirect_pos=`expr index "$command" '>>'`       
    if [ `expr index "$command" '>>'` != 0 ];then
        redirect_fn "$redirect_pos" "$command";        
        if [ $state != 0 ];then
                echo "command failed." >> "$OUTPUT_FILE"
        echo "executed successfully." >> "$OUTPUT_FILE"
    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?

share|improve this question
You should enclose what follows ssh uname@hostname1 in quotes: '' if you don't want your shell to operate any substitution, "" otherwise. – MariusMatutiae May 20 '14 at 9:54

I'm not sure I get your problem correctly. But just generate the script on the local side, copy it via scp and then execute it with ssh should be much easier.

If you want to execute it soley with ssh you should use this syntax:

ssh localhost -C "echo test; echo blub"

EDIT: Okay I finally found the problem in your script. $command is running your "command" inside the "while read" loop, and while local commands like "echo" or "tar -f" (the -f is important as it take a FILE and no input from stdin) everything works like a charm.

SSH on the other hand tries to create an "interactive" session where your keyboard input is forwarded to the other machine. The problem now is that it reads all the "waiting data" from your "< $INPUT_FILE" redirection and is sending it to the ssh host.

You can actually see this behavior. Try adding a command like:

ssh user@host tee flush.txt

Tee is now redirecting the "rest" of your $INPUT_FILE to the remote hosts file "flush.txt". So the next time we pass "done" there is nothing left to consume for "read".

To fix this behaviour you simply have to tell $command to get the input from something like /dev/null.

$command < /dev/null

will fix your issue.

share|improve this answer
As I said in my query I am reading this commands from a text file. If I use quotes in text file commands it is not working as expected. What I meant is this is my command in text file "ssh root@SC-2-2 -C "ls /var/tmp/" .....When I am trying to execute this command using the above said script I am getting an error like this "bash: ls /var/tmp/: No such file or directory" – kumar May 21 '14 at 6:41
Wow this was a tough one! See my updated solution :) – SaschaZorn May 27 '14 at 0:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.