0

i am writing a wrapper script for sshfs and trying to pass on some of it arguments to sshfs like say sshmount -o allow_others mountpoint

but sshfs also accepts arguments like -o sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server" but this case the quotes gets stripped

i am making use of getopts like this

  7  while getopts 'fsCu:p:o:d1hV' flag; do
  6     case "${flag}" in
  5        u) LD=`fetchHostAttribute $2 'LocalDirectory'`;
  4            [ -n $LD ] && { fusermount -$flag $LD && rmdir $LD; }\
  3                || echo host missing LocalDirectory attribute in $confile
  2            exit;;
  1        p) flags+=" -$flag ${OPTARG}";;
39         o) echo ${OPTARG};exit;;
  1        C|f|1|s|d) flags+=" -$flag";;
  2        V) sshfs --version;exit;;
  3        *) print_usage;
  4            exit;;
  5     esac   
  6 done

with this snipped the output of sshmount -o sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server" is sftp_server=/usr/bin/sudo /usr/lib/openssh/sftp-server is there a way to preserve the quotes? simply quoting ${OPTARGS} turns it into "sftp_server=/usr/bin/sudo /usr/lib/openssh/sftp-server"

To whoever marked this as duplicate, yes i'm aware of that question i have stumbled accross it more then i would like, here's the full source https://github.com/fuseteam/sshmount if you think that question solves the issue i described i.e. Turning sshmount -o sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server" [user@]host into sshfs -o sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server" [user@]host ~/mountpoint while preserving the usage of -u to invoke fusermount.......submit a pull request. I've sunk more time into it than i would like trying to get the answers to those questions to work with it

Fuseteam
  • 376
  • 2
  • 15
  • [Double-quote your variable references](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable); and don't try to store lists of arguments in a plain variable (like `flags`), [use an array instead](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quoting-characters-in-arguments-passed-to-it-through-varia). BTW, [shellcheck.net](https://www.shellcheck.net) will point out many common problems (though it's not quite smart enough to realize you should use an array). – Gordon Davisson May 13 '21 at 04:22
  • @Gordon Davisson it does not as mention double quoting turns it in "sftp_server=/usr/bin/sudo /usr/lib/openssh/sftp-server" which is incorrect it should be sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server" – Fuseteam May 14 '21 at 18:04
  • Common guys if you're mark it as duplicate at least read the question properly the other question is about quoting the **entire** flag argument. I can do that fine but that **breaks** sshfs. It **has** to be option="foo bar" **not** "option=foo bar" – Fuseteam May 14 '21 at 18:11
  • In shell syntax, `option="foo bar"` and `"option=foo bar"` are fully equivalent (as are `option=foo" "bar`, `option=foo\ bar`, and many others). See my and William Pursell's answers [to this question](https://stackoverflow.com/questions/10835933/preserve-quotes-in-bash-arguments). – Gordon Davisson May 14 '21 at 18:35
  • @GordonDavisson yes i would expect the two to be equivalent as mentioned i did try that. but in practice running `sshfs -o "sftp_server=/usr/bin/sudo /usr/lib/openssh/sftp-server" [user@]host ~/mountpoint` breaks sshfs. as i said i've sunk way more time into this than i would like to fix it with the existing answers. if those answers fixed it i would not have posted this question in the first place – Fuseteam May 15 '21 at 04:48
  • Suggestion: put `set -x` (to make it print an equivalent of each command as it's executed) before both a successful invocation and one that fails, and compare. Both versions should include `... 'sftp_server=/usr/bin/sudo /usr/lib/openssh/sftp-server' ...` because `set -x` tends to prefer single-quotes for arguments with any funny characters in them. If there's a difference, it might help figure out what's going wrong. – Gordon Davisson May 15 '21 at 05:01
  • @GordonDavisson i have linked the full source code in the question now, i'm tired of trying things that just don't work because of how `sshfs` handles things do with it what you want. especially when i tried any and all solution sunk this hours searching for an answer trying out many of the proposed solutions. double and triple checking if any of the available solutions apply. i'll push my latest attempt in a bit – Fuseteam May 15 '21 at 05:14
  • 1
    I don't have a setup to test with, but it looks to me like you should add `flags=()` before the `getopts` loop, use `p|o) flags+=(-$flag "${OPTARG}");;` and `C|f|1|s|d) flags+=(-$flag);;` in the `case`, and `sshfs "${flags[@]}" "$LD" "$Host:$RD";` at the end. – Gordon Davisson May 15 '21 at 06:38
  • @GordonDavisson oh hey that works out, thanks for the patience, if you can add that as an answer i'll accept it as the answer – Fuseteam May 15 '21 at 18:25

0 Answers0