I have created a mini script to reboot my Raspberry Pi upon the push of a button. The script simply uses wiringPi (gpio command) to set pin 0 (pin 17 in Raspberry Pi standard numbering order) to input, and then reads the value until it's one (that's, when the button is pushed or held down).Here's my script:gpio mode 0 inwhile (true)do        if [ `gpio read 0` -eq 1 ]        then                echo password | sudo -S reboot                break        fidone &The script works fine and everything. However, for those of you not familiar with the Pi, it comes with very limited hardware resources (including 512 MB of memory) which can be easily consumed by a loop like the one I'm using.What I'm trying to achieve here is to find another way for bash to find out when the value has changed from 0 to 1 without having to dedicate a more like an unconditional loop for it. Is this doable? Please share your ideas. 
Optimizing a `while` loop
bash;shell script;raspberry pi
AnalysisThe script is a busy loop: it keeps reading the GPIO pins over and over. It doesn't consume much memory but it keeps the CPU busy.You should set the GPIO pin in edge mode. The WiringPi author is considering adding a wait command to the gpio utility but currently there is no way to react to an edge trigger from a shell script.A Python solutionThere is a Python library for GPIO access, which supports edge mode. Here's some completely untested Python code that should do what you want.#!/usr/bin/env pythonimport osfrom RPi import GPIOGPIO.wait_for_edge(0, GPIO.RISING)system(sudo reboot)Additional shell tips(true) could be written just true. The parentheses create a subprocess, which is completely unnecessary.`gpio read 0` should be in double quotes. Without quotes, the output of the command is treated as a list of file name wildcard patterns. With double quotes, the output of the command is treated as a string. Always put double quotes around command substitutions and variable substitutions: $(some_command), $some_variable. Also, you should use the syntax $() rather than ``: it has exactly the same meaning, but the backquote syntax has some parsing quirks when the command is complex. Thus: if [ $(gpio read 0) -eq 1 ]Don't put the root password in the script. If the script is running as root, you don't need sudo at all. If the script isn't running as root, then give the user running the script the permission to run sudo reboot without supplying a password. Run visudo and add the following line:userwhorunsthescript ALL = (root) NOPASSWD: /sbin/reboot Note that if there's an entry for the same user in the sudoers file that requires a password, the NOPASSWD entry must come after.Once you trigger a reboot, you don't need to break the loop, the system will stop anyway.If you decide to keep using this shell script, here's an improved version which only checks the button state every second. Note that since the pin is only read once per second, this means you need to keep the button pressed for at least one second to be sure that the event is picked up.gpio mode 0 inwhile sleep 1; do    if [ $(gpio read 0) -eq 1 ]; then        reboot    fidone &