Shell scripts, SSH sessions, and for loops, oh my!

Damian Gerow dgerow at afflictions.org
Mon Jul 25 10:09:18 GMT 2005


Thus spake Gary W. Swearingen (garys at opusnet.com) [25/07/05 02:13]:
: > I'm trying to write a shell script that runs a for loop in an SSH session.
: > Simply, I'm trying to do this:
: >
: >     for HOST in `cat hostnames` ; do
: > 	ssh ${HOST} "for PROCESS in 01 02 ; do echo '${PROCESS}' ; done"
: >     done
: >
: > But because this is run in a script, that gets translated to:
: >
: >     for HOST in `cat hostnames` ; do
: > 	ssh ${HOST} "for PROCESS in 01 02 ; do echo '' ; done"
: >     done
: 
: Here's a few clues (assuming you really want the single quotes in there):
: 
: echo "THIS is SHELL: '\$SHELL'"
: echo "THIS is SHELL: '"'$SHELL'"'"
: echo 'THIS is SHELL: '\''$SHELL'\'
: 
: which all give
: 
: THIS is SHELL: '$SHELL'

Yeah, but...

The current shell still interprets $SHELL, and assigns it whatever local
value it has.  I don't want that.  I want $SHELL to be evaluated by the
remote system (the on to which I am establishing the SSH connection).

To answer my own question: it looks like sh just isn't the right tool for
the job.  I'm going to have to either completely change my approach to the
problem I'm trying to solve, or use Perl and Net::SSH (and given my problem,
I'm tending towards the latter).

  - Damian


More information about the freebsd-questions mailing list