sudo su -
.
It also requires multiple checks - if
Instead of the command
This gives you the username that logged in to the session.
These work regardless of
#----------------------------------------------------------
#Ta reda på vem du är
function findUser() {
thisPID=$$
origUser=$(whoami)
thisUser=$origUser
while [ "$thisUser" = "$origUser" ]
do
ARR=($(ps h -p$thisPID -ouser,ppid;))
thisUser="${ARR[0]}"
myPPid="${ARR[1]}"
thisPID=$myPPid
done
getent passwd "$thisUser" | cut -d: -f1
}
user=$(findUser)
echo "logged in: $user"
#----------------------------------------------------------
$USER == 'root'
then get $SUDO_USER
.Instead of the command
whoami
use who am i
. This runs the who
command filtered for the current session. It gives you more info than you need. So, do this to get just the user:who am i | awk '{print $1}'
Alternatively (and simpler) you can use logname
. It does the same thing as the above statement.This gives you the username that logged in to the session.
These work regardless of
sudo
or sudo su [whatever]
. It also works regardless of how many times su
and sudo
are called.#----------------------------------------------------------
#Ta reda på vem du är
function findUser() {
thisPID=$$
origUser=$(whoami)
thisUser=$origUser
while [ "$thisUser" = "$origUser" ]
do
ARR=($(ps h -p$thisPID -ouser,ppid;))
thisUser="${ARR[0]}"
myPPid="${ARR[1]}"
thisPID=$myPPid
done
getent passwd "$thisUser" | cut -d: -f1
}
user=$(findUser)
echo "logged in: $user"
#----------------------------------------------------------
Inga kommentarer:
Skicka en kommentar