Reputation: 55
I'm trying to do something really simple: Give myself the ability to choose what to run on boot. This is on a Raspberry Pi so sometimes it will be headless, sometimes it won't. I thought a simple approach would just be to run a little script on boot (don't even know if this will work - haven't gotten that far yet) that either lets me mash the keyboard and start up the GUI or just time out and start a python script. But yeah, I'm failing at this simple task. The if statement always evaluates to true no matter what I try, and I've tried many, many things. Here is the iteration I want to use...
If I let it time out, the return value of the read command should be greater than 128. If I press something the return value should be zero. So...
#!/bin/bash
# this gives the user the chance to startx or leave it alone and let camera.py run
# we will give the user 5 seconds to make a choice
/bin/echo "Hit any key to startx, otherwise I will start camera.py."
if [[ "$(read -t 5 -n 1)" -eq 0 ]]; then
/bin/echo "You want to startx!"
else
/bin/echo "You want to start the python script!"
fi
The result I get is that it always evaluates to true. What am I doing wrong?
Upvotes: 2
Views: 105
Reputation: 113814
Replace:
if [[ "$(read -t 5 -n 1)" -eq 0 ]]; then
With:
if read -t 5 -n 1; then
If you type a character, read
returns and exit code of zero (true). If read
reaches end-of-file or it times out, it returns an exit code greater than zero (false). if
responds to exit codes. There is no need for any test ([[
) commands.
Upvotes: 1