Skip to content
Advertisement

Problem while loop to compare current date to date in a file

I want to compare current date to date present in file [every 30 seconds and in background] I don’t know if I have to do an infinite loop or a while read line loop … So, every match of current date with a date of a file, I print the current line matched in xmessage. ( I add a date using ./script.sh 0138 test ) The date is 0138 in this form : +%H%M.

I already tried this but my loop works only one time for the first date in file. Only one window of xmessage is opened… How can this work for each date added ?

while true
do
    currentDate="$(date +%H%M)"  
    futureDate="$(cat test.txt | cut -d ' ' -f 1 | grep "${currentDate}" | head -n 1)"
    printLine="$(cat test.txt | grep "${currentDate}")"

    if test "${currentDate}" = "${futureDate}"
    then
        xmessage "${printLine}" -buttons Ok -geometry 400x100 -center
    fi
    sleep 30
done &

Exemple of file : test.txt

0142 test xmessage
0143 test other xmessage
0144 other test
0145 other xmessage !

Thanks for helping me !

Advertisement

Answer

Your main problem is that the execution halts until you “confirm” the message by clicking OK.

You could work around that by detaching xmessage (put an ampersand at the end of the line:

xmessage -buttons Ok -geometry 400x100 -center "${printLine}"&
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement