I have the following script.sh:
#!/bin/bash
ssh server1 "echo hello from server1"
ssh server2 "echo hello from server2"
When executing it as cat ./script.sh | bash I get
hello from server1
And when executing it as bash ./script.sh I get
hello from server1
hello from server2
Can anyone explain the difference? 🙂
In the first case the output of cat is connected to the standard input both of bash and of ssh. ssh reads from its stdin, therefore consuming the rest of the output of cat, although in this case the result is discarded since the remote command never itself reads stdin.
In the second case the stdin for bash, and therefore ssh, is your terminal, and bash opens the script file separately, so ssh does not get to see it.