首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >sh脚本在while循环中被读命令卡住。

sh脚本在while循环中被读命令卡住。
EN

Stack Overflow用户
提问于 2019-10-15 17:44:53
回答 1查看 488关注 0票数 1

我正在尝试写一个脚本,我将把它放入我的pi的cron中,每10秒检查一次网络连接情况,如果它对google的ping失败了,它会将一个文本文件写成false,然后下次它成功的时候,它将重新启动一个程序,因为特定的程序在自动重新连接网络时有问题。

当我在同一个目录下从终端执行脚本时,脚本似乎在工作,然后我cd返回/并添加了一堆注释,现在它只是退出脚本而没有任何输出,而对于我的一生,我无法弄清楚我在哪里搞砸了--我对脚本还比较陌生,所以我可能在这里遗漏了一些非常明显的东西,但是我在google上找不到任何有用的东西。

代码语言:javascript
运行
复制
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

我已经尝试删除所有的注释,将文件位置更改为./并创建while;do命令一行,但我不知道问题出在哪里。如果我为脚本运行sh -x,它将返回:

代码语言:javascript
运行
复制
    pi@raspberrypi:~/WEB_UI $ sh -x nonet.sh
    + ping -q -c 1 -W 1 google.com
    + read line

有趣的是,我从我使用的测试脚本中得到了相同的结果--基本上是

代码语言:javascript
运行
复制
"if var.txt says 'true', echo 'up', else echo 'down'" 

  • 我想知道我的sh解释器

是不是出了什么问题

代码语言:javascript
运行
复制
    #!/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

脚本应该只是重复一条简单的行,根据它传递/失败的标志数量,说明网络是向上的、向下的、向上的还是仍然向下的。如能提供任何帮助,将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-16 17:45:17

正如Shellter在上面的评论中所说的,问题是我需要在我的var.txt中的行尾加上\n

我想我最近看到了另一篇文章,在读的时候.对缺少的\n字符感到沮丧,所以您可能想要执行printf "false\n“>文件。祝你好运,

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58400052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档