The problem is this:
sudo echo 6 > /sys/class/gpio/export
This tells the shell to run “echo 6” in the context of super-user, after redirecting standard out to /sys/class/gpio/export…
But the shell is not root, so it can’t redirect to that file.
You need to either first “sudo -i” and then run the commands without sudo (so you have a root shell,) or run the entire shell expression as root.
sudo sh -c 'echo 6 > /sys/class/gpio/export
Or you can just bang the registers directly, using C/C++ code. There’s some code here to do that:
Note that this code is written as an example of how to talk to the GPIO hardware from user space; I haven’t tested it for this particular use case. But … it ought to work, perhaps with minimal changes?
Also, because it uses /dev/mem, it needs root.