Ok. I'm trying to write a BASH script that will read a number from the filesystem (which will sometimes be there and sometimes not) every 3 seconds, to compare it to a list of numbers in a text file; I want it to execute a command if the value of the number in the filesystem DOES match one of the text file values, and a different command if the number DOESN'T match, but I also need for it to just start over and look for a number again if there is no number on the filesystem), and I want it to loop infinitely.
If that was too complicated, look at this diagram.
---------------------------------- | IS there an ID NUMBER PRESENT? | (There won't always be one) ---------------------------------- | | ----- ----- | Y | | N |-- Wait 3 seconds and recheck ----- ----- | ------------------------------------------------------ | Does it match one of the IDs in the ALLOWED list? | ------------------------------------------------------ | | ----- ----- | Y | | N |-- Echo that a failure ID was tried, then wait and recheck ----- ----- | Good, then echo that a good ID was used, then wait 3 seconds and recheck.
|
So you see why it loops infinitely.
This is what I have:
#!/bin/bash function buttonID { for i in $( ls /mnt/1wire/ | grep [0-9][0-9].* ); do WIREID=$i done } #The above function reads the device ID from /mnt/1wire/<device>
function busALLOW { for z in $( cat /root/allow.ib ); do ALLOW=$z done } #The above function reads the list of ALLOWed device IDs
#And this runs simple ECHO commands if the IDs are right or wrong #And should also start over if there is no value for "WIREID" function bus { if [ "$WIREID" = "$ALLOW" ]; then echo Open Door for `cat /root/users.ib | grep $WIREID` on `date -R`! sleep 3 echo Closing Door for `cat /root/users.ib | grep $WIREID` on `date -R`! sleep 3 unset WIREID buttonID loop elif [ "$WIREID" = "" ]; then sleep 3 unset WIREID buttonID loop else echo Hack Attempt by $WIREID on `date -R` sleep 3 unset WIREID buttonID loop fi }
#But there's either a problem here, or in how I tried to loop in function BUS #so eventually the value for "WIREID" sticks, but only for an ALLOWED ID function loop { bus }
#And this is the order that the program is started initially buttonID busALLOW bus #I didn't end with "exit", it just ends there
|
And this is why it doesn't work:
root@owfs:~ # ./ibcheck OK Open Door for KEY USER Primary Key OK Closing Door for KEY USER Primary Key <Key Removed> <Bad Key Used> ** FAIL Hack Attempt by BAD KEY <Key Removed> <Good Key Used> OK Open Door for KEY USER Primary Key OK Closing Door for KEY USER Primary Key <Key Removed> <Bad Key Used> ** FAIL Hack Attempt by BAD KEY <Key Removed> <Good Key Used> OK Open Door for KEY USER Primary Key OK Closing Door for KEY USER Primary Key <Key Removed> Open Door for KEY USER Primary Key Closing Door for KEY USER Primary Key Open Door for KEY USER Primary Key Closing Door for KEY USER Primary Key
|
As you see, there is likely a problem with how this program loops since it works fine many times before being "stuck"
I could, of course, get around this by just having it run every few seconds, or to have it launch itself back and forth, or come other ridiculous hack, but I don't want a ducttape solution, I want to know why I'm screwing up
.
Progressive Notes:Because of a suggestion that it was the infinite recursion of the program, I changed it to:
#!/bin/bash function buttonID { for i in $( ls /mnt/1wire/ | grep [0-9][0-9].* ); do WIREID=$i done } function ALLOWid { for z in $( cat /root/allow.ib ); do ALLOW=$z done } function bus { if [ "$WIREID" = "$ALLOW" ]; then echo Open Door for `cat /root/users.ib | grep $WIREID` on `date -R`! sleep 3 echo Closing Door for `cat /root/users.ib | grep $WIREID` on `date -R`! sleep 3 unset WIREID unset ALLOW elif [ "$WIREID" = "" ]; then sleep 3 unset WIREID unset ALLOW else echo Hack Attempt by $WIREID on `date -R` sleep 3 unset WIREID unset ALLOW fi } while [ 1 ] do buttonID ALLOWid bus done
|
But still have the same problem of it "sticking" on ON after awhile of testing good and bad keys, after the good key has been removed... My next idea is to try to finish a proper loop and see if I can get rid of the UNSETs...
Then, trying to get rid of the "functions";
#!/bin/bash while [ 1 ] do for i in $( ls /mnt/1wire/ | grep [0-9][0-9].* ); do WIREID=$i done for z in $( cat /root/allow.ib ); do ALLOW=$z done if [ "$WIREID" = "$ALLOW" ]; then echo Open Door for `cat /root/users.ib | grep $WIREID` on `date -R`! sleep 3 echo Closing Door for `cat /root/users.ib | grep $WIREID` on `date -R`! unset WIREID unset ALLOW elif [ "$WIREID" = "" ]; then sleep 3 else echo Hack Attempt by $WIREID on `date -R` sleep 3 unset WIREID unset ALLOW fi done
|
Doesn't seem to work either, always with the same problem.
Just to try to isolate the problem, the code is now:
#!/bin/bash function buttonID { for i in $( ls /mnt/1wire/ | grep [0-9][0-9].* ); do WIREID=$i done } function ALLOWid { for z in $( cat /root/allow.ib ); do ALLOW=$z done } function bus { if [ "$WIREID" = "$ALLOW" ]; then echo Open Door for `cat /root/users.ib | grep $WIREID` on `date -R`! sleep 3 echo Closing Door for `cat /root/users.ib | grep $WIREID` on `date -R`! else echo Hack Attempt by $WIREID on `date -R` sleep 3 fi } while [ 1 ] do unset WIREID unset ALLOW buttonID ALLOWid bus unset WIREID unset ALLOW done
|
And, with this code, it sticks "open" the second time the Good key is put on...