0

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

1 Answers1

1

There are two issues here:

  • The $ strings are not quoted correctly, so are substituted early before they are passed to sudo. If you don't want them replaced early, quote them with single quotes or backslash quote them. That quoting will be stripped out and then the $ will be passed along to the following command.
  • The $ substitution is done by a shell...somewhere. So if you run the command without a shell (i.e., directly by sudo) then it will just echo the raw $var. The sh -c provides that second shell.

For example:

$ echo $$
5665

This is from my original shell

$ sudo echo $$
5665

...original shell did the substitution even though sudo ran echo.

$ sudo 'echo $$'
sudo: echo $$: command not found

No shell -- the command was not parsed, so sudo tried to find echo $$ as an executable (with the space in it).

$ sudo echo '$$'
$$

No substitution was done...the raw $$ was printed.

$ sudo sh -c 'echo $$'
5680

New shell with new pid did the command parsing and substitution

$ echo $$
5665

and the original shell pid is still the same

The article referenced in the comment ("Why doesn't $HOME change if I use sudo?") is a separate but related problem. Many of the $ variables are not set by the shell but come from the environment, and sudo selectively passes through some of those unchanged, selectively filters others, and selectively changes others, depending on configuration files and command line options.

You seem to be confused where some of your commands are getting their information.

$$ is a local shell constant, subsituted with the pid of the "current" shell

The man page for logname doesn't say where it gets its info, but I'd guess it gets it from who is logged into the current terminal.

$SUDO_UID and $SUDO_USER are environment variables specifically set by sudo to the user who invoked sudo, not the user sudo is switching to; this is explicitly stated in the sudo man page, so it shouldn't be confusing.

user10489
  • 5,533