Bash脚本在if条件下退出无法解释

时间:2018-07-20 14:18:12

标签: bash shell ssh raspberry-pi3 subshell

我已经搜索了几个小时,但是找不到与此有关的任何内容:

我有一个bash脚本,该脚本通过ssh从PC传输到Pi,以测试某些文件以及是否满足某些条件。我最近添加了一个功能,用于测试Pi的BLE功能;为此,我再次从第一个Pi隧道到第二个Pi,并在第二个Pi上启动发送BLE公告的脚本。现在,我检查第一个Pi是否识别出该广告。该函数如下所示:

function BLE_scanning {
echo "Connecting to RPI BLE advertiser..."
#starting RPI BLE advertisement
if ssh -q root@$RPIP exit
then
    func_return $? && echo "Connected to RPI"
ssh root@$RPIP './bluez-5.43/test/example-advertisement' &
PID=$(ps -ef|pgrep -f "ssh root@$RBIP")
else
    func_return $? && echo "Can't connect to RPI. Check the device. Tests will fail."
fi
#searching for RPI BLE advertisement
echo "Searching BLE advertisement..."
if timeout 30s grep -m 1 "entry $RPMAC" <(tail -f $LOG)
then
    func_return $? && echo "Advertisement found from right MAC $RPMAC"
    sleep 1
    #added this to kill the ssh connection to the 2nd Pi, which didn't change anything
    kill -15 $PID
    #killing the advertisement on the 2nd Pi, which works perfectly
    ssh root@$RPIP 'killall -15 example-advertisement'
else
    func_return $? && echo "No advertisement found"
    sleep 1
    kill -15 $PID
    ssh root@$RPIP 'killall -15 example-advertisement'
fi
}

这很好,它在第二个Pi上启动广告脚本,第一个Pi正确检测到MAC并关闭“ example-advertisement”。真正的问题:我只希望函数在特定条件下执行。

REBOOT="path/to/a/created/flagfile"
if [ ! -f $REBOOT ]
then
    BLE_scanning
    echo "This gets correctly echoed after the function executed."
    echo "I can add as many commands after the function as I want."
fi
echo "But THIS echo just doesn't get executed, no matter what." 
exit 0

脚本仅在“ fi”之后的“ echo”之前退出。我100%肯定if和fi或其他内容没有语法错误。它只是在if命令的最后一个命令处停止。我通常还会运行许多其他函数,所有这些函数都会执行,然后“ if”会正确退出。但是,BLE功能是唯一启动ssh连接的功能。

我还使用了“ jobs -l”,它显示了到第二个Pi的ssh连接的PID,并说“ exit”,因此它停止了并且应该没问题。有任何想法吗?是因为我是从已经开放的隧道中挖出来的吗?子外壳有东西吗?

顺便说一句。函数“ func_return”仅获取最后执行的命令的退出代码,将“ OK”表示为0,将“ FAILED”表示为1,则实际上不执行任何操作。

0 个答案:

没有答案