From b1a1c7d427bccc1df4723282fdd7a775ebc7b13f Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Fri, 9 Dec 2016 05:57:28 -0800 Subject: [PATCH] scope launch script: Attempt to quote args in "scope command" As indicated by the TODO, any args passed into the command do not get escaped when output, so for example: scope command "foo bar" would output results like: foo bar instead of "foo bar" or foo\ bar The "right" way to do this seems to be printf %q, which prints a quoted version of the string. However this format specifier is not available in POSIX sh (though it does work in many implementations of it, such as the ones provided by bash which make up the likely majority of real-world usage). This code is a compromise that uses the added functionality where available, while still falling back to the old behaviour when it isn't. --- scope | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/scope b/scope index 9bb0ddbed..010411c8b 100755 --- a/scope +++ b/scope @@ -172,8 +172,16 @@ check_docker_version case "$COMMAND" in command) - # TODO: properly escape/quote the output of "$@" - echo "$(launch_command)" "$@" + # Most systems should have printf, but the %q specifier isn't mandated by posix + # and can't be guaranteed. Since this is mainly a cosmetic output and the alternative + # is not making any attempt to do escaping at all, we might as well try. + quoted=$(printf '%q ' "$@" 2>/dev/null || true) + # printf %q behaves oddly with zero args (it acts as though it recieved one empty arg) + # so we ignore that case. + if [ -z "$quoted" ] || [ $# -eq 0 ]; then + quoted="$*" + fi + echo "$(launch_command) $quoted" ;; version) @@ -245,7 +253,7 @@ EOF fi if check_docker_for_mac ; then if docker inspect "$SCOPE_APP_CONTAINER_NAME" >/dev/null 2>&1 ; then - docker stop "$SCOPE_APP_CONTAINER_NAME" >/dev/null + docker stop "$SCOPE_APP_CONTAINER_NAME" >/dev/null fi fi ;;