Search code examples
shraspberry-pi3raspbian

sh script gets stuck on read command in while loop


I'm trying to write a script that I'll put in my pi's cron to check for network connectivity every 10 seconds, if it fails a ping to google it will write a text file as false, then next time it succeeds, it will restart a program, because the specific program has issues with reconnecting to the network automatically.

The script seemed to be working when I was executing it from the terminal out of the same directory, then I cd back to / and added a bunch of comments, and now it just exits the script without any output, and for the life of me I can't figure out where i messed it up - I'm still relatively new to scripting so I could be missing something absolutely obvious here, but I couldn't find anything useful on google.

file heirarchy:
/home/pi/WEB_UI/
inside the WEB_UI folder are both of the scripts i'm running here.
nonet.sh - the script in question
pianobar.sh - a simple script to pkill a program and reload it after 5 seconds.
var.txt - a text file that will only ever contain "true" or "false

I've tried removing all of the comments, changing the file locations to ./ and making the while; do commands a single line, but I can't figure out where the issue is. if I run sh -x for the script, it returns:

    pi@raspberrypi:~/WEB_UI $ sh -x nonet.sh
    + ping -q -c 1 -W 1 google.com
    + read line

interestingly I get the same result from a test script I was using that was basically

"if var.txt says 'true', echo 'up', else echo 'down'" 
  • I wonder if something is wrong with my sh interpreter?
    #!/bin/sh

    #ping google, if successful return true
    if ping -q -c 1 -W 1 google.com >/dev/null; then
        #read variable line, perform action do
        while read line
        do
        #leading $ means return previous output. if line is false:
        if [ "$line" = "false" ]
        then
            #return network up text, run pianobar script, set var.txt to true.
            echo "the network is back up"
            sh /home/pi/WEB_UI/pianobar.sh
            echo true > /home/pi/WEB_UI/var.txt
        else
            #otherwise return network is up, set var.txt to true
            echo "the network is up"
            echo true > /home/pi/WEB_UI/var.txt
        #fi ends an if statement, done ends a while loop.
        #text after done tells the while loop where to get the line variable
        fi
        done < /home/pi/WEB_UI/var.txt
    else
        while read line
        do
        if [ "$line" = "false" ]
        then
            #if var.txt is already false, ping google again
            if ping -q -c 1 -W 1 google.com >/dev/null; then
                #if ping works, the network is back, restart pianobar, set var to true
                echo "the network is back up"
                sh /home/pi/WEB_UI/pianobar.sh
                echo true > /home/pi/WEB_UI/var.txt
            else
                #if var.txt is false, network is still down. wait.
                echo "the network is still down"
            fi
        else
            echo "the network is down"
            echo false > /home/pi/WEB_UI/var.txt
        fi
        done < /home/pi/WEB_UI/var.txt
    fi

the script SHOULD just echo a simple line saying whether the network is up, down, back up, or still down, depending on how many flags it passes/fails. Any assistance would be greatly appreciated!


Solution

  • as Shellter said in comments above, the issue was that I needed to add \n to the end of the line in my var.txt

    I think I saw another post recently where while read... was frustrated by a missing \n char, so maybe you want to do printf "false\n" > file instead. Good luck.