You are not logged in.
I have some installation-related script that will be run by root or by someone else already in the sudo group.
How should I code commands within the script ?
sudo ./whatever; ### needed if run by someone else; OK
sudo ./whatever; ### when run by root is a no-go; eg: sudo ls manually entered on tty0 shows no output
Of course I can use if then ... fi and place both commands within those statements but ... it is a horrible approach.
Last edited by dawnofman (2022-06-17 21:48:37)
Offline
sudo ./whatever; ### when run by root is a no-go; eg: sudo ls manually entered on tty0 shows no output
Makes no sense at all. "sudo ls" as root will not show any output if $PWD is an empty directory - maybe try "# sudo ls /etc"
Then be less generic and present the actual problem, the current description provides a false premise.
Offline
sudo ./whatever; ### when run by root is a no-go; eg: sudo ls manually entered on tty0 shows no output
Makes no sense at all. "sudo ls" as root will not show any output if $PWD is an empty directory - maybe try "# sudo ls /etc"
Then be less generic and present the actual problem, the current description provides a false premise.
My fault; wrong example lead me to wrong conclusion: I used the sudo ls under root as a quick check -it was the first time I tried to use sudo under root; and this was because it was the first time I executed a script that was always used by someone else which already had all commands prefixed with sudo as necessary. I do not want to run sudo ls; I just want my script to be able to deal with both cases; eg: sudo sgdisk ... runs as expected when executed by someone else but then I guessed if executed by root sudo sgdisk --print /dev/sda will fail/whatever; and no; it does not fails. That's was the issue and the wrong conclusion. Sorry.
Offline
As the first line of the script:
[ $(id -u) -eq 0 ] || exec sudo $0 $@Last edited by Trilby (2022-06-17 21:09:32)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
sudo ./whatever; ### when run by root is a no-go; eg: sudo ls manually entered on tty0 shows no output
Makes no sense at all. "sudo ls" as root will not show any output if $PWD is an empty directory - maybe try "# sudo ls /etc"
Then be less generic and present the actual problem, the current description provides a false premise.
On second thought I think maybe I am using the wrong approach; will
sudo ./script;execute everything within it as sudo ?
Offline
Unless something inside "script" drops privileges, it'll run as root, yes.
Offline
Unless something inside "script" drops privileges, it'll run as root, yes.
Gotcha. I should have tested this before asking. My fault. Thanks for the replies.
Offline