-
Notifications
You must be signed in to change notification settings - Fork 0
/
nudo.sh
40 lines (37 loc) · 885 Bytes
/
nudo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
# Function to get the name of the user who invoked sudo
get_sudo_user() {
if [ -n "$SUDO_USER" ]; then
echo "$SUDO_USER"
elif [ -n "$LOGNAME" ]; then
echo "$LOGNAME"
else
echo "$USER"
fi
}
# Function to execute command as normal user
run_as_normal_user() {
local normal_user
normal_user=$(get_sudo_user)
if [ "$normal_user" = "root" ]; then
echo "Error: Unable to determine non-root user." >&2
exit 1
fi
su - "$normal_user" -c "$*"
}
# Main script logic
if [ "$EUID" -eq 0 ]; then
# If root, run command as normal user
if [ $# -eq 0 ]; then
echo "Usage: $0 <command>" >&2
exit 1
fi
run_as_normal_user "$*"
else
# If normal user, just execute the command
if [ $# -eq 0 ]; then
echo "Usage: $0 <command>" >&2
exit 1
fi
eval "$@"
fi