From 6aa2a9af5e24ddfed681fd7f1d68013abb6c471f Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sat, 22 Oct 2016 19:01:41 +0200 Subject: Fix shell-wrap as needed, singlequote-escaped. --- xsh | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'xsh') diff --git a/xsh b/xsh index 081d471..c655c57 100755 --- a/xsh +++ b/xsh @@ -6,6 +6,18 @@ usage() { echo "Usage: $SCRIPT [--x] [--verbose] [su|sudo] [[{user}]@{host}] [[--] command...]" } +# origin: http://stackoverflow.com/a/34515468 +quote_args() { + local sq="'" + local dq='"' + local space="" + local arg + for arg; do + echo -n "$space'${arg//$sq/$sq$dq$sq$dq$sq}'" + space=" " + done +} + # Initial setup SU='' SUDO='' @@ -31,16 +43,15 @@ done [ $# -gt 0 ] || set -- /bin/bash -# FIXME: needs shell wrapper (provided by SSH but not in local use) -[ -z "$SU" ] || set -- "echo -n 'Changing to root...: '; su -c \"cd; "$@"\"" +[ -z "$SU" ] || set -- su -c "cd; $(quote_args "$@")" +[ -z "$SU" ] || set -- sh -c "echo -n 'Changing to root...: '; $(quote_args "$@")" [ -z "$SUDO" ] || set -- sudo -i -- "$@" # Pause for a moment if verbose -# FIXME: needs shell wrapper (provided by SSH but not in local use) -[ -z "$V" ] || set -- "$@"\; sleep 4 +[ -z "$V" ] || set -- sh -c "$(quote_args "$@"); sleep 4" -[ -z "$HOST" ] || set -- ssh -t${X:+ -X}${V:+ -v}${PORT:+ -p "$PORT"} $HOST "$@" +[ -z "$HOST" ] || set -- ssh -t${X:+ -X}${V:+ -v}${PORT:+ -p "$PORT"} $HOST "$(quote_args "$@")" [ -z "$V" ] || echo "$@" [ -z "$V" ] || sleep 4 -- cgit v1.2.3