You're still using the
"Are we there yet? Are we there yet? ..." method instead of the
"Tell me when we're there" one.
If you do really want to do it this way, since you only care about shutdown, you don't need to store/check oldButtonState and can simplify it to:
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)
# endless loop until button is pressed
while GPIO.input(5):
time.sleep(.1)
# verify button state before switching off
if not GPIO.input(5):
subprocess.call("echo 1 > /sys/class/backlight/rpi_backlight/bl_power && shutdown -h now",
shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
The if verification is probably not necessary (depending on how Python works), but even so it acts as both a safety against unwanted shutdown (incase something goes wrong), and clarifies the intent slightly. (The event-driven/callback method would make it even clearer.)