As root, how do I read another user's environment variables?
+2
−0
I have a script that should be run using sudo, with the goal of "promoting" a script from a user-level installation to a system installation. Reproduced here:
Existing code
#!/bin/bash
if [[ $EUID -gt 0 ]]; then
echo "This script must run as root"
exit 1
fi
if [[ -z "${SUDO_USER-}" ]]; then
echo "Sudo user not found"
exit 1
fi
if [[ -z "${1-}" ]]; then
echo "Usage: sudo share-command SCRIPTNAME"
exit 1
fi
user_bin="$(getent passwd $SUDO_USER | cut -d: -f6)/.local/bin"
src="$user_bin/$1"
dst="/usr/local/bin/$1"
if [[ -f "$dst" ]]; then
echo "Already exists"
exit 1
fi
/usr/bin/install -m 755 "$src" "$dst"
Currently, it looks up the sudo user's home directory and looks in .local/bin relative to that.
I would like it to be able to find the script being installed by name.
I do not want to set the root user's PATH; I understand that this is insecure and there are protections against it. But given that the script is running as root, and I know who the sudo user is, it seems like I should be able to determine that user's PATH, and then iterate over it manually to look for the script.
Is this indeed possible? How?

1 comment thread