RESOLVED, Updated script added - Thank you both @steeldriver and @user10489
Shell printout is out of order and missing some info.
I'm brand spanking new to linux, and I'm still at the copy/paste and see what happens phase.
I've already tried google searching to no avail.
I'm running a script which employs multiple ways of returning the current user.
The script uses
- direct calls
- calls using
sudo - calls using
sudo su -
Each method should return 7 values, instead the first and second methods return 5 values, and the third method returns all 7.
The script in question:
#!/usr/bin/env bash
echo ""
echo "GET USER - DIRECT"
echo ""
whoami
echo $USER
id -u -n
logname
ps -o user= -p $$ | awk '{print $1}'
echo $SUDO_UID
echo $SUDO_USER
echo ""
read -p "Press enter to continue"
echo -e "\n"
echo "GET USER - SUDO"
echo ""
sudo whoami
sudo echo $USER
sudo id -u -n
sudo logname
sudo ps -o user= -p $$ | awk '{print $1}'
sudo echo $SUDO_UID
sudo echo $SUDO_USER
echo ""
read -p "Press enter to continue"
echo -e "\n"
echo "GET USER - SUDO SU -"
echo ""
sudo su -c 'whoami; echo $USER; id -u -n; logname; ps -o user= -p $$ | awk "{print $1}"; echo $SUDO_UID; echo $SUDO_USER; echo ""'
echo -e "\n"
The output I get:
player1@Arcade:~/Desktop$ ./testuser.sh
GET USER - DIRECT
player1
player1
player1
player1
player1
Press enter to continue
GET USER - SUDO
[sudo] password for player1:
root
player1
root
player1
player1
Press enter to continue
GET USER - SUDO SU -
root
root
root
player1
root
1000
player1
What am I missing here? My assumption is that the direct calling of $SUDO_UID and $SUDO_USER is permissions related. The order in which the printout is happening still puzzles me though.
EDIT:
understanding the use of sh -c, proper quotation, and access to $SUDO_xxxxx variables did the trick.
Also, I cant count correctly. The script aims to return 7 values, not 6.
The updated script is now working as one could expect.
Thank you both for your help!
my updated script:
#!/usr/bin/env bash
echo ""
echo "GET USER - DIRECT"
echo ""
whoami
echo $USER
id -u -n
logname
ps -o user= -p $$ | awk '{print $1}'
sh -c 'echo $SUDO_UID'
sh -c 'echo $SUDO_USER'
echo ""
read -p "Press enter to continue"
echo -e "\n"
echo "GET USER - SUDO"
echo ""
sudo whoami
sudo echo $USER
sudo id -u -n
sudo logname
sudo ps -o user= -p $$ | awk '{print $1}'
sudo sh -c 'echo $SUDO_UID'
sudo sh -c 'echo $SUDO_USER'
echo ""
read -p "Press enter to continue"
echo -e "\n"
echo "GET USER - SUDO SU -"
echo ""
sudo su -c 'whoami'
sudo su -c 'echo $USER'
sudo su -c 'id -u -n'
sudo su -c 'logname'
sudo su -c 'ps -o user= -p $$ | awk '"'"'{print $1}'"'"
sudo su -c 'sh -c '"'"'echo $SUDO_UID'"'"
sudo su -c 'sh -c '"'"'echo $SUDO_USER'"'"
echo -e "\n\n"
my updated output:
player1@Arcade:~/Desktop$ ./testuser.sh
GET USER - DIRECT
player1
player1
player1
player1
player1
Press enter to continue
GET USER - SUDO
[sudo] password for player1:
root
player1
root
player1
player1
1000
player1
Press enter to continue
GET USER - SUDO SU -
root
root
root
player1
root
1000
player1