Skip to content

Instantly share code, notes, and snippets.

@oldratlee
Created May 10, 2013 06:46
Show Gist options
  • Save oldratlee/5552785 to your computer and use it in GitHub Desktop.
Save oldratlee/5552785 to your computer and use it in GitHub Desktop.
solve the problem that ssh break the while read loop

Description

ssh break the while loop.

0</dev/null or -n option of ssh can resolve this problem.

Sample Code

#!/bin/bash

cmd="ps -ef | awk '{print \$2}'"

while read node ; do
    pids=$(0</dev/null ssh "root@$node" "$cmd")
	echo "$node" $pids
done
#!/bin/bash

cmd="ps -ef | awk '{print \$2}'"

while read node ; do
	pids=$(ssh -n "root@$node" "$cmd")
	echo "$node" $pids
done

# -n is a more simple and direct way!

Related Material

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment