2

I am running a Ubuntu system on my Radxa Zero 3E (single board computer)

Using its GPIO I am driving a 5V LED via an NPN transisitor. Everything works.

For switching it on I use gpioset $(gpiofind PIN_36)=1 & for off gpioset $(gpiofind PIN_36)=0

My question is I cannot run above commands as a single line like sudo gpioset $(gpiofind PIN_36)=0 I must first move to root terminal via sudo su & then commands work perfectly.

The other alternative is that I use x2 separate sudo one liners first for gpiofind & then for gpioset. Both Binaries are located in /usr/bin

3
  • What happens when yo try? I have never used GPIO for anything, so I don't know those tools, but a guess would be that gpiofind expects to be run as root, and your sudo invocation doesn't. Commented Oct 13 at 11:56
  • So how do we run a command with x2 sudo commands as a one-liner? Commented Oct 13 at 12:05
  • @AndrewHenle I deleted our exchange since it really doesn't belong in the comments (my bad, I started it) but I would be more than happy to debate you on the (de)merits of sudo su if you want to ping me in /dev/chat. Commented Oct 14 at 13:18

1 Answer 1

7

As I inferred from OP's follow-up to my comment that my guess is probably right, I'm posting this as an answer.

The problem here is that in sudo gpioset $(gpiofind PIN_36)=0, only gpioset is executed as root. The shell evaluates the substitution by executing gpiofind PIN_36 before executing the surrounding command, so that is executed as whoever started the shell, which is also why it works in a shell started by root.

The fix is just to add sudo in front of the "inner" command, so the command becomes sudo gpioset $(sudo gpiofind PIN_36)=0.

2
  • 3
    Or sudo 'sh -c gpioset $(gpiofind PIN_36)=0'. Commented Oct 13 at 12:26
  • If the user is allowed to execute sh with sudo (as OP can do sudo su, he probably is) that will work, but in the case where strict permissions are applied to sudo, it's best not to rely on more commands. (you can of course say that I also add gpiofind to the list, but I'm assuming that would be on, in that case. Commented Oct 13 at 13:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.