Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

linux - 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 !

question from:https://stackoverflow.com/questions/65545621/problem-while-loop-to-compare-current-date-to-date-in-a-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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}"&

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...