1

I have a simple script I have written for a game. This script is called from ~/.profile in the game user's home directory, and is supposed to simulate a fake login that runs another script upon successfully comparing two variables. It worked for a little while and then abruptly stopped working. (Edit: When I mean it stopped working, I don't mean I changed something and it messed everything up, I mean it stopped working after not touching it for a night.)

#!/bin/bash

pause(){
  read -p "Place [CARD] on scanner and press [ENTER] key..."
}

init(){
        cat /etc/issue
        pause
        READ=`sudo ~/bin/Read.py`
        CARD=`cat ~/var/keycode.txt`
        if [ "$READ" == "$CARD" ]; then
                ~/bin/mmenu.sh
        else
                echo "Incorrect... Please try again"
        fi
}

while true
do
        init
done

What am I doing wrong? Thanks in advance.

Edit: The output of Read.py is 12345, and the output of cat keycode.txt is 12345 yet, it doesn't seem to be able to get them to compare correctly.

The script has been dumbed down, with Read.py not printing, just writing its contents to a file called rcard.txt, and both variables like this:

CARD=`cat ~/var/kcard.txt`
READ=`cat ~/var/rcard.txt`

Try as I might, even with [ "$READ" == "$CARD" ], it still fails the if statement. Even with the contents being the exact same. I really don't get it.

stelicho
  • 21
  • 8

2 Answers2

0

There are a few possible sources of error here, which depend on what your error message is (if you are even getting one). It's possible that sudo ~/bin/Read.py is returning more than one word of text, separated by spaces, which would cause an error like so:

$ foo=hello world
$ bash: world: command not found

This same error could also be occuring with cat ~/var/keycode.txt. To avoid this potential error, even if this is not the error you are receiving, I would wrap your backticks in quotation marks like so:

    READ="`sudo ~/bin/Read.py`"
    CARD="`cat ~/var/keycode.txt`"

If you are receiving no error message, but the code is not working when you have what should be correct input, I would check the files ~/bin/Read.py and ~/var/keycode.txt and see if you can identify your issue there - an inconsistency could be occurring in one of the two to cause an uncaught error.

If you can offer more clarification on how your program "abruptly stopped working," that would be useful.

  • The right-hand side of an assignment doesn't need quotes, as word splitting doesn't take place. It only needs quotes if you're directly assigning a string with spaces, but not if the space is within backticks. This works without problems: ``var=`echo one two`; echo "$var"``. – Benjamin W. Oct 22 '18 at 01:00
  • Thanks for your reply. There is not error, actually, there is no output from the Read.py. It loops through the else statement. Running Read.py does not return any errors. That being said I think you are right. I'm going to try something really quick and I'll be back. – stelicho Oct 22 '18 at 01:07
  • So yes, Read.py prints two lines of text, one for the UUID of the card, and one for the string written to the card. and keycode.txt reflects this now, and both backticks are wrapped in quotes. However, it still loops through the else statement. Do you have any other ideas or should I just use sed to cut it down to one line? – stelicho Oct 22 '18 at 01:23
  • Given the information you have, I'm out of ideas as to what's happening. I would try using `sed` to cut it down to just one line and see what happens... I'm sorry I don't have any other ideas. :( – Evan Keeton Oct 23 '18 at 02:24
0

I think it still loops in the else statement, because there are several lines to compare, and it won't be OK like this. In addition, you assignement may join some lines in once.

You can replace your if statement this way:

if [ $( sudo ~/bin/Read.py |grep -wc "$CARD" ) -ge 1 ]; then

This grep instruction with the -c option, will return the count of lines in "$CARD" mathing those returned by your Read.py execution. Thus, if there is at least one line matching, you will loop in the then statement like you want.

Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
  • Thank you for your reply and idea. However, I had added an edit to the end of the question; I scaled back the output to just one line, and tried to work through a file to get around the execution of the .py script. However, it seems to think that the two files do not match, even when one is just a copy of the other. I'm starting to wonder if its a location issue or something else with bash. It might still be UK instead of US – stelicho Oct 23 '18 at 16:00
  • I think you can't use the == operator like this, on a multiple-lines file. I'm going to try it. Anyway, for sustainable system, I recommend using **grep** like I show you. – Bsquare ℬℬ Oct 24 '18 at 07:17
  • Hum, I just tried a little script, and it is working fine, even with multiple lines: #!/bin/bash CARD=`cat /tmp/readpy` READ=`cat /tmp/readpy` if [[ "$READ" == "$CARD" ]]; then echo "matching" else echo "KO" fi What is your GNU/Bash version? – Bsquare ℬℬ Oct 24 '18 at 07:34
  • Can you try, setting READ with exactly the same instruction than CARD? In addition, can you give output of: **diff ~/var/kcard.txt ~/var/rcard.txt** – Bsquare ℬℬ Oct 24 '18 at 07:36
  • Sorry I walked away for a couple of days. I will try that. – stelicho Oct 25 '18 at 22:26
  • diff kcard rcard says the files are the same, which I had tried before, but gave it another shot just in case, also cp kcard rcard doesnt work. bash version is 4.4.12. And to be clear it had already been reduced to a single line before you answered. I am however going to agree that maybe something is wrong with bash or bash's configuration somewhere. I'm going to try something. – stelicho Oct 26 '18 at 04:44
  • Is there a shell that matches bash syntax like above that I could try? A reinstall of bash, zsh, and dash dont seem to help. I can confirm that this works in bash in macOS, though without the python script – stelicho Oct 28 '18 at 16:15
  • I don't think you need to reinstall your Shell. Did you try that? **CARD=cat /tmp/readpy; READ=cat /tmp/readpy** – Bsquare ℬℬ Oct 28 '18 at 16:32
  • @stelicho On Stackoverflow you can give [up-vote](https://stackoverflow.com/help/privileges/vote-up) to people's helpful answers to thank them and select any one of the answer as [correct answer](https://stackoverflow.com/help/someone-answers) too out of all. – Bsquare ℬℬ Nov 07 '19 at 07:48