Is there a way to pass in a variable as an argument to a bash script and have it evaluated scoped by the bash script?
Given:
# cat /path/to/file/of/host/names
bob
tom
joe
etc...
# dofor
FILE=$1
shift
CMD=$*
while read host; do
# recursively turns "ssh \$host hostname" from $@ into:
# ssh bob hostname
# ssh tom hostname
# ssh joe hostname
# etc...
eval $CMD
done < $FILE
When:
# dofor /path/to/file/of/host/names "ssh \$host hostname"
Then: I'd receive the output from running ssh host hostname
for each hostname listed in /path/to/file/of/host/names. e.g.:
bob.example.com
tom.example.com
joe.example.com
etc...
dofor
code as posted already does what you're asking...$host
is enclosed in single quotes and the command does not try to steal access to stdin.