Why does this GPIO command fail?

Viewed 142

Why do these GPIO commands not work as expected?

I am trying to export a GPIO using the following command:

sudo echo 4 > /sys/class/gpio/chip0/export

but I get:

-bash: /sys/class/gpio/chip0/export: cannot overwrite existing file

I also tried:

echo 17 | sudo tee export

The command displays:

17

But when I run:

ls

I see:

export  gpio.sh
4 Answers

In this case, the problem is that the sudo command applies only to echo and not to the > redirection, which is executed without administrator privileges. Additionally, the path /sys/class/gpio/chip0/export is incorrect. It should be written without /chip0/.

Another issue to correct is that tee export was written without the full path, resulting in the creation of a file named export in the current directory (which appears when running ls).

The correct command line is as follows:
echo 17 | sudo tee /sys/class/gpio/export

This way, the tee command receives administrator permissions to write directly to the system file.

This is an error similar to what happened in class a couple of sessions ago. The problem is that the directory path you are trying to access using chip0 cannot be used to perform the export, as it isn't used in that command structure.

The sudo command only grants elevated permissions to echo, so higher permissions won't be applied to the rest of the command after the > redirection operator.

In the command where you tried echo 17 | sudo tee export, you only wrote export without the rest of the path. This causes a text file to be created containing the output of export gpio.sh when you try running ls.

The correct syntax to use—removing chip0, setting permissions properly, and writing the full path—is:

echo 17 | sudo tee /sys/class/gpio/export

For the first case you should be inside the super user terminal, even if you use sudo when starting the command, the shell tries to perform the redirection > before executing sudo.

Try getting inside the super user terminal first
sudo su
Then
echo 4 > /sys/class/gpio/chip0/export

This should work.

For the second issue you should use the full path to export a GPIO pin even if you are inside the correct directory. Instead of

echo 17 | sudo tee export

Use

echo 17 | sudo tee /sys/class/gpio/export

If you use

ls

Then you should see the GPIO17 directory.

You can export a GPIO easily using a similar command, if you were to use:

sudo echo 16 >> export 

it wouldn't give you the "cannot overwrite existing file", because while it is true that “>” and “>>” both of these operators represent output redirection in Linux.
“>” truncates the file, or creates it if it doesn't exist and then writes which makes it easy to wipe out a file by accident. While, “>>” opens the file in append mode, with no truncation; so appending can't destroy existing content

As to the other command:

echo 17 | sudo tee export

This command reads its input, writes it to the file export, and also copies it to your terminal, that's why the command displays 17, but if gpio17 isn't listed maybe it is due to the fact that export is a relative path, tee would write to a file named export in your current directory. To be sure you'd need to run:

echo 17 | sudo tee /sys/class/gpio/export