Page 1 of 1

Bash script will not exit

Posted: Fri Jan 22, 2016 1:24 pm
by utomsig
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

Re: Bash script will not exit

Posted: Fri Jan 22, 2016 1:47 pm
by elatllat
While starts a sub shell and you are only reading 1 lime anyway so just remove that unused loop.

Re: Bash script will not exit

Posted: Fri Jan 22, 2016 2:44 pm
by Mike_at_no27
#!/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

Re: Bash script will not exit

Posted: Fri Jan 22, 2016 4:19 pm
by utomsig
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!