You can use
#!/bin/bash
FILE=$1
exec 3<$FILE
while read -u 3 host; do
$2 $host $3
done
Then if you have:
$ cat /tmp/hostnames
some.host1.com
another.host2.net
some.host3.org
some.host4.com
and run dofor.sh /tmp/hostnames ssh ls, it will run in sequence:
ssh some.host1.com ls
ssh another.host2.net ls
ssh some.host3.org ls
ssh some.host4.com ls
EDIT1:
If you'd like to change ssh or ls into some longer commands (or parts of such), just use quotes:
dofor.sh /tmp/hostnames "ssh -p 23" "ls -lh /"
EDIT2:
With the following script, you'll be able to use $host variable in as many places in your command as you want:
#!/bin/bash
exec 3<$1
while read -u 3 host; do
eval $2
done
(I made this one shorter - no useless introducing of $FILE variable.)
The important part here is that you need to use single quotes around the command containing the $host variable:
dofor.sh /tmp/hostnames 'echo "trying $host :"; ssh -p 23 myuser@$host "ls -lh /"'
But beware that it is dangerous to use eval (see l0b0's walll0b0's wall) because if the file /tmp/hostnames contained a command on some line, it would be executed. Better not use this as root!