utomsig
Posts: 4
Joined: Thu Jan 21, 2016 10:29 am

Bash script will not exit

Fri Jan 22, 2016 1:24 pm

Hi! I have a problem with a bash script. I thought it would exit at a certain point, but it does not! It tries to check if the script has already started the last 60 s, and exit if it has. It will be executed by udev when a usb device is connected. But the udev starts the script about 10 times. So I need to check if it has been started recently.
Thanks!
Ps. Isn't it a clever script?! But sadly it's not mine. Ds.

Code: Select all

#!/bin/bash

sleep 1
cat /tmp/epochtimetesfile.txt | head -1 | while read TIMEBEFORE
do
#echo "TIME BEFORE IS=$TIMEBEFORE"
CURRDATE=`(date +'%Y-%m-%d %H:%M:%S UTC')`
EPOCHTIME=`(date --date="$CURRDATE" +%s)`
#echo "$EPOCHTIME-$TIMEBEFORE" 
DIFFTIME=`(echo "$EPOCHTIME-$TIMEBEFORE" | bc)`
#echo "Difftime=$DIFFTIME"
if [[ "$DIFFTIME" -lt "60" ]]
then
echo "START AGAIN , DIFF IS TOO SMALL"
exit
#                    <<< I think it should exit here!! But the script prints both "START AGAIN... and "end of script" at the end of the script :)
fi
done
#echo "TIME BEFORE IS=$TIMEBEFORE"
#Current full epoch date/time =
CURRDATE=`(date +'%Y-%m-%d %H:%M:%S UTC')`
#echo "$CURRDATE"
EPOCHTIME=`(date --date="$CURRDATE" +%s)`
#echo "New epoch time is entered"
echo "$EPOCHTIME" > /tmp/epochtimetesfile.txt
#date --date='1970-01-01 00:01:10 UTC' +%s
#echo "MOUNTING DRIVE"
echo end of script
exit

elatllat
Posts: 1337
Joined: Sat Dec 17, 2011 5:05 pm

Re: Bash script will not exit

Fri Jan 22, 2016 1:47 pm

While starts a sub shell and you are only reading 1 lime anyway so just remove that unused loop.
SBC with 32GB RAM: https://hardkernel.com

FAQ : https://raspberrypi.stackexchange.com

Unanswered: https://www.raspberrypi.org/forums/search.php?search_id=unanswered

Mike_at_no27
Posts: 1
Joined: Fri Jan 22, 2016 2:28 pm

Re: Bash script will not exit

Fri Jan 22, 2016 2:44 pm

#!/bin/bash

TMPFILE=/tmp/epochtimetesfile.txt

sleep 1

TIMEBEFORE=$( head $TMPFILE -n1 )
CURRDATE=`(date +'%Y-%m-%d %H:%M:%S UTC')`
EPOCHTIME=`(date --date="$CURRDATE" +%s)`
DIFFTIME=$(( $EPOCHTIME - $TIMEBEFORE ))

if [[ "$DIFFTIME" -lt "60" ]]
then
echo "START AGAIN , DIFF IS TOO SMALL"
else
echo "$EPOCHTIME" > $TMPFILE
#date --date='1970-01-01 00:01:10 UTC' +%s
echo "MOUNTING DRIVE"
# code here will only run if 1 minute or more has elapsed
#
fi

exit
#end of script

utomsig
Posts: 4
Joined: Thu Jan 21, 2016 10:29 am

Re: Bash script will not exit

Fri Jan 22, 2016 4:19 pm

Thank you elatllat and Mike! I had a suspicion about the while loop, but it was hard to get something out of google on the subject. Now I can continue!
Thanks again!

Return to “Other programming languages”