From 9c12102d0bb4ca5d1e002e6e6e09813ff9bf4412 Mon Sep 17 00:00:00 2001 From: Dev & Sec Date: Sun, 8 Nov 2015 10:26:15 +0000 Subject: [PATCH] use `command -v` instead of `type`, and add comments for the `su_sudo` function --- letsencrypt-auto | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/letsencrypt-auto b/letsencrypt-auto index 8626ab329..ce58488c4 100755 --- a/letsencrypt-auto +++ b/letsencrypt-auto @@ -14,11 +14,24 @@ VENV_PATH=${VENV_PATH:-"$XDG_DATA_HOME/$VENV_NAME"} VENV_BIN=${VENV_PATH}/bin if test "`id -u`" -ne "0" ; then - if type sudo 1>/dev/null 2>&1; then + if command -v sudo 1>/dev/null 2>&1; then SUDO=sudo else + # `sudo` command does not exist, use `su` instead. + # Because the parameters in `su -c` has to be a string, + # we need properly escape it su_sudo() { args="" + # This `while` loop iterates over all parameters given to this function. + # For each parameter, all `'` will be replace by `'"'"'`, and the escaped string + # will be wrap in a pair of `'`, then append to `$args` string + # For example, `echo "It's only 1\$\!"` will be escaped to: + # 'echo' 'It'"'"'s only 1$!' + # │ │└┼┘│ + # │ │ │ └── `'s only 1$!'` the literal string + # │ │ └── `\"'\"` is a single quote (as a string) + # │ └── `'It'`, to be concatenated with the strings followed it + # └── `echo` wrapped in a pair of `'`, it's totally fine for the shell command itself while [ $# -ne 0 ]; do args="$args'$(printf "%s" "$1" | sed -e "s/'/'\"'\"'/g")' " shift