From 1189ace8115ce649fabebbee60d6713e1077cd2e Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Sat, 2 Feb 2019 10:48:19 +1100 Subject: [PATCH] Issue #18: Use cat to reduce level of quotes This does not fix the Issue #18 but will simplify the structure of the remote command by removing a level of quotes. --- lib/core.sh | 38 ++++++++++++++------------------ tests/integ-tests/integ-tests.sh | 2 +- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/lib/core.sh b/lib/core.sh index 47012ce..c57d991 100755 --- a/lib/core.sh +++ b/lib/core.sh @@ -90,10 +90,7 @@ function _parse_args(){ esac done - COMMANDS=() - for arg in "$@"; do - COMMANDS+=("$arg") - done + COMMANDS=("$@") } ####################################### @@ -148,21 +145,20 @@ function _get_remote_command(){ local commands_opt="" [[ -z "${COMMANDS[@]}" ]] || commands_opt="-c \"${COMMANDS[@]}\"" - local cmd=" - [[ -e /etc/update-motd.d ]] && command -v run-parts &> /dev/null && run-parts /etc/update-motd.d/ - [[ -e /etc/motd ]] && $CAT /etc/motd; - [[ -d \"$GNUBIN\" ]] && PATH=\"$GNUBIN:\$PATH\"; - for tmp_dir in ${BASE_DIRS[@]}; do [[ -w \"\$tmp_dir\" ]] && { base_dir=\"\$tmp_dir\"; break; } done; - [[ -z \"\$base_dir\" ]] && { echo >&2 \"Could not find writable temp directory on the remote host. Aborting.\"; exit $NO_WRITABLE_DIRECTORY; }; - command -v $BASE64 >/dev/null 2>&1 || { echo >&2 \"kyrat requires $BASE64 command on the remote host. Aborting.\"; exit $NOT_EXISTING_COMMAND; }; - command -v $GUNZIP >/dev/null 2>&1 || { echo >&2 \"kyrat requires $GUNZIP command on the remote host. Aborting.\"; exit $NOT_EXISTING_COMMAND; }; - kyrat_home=\"\$(mktemp -d kyrat-XXXXX -p \"\$base_dir\")\"; - trap \"rm -rf \"\$kyrat_home\"; exit\" EXIT HUP INT QUIT PIPE TERM KILL; - [[ -e \${HOME}/.bashrc ]] && echo \"source \${HOME}/.bashrc\" > \"\${kyrat_home}/bashrc\"; - echo \"${rc_script}\" | $BASE64 -di | $GUNZIP >> \"\${kyrat_home}/bashrc\"; - echo \"${inputrc_script}\" | $BASE64 -di | $GUNZIP > \"\${kyrat_home}/inputrc\"; - echo \"${vimrc_script}\" | $BASE64 -di | $GUNZIP > \"\${kyrat_home}/vimrc\"; - VIMINIT=\"let \\\$MYVIMRC=\\\"\${kyrat_home}/vimrc\\\" | source \\\$MYVIMRC\" INPUTRC=\"\${kyrat_home}/inputrc\" $BASH --rcfile \"\${kyrat_home}/bashrc\" -i ${commands_opt}; - " - echo "$cmd" + $CAT < /dev/null && run-parts /etc/update-motd.d/ +[[ -e /etc/motd ]] && $CAT /etc/motd; +[[ -d "$GNUBIN" ]] && PATH="$GNUBIN:\$PATH"; +for tmp_dir in ${BASE_DIRS[@]}; do [[ -w "\$tmp_dir" ]] && { base_dir="\$tmp_dir"; break; } done; +[[ -z "\$base_dir" ]] && { echo >&2 "Could not find writable temp directory on the remote host. Aborting."; exit $NO_WRITABLE_DIRECTORY; }; +command -v $BASE64 >/dev/null 2>&1 || { echo >&2 "kyrat requires $BASE64 command on the remote host. Aborting."; exit $NOT_EXISTING_COMMAND; }; +command -v $GUNZIP >/dev/null 2>&1 || { echo >&2 "kyrat requires $GUNZIP command on the remote host. Aborting."; exit $NOT_EXISTING_COMMAND; }; +kyrat_home="\$(mktemp -d kyrat-XXXXX -p "\$base_dir")"; +trap "rm -rf "\$kyrat_home"; exit" EXIT HUP INT QUIT PIPE TERM KILL; +[[ -e \${HOME}/.bashrc ]] && echo "source \${HOME}/.bashrc" > "\${kyrat_home}/bashrc"; +echo "${rc_script}" | $BASE64 -di | $GUNZIP >> "\${kyrat_home}/bashrc"; +echo "${inputrc_script}" | $BASE64 -di | $GUNZIP > "\${kyrat_home}/inputrc"; +echo "${vimrc_script}" | $BASE64 -di | $GUNZIP > "\${kyrat_home}/vimrc"; +VIMINIT="let \\\$MYVIMRC=\\"\${kyrat_home}/vimrc\\" | source \\\$MYVIMRC" INPUTRC="\${kyrat_home}/inputrc" $BASH --rcfile "\${kyrat_home}/bashrc" -i ${commands_opt}; +EOF } diff --git a/tests/integ-tests/integ-tests.sh b/tests/integ-tests/integ-tests.sh index e653c53..8cb5974 100755 --- a/tests/integ-tests/integ-tests.sh +++ b/tests/integ-tests/integ-tests.sh @@ -9,7 +9,7 @@ sudo chsh -s $DEFAULT_SHELL $USER mkdir -p ~/.config/kyrat echo "alias q='exit'" > ~/.config/kyrat/bashrc -./bin/kyrat -o "StrictHostKeyChecking no" localhost -- echo \$SHELL +./bin/kyrat -v -o "StrictHostKeyChecking no" localhost -- echo \$SHELL ./bin/kyrat -v -o "StrictHostKeyChecking no" localhost -- q ./bin/kyrat -v -o "StrictHostKeyChecking no" localhost -- ls -lh ./bin/kyrat -v -o "StrictHostKeyChecking no" localhost -- [[ ! -z "\\\$INPUTRC" ]]