I want to do this ssh ${w100user}@web100 'ls -l "$(grep "${1}" /etc/pure-ftpd/pureftpd.passwd|cut -d':' -f6)"'

Which obviously performs an ssh session to server web100 as w100user and then greps for the script's first argument "$1" in the file pureftp.passwd, grabbing the 6th field which should be that user's home directory and will then list the contents with a long listing of that user's home directory. I can't seem to get $1 to expand properly before in the command substitution over SSH. I tried multiple quoting methods including '$( "$1" command...)' and at this point it's just guessing.


Pass it in a $LC_xxx variable which is one that ssh often passes across:

LC_MY_USER=$1 ssh "${w100user}@web100" '
    ls -l "$(grep -F "$LC_MY_USER" /etc/pure-ftpd/pureftpd.passwd|cut -d: -f6)"'

Or maybe more robustly:

LC_MY_USER=$1 ssh "${w100user}@web100" '
    ls -l -- "$(awk -F: "/\$1 == ENVIRON[\"LC_MY_USER\"] {print \$6; exit}
      " /etc/pure-ftpd/pureftpd.passwd)"'

(assuming you want to check $1 against the first field in that password file).

You could do:

ssh "${w100user}@web100" '
    ls -l "$(grep -F "'"$1"'" /etc/pure-ftpd/pureftpd.passwd|cut -d: -f6)"'

to have the local shell expand $1, but beware that it's potentially dangerous as it's interpreted as shell code by the remote shell (think for instance of $1 being $(rm -rf /)). So you'd have to sanitize $1 first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.