You are not logged in.

#1 2022-06-17 20:38:00

dawnofman
Member
Registered: 2019-07-26
Posts: 140

[solved]sudo within script to be run by someone ... and root; how to ?

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

#2 2022-06-17 20:48:38

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,573

Re: [solved]sudo within script to be run by someone ... and root; how to ?

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

#3 2022-06-17 21:01:22

dawnofman
Member
Registered: 2019-07-26
Posts: 140

Re: [solved]sudo within script to be run by someone ... and root; how to ?

seth wrote:

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

#4 2022-06-17 21:06:22

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,480
Website

Re: [solved]sudo within script to be run by someone ... and root; how to ?

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

#5 2022-06-17 21:10:15

dawnofman
Member
Registered: 2019-07-26
Posts: 140

Re: [solved]sudo within script to be run by someone ... and root; how to ?

seth wrote:

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

#6 2022-06-17 21:29:48

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,573

Re: [solved]sudo within script to be run by someone ... and root; how to ?

Unless something inside "script" drops privileges, it'll run as root, yes.

Offline

#7 2022-06-17 21:47:30

dawnofman
Member
Registered: 2019-07-26
Posts: 140

Re: [solved]sudo within script to be run by someone ... and root; how to ?

seth wrote:

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

Board footer

Powered by FluxBB