Created
March 21, 2024 00:28
-
-
Save soraxas/7dcc7b1918ac8df99f8e6ac3c9a2edcc to your computer and use it in GitHub Desktop.
This is a transparent wrapper that pretty much does nothing (aka it passthrough and eval all arguments). This is useful in-place of `sudo`, where you might have sudo-right (e.g. inside a container) but cannot perform `setuid` (because you are not actually sudo or using fakeroot as far as the host-machine's concern).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
requote_args() { | |
# Given a list of args, add quotes if necessary | |
C='' | |
for i in "$@"; do | |
case "$i" in | |
*[[:space:]]*|*'*'*) # contain space or literal astrisk | |
case "$i" in | |
*\'*) # escape single quote | |
i="$(printf "%s" "$i" | sed "s/'/'\"'\"'/g")" | |
;; | |
*) : ;; | |
esac | |
# quote only the parts after equal sign | |
# i.e. in the format of pre='echo foo && echo bar' | |
case "$i" in | |
*=*) # escape single quote | |
i="${i%%=*}='${i#*=}'" | |
;; | |
*) # normal case | |
i="'$i'" ;; | |
esac ;; | |
*) # no space, do nothing | |
: ;; | |
esac | |
if [ -z "$C" ]; then | |
C="$i" | |
else | |
C="$C $i" | |
fi | |
done | |
printf "%s" "$C" | |
} | |
eval "$(requote_args "$@")" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If this is save as
/usr/bin/sudo
, it will works withsudo echo Hi
as well as
sudo FOO=bar echo Hi
where the second one will fail if you don't do
eval
(and won't works correctly if you don't requote the arguments).