Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • Note that the script in the question is executed with /bin/sh as the shell. If that shell does not set the USER variable, Tim's code would call id to figure out the username. Under what circumstances would logname get the username right and id get it wrong? Would the shell need to set USER to the wrong username for that to be the case? What do you mean by "not work"? Are you expecting the user to be reported as root or the invoking user in your example? Commented Jun 25, 2022 at 17:55
  • Sorry for the tone in my last comment (I just re-read it). What I was fishing for is merely an explanation of the problem that you see and an explanation of your solution, taking into account that since you refer to Tim's answer, you would need to clarify the circumstances that his answer gets it wrong and what "wrong" actually means. Commented Jun 25, 2022 at 18:15
  • 1
    @Kusalananda: The explanation is right there in the first paragraph. OK, yes, the script in the question contains multiple sudo commands, so it’s ‘ ‘clearly’ ’ meant to be run by a non-root user. But what if somebody inadvertently runs the script itself under sudo? Then the id command (as used by Tim Cutts) would return “root” and not, as (presumably) desired, the name of the logged-in user. Commented Jun 25, 2022 at 22:40
  • @Kusalananda: OK, now it’s my turn to re-read and re-write/clarify.  Hogcryat’s answer (above) links to Eric James Deiter’s answer, and the explanation is in the first paragraph of Eric’s answer. Commented Jun 26, 2022 at 2:08
  • @Kusalananda it is just as Reinstate Monica says. It is an improvement to Eric's answer in the case the script was called with sudo (which I know is not the question's original intention). I would have liked to comment under Eric's answer but I don't have enough reputation. Commented Jun 26, 2022 at 18:37