I searched for hours already, but can't find anything regarding this:
I have a bash script that tunnels via ssh from my PC to a Pi to test certain files and if certain conditions are met. I recently added a function which tests the BLE functionality of the Pi; for this I again tunnel from the first Pi to a second one and start a script on the second one that sends a BLE advertisement. Now I check if the first Pi recognizes the advertisement. The function looks like this:
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
}
This works fine, it starts the advertisement script on the second Pi, the first correctly detects the MAC and closes the "example-advertisement". The real problem: I want the function only executed under certain conditions.
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
The script just exits BEFORE the "echo" after the "fi". I'm 100% sure it's no syntax error with if and fi or something. It just stops at the last command of the if-commands. I also normally run many, many more functions which all get executed and the "if" properly exits after. The BLE funtion however is the only one starting a ssh connection.
I also used "jobs -l" which shows the PID of the ssh connection to the second Pi and says "exit", so it stopped and should be fine. Any ideas? Is it because I tunnel from an already open tunnel? Something something sub shell?
Btw. the function "func_return" just takes the exit code of the last executed command and puts out "OK" for 0 and "FAILED" for 1, it does nothing really.