Вместо того, чтобы пытаться понять, как вкладывать кавычки самостоятельно, пусть оболочка сделает это за вас.Обратите внимание:
# put the code we want to run remotely in a function
cmd1_remote_part() { [ -d "$1/.git" ] || git init "$1"; }
cmd1() {
# create a single string with the remote argument(s) we want to pass w/ eval-safe quoting
printf -v args_q '%q ' "${REMOTE_GIT_REPO_DIR}/${path}"
# pass to the remote shell (1) the function definition; (2) a function invocation;
# ...(3) the above argument list.
ssh "${instance_ipaddr}" "$(declare -f cmd1_remote_part); cmd1_remote_part $args_q"
}
cmd2() {
submodule_stash_commit=$(git rev-parse HEAD)
git push -uf "ssh://${instance_ipaddr}/${REMOTE_GIT_REPO_DIR}/${path}" \
"${submodule_stash_commit}:refs/heads/remote-push"
}
cmd3() {
submodule_stash_commit=$(git rev-parse HEAD)
# substituting paths into remote ssh commands introduces security risks absent eval-safe
# ...quoting, as done with printf %q.
printf -v remote_cmd_q 'cd %q && git checkout %q' \
"${REMOTE_GIT_REPO_DIR}/${path}" "${submodule_stash_commit}"
ssh "${instance_ipaddr}" "$remote_cmd_q"
}
git submodule foreach "$(declare -f cmd1 cmd1_remote_part); cmd1"
git submodule foreach "$(declare -f cmd2); cmd2"
git submodule foreach "$(declare -f cmd3); cmd3"
Обратите внимание, что тела всех этих функций написаны точно так, как вы бы написали их для локальной оболочки;declare -f
затем генерирует объявление функции, которое может быть развернуто удаленно.