Skip to main content
deleted 2 characters in body
Source Link
thanasisp
  • 8.5k
  • 2
  • 29
  • 40

For the general case you want to monitor a file, and send a desktop notification with the new file content only when the content has changed, you can use inotifywait (from inotify-tools) with the -m, --monitor option to execute indefinitely.

--format "%e" will print only the type of the event to the next command.

notify-send, from libnotify for desktop notifications, is being used to send the notification only if the file content is modified.

#!/bin/bash

f="file"f="filename"
curr="$curr=$(<"$f")"

inotifywait -m -e modify "$f" --format "%e" | while read -r event; do
    if [ "$event" == "MODIFY" ]; then
        prev="$curr"
        curr="$curr=$(<"$f")"
        [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    fi
done

For your specific case, I would not monitor changes to files, if your goal is to display a desktop notification with a text like "you are connected" or "you are disconnected". I would modify the place where you print that text (every N seconds as you say) into that file, to something like this:

while true; do
    prev="$curr"
    curr="$curr=$( <here you output the new text> )"
    [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    sleep 4
done

For the general case you want to monitor a file, and send a desktop notification with the new file content only when the content has changed, you can use inotifywait (from inotify-tools) with the -m, --monitor option to execute indefinitely.

--format "%e" will print only the type of the event to the next command.

notify-send, from libnotify for desktop notifications, is being used to send the notification only if the file content is modified.

#!/bin/bash

f="file"
curr="$(<"$f")"

inotifywait -m -e modify "$f" --format "%e" | while read -r event; do
    if [ "$event" == "MODIFY" ]; then
        prev="$curr"
        curr="$(<"$f")"
        [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    fi
done

For your specific case, I would not monitor changes to files, if your goal is to display a desktop notification with a text like "you are connected" or "you are disconnected". I would modify the place where you print that text (every N seconds as you say) into that file, to something like this:

while true; do
    prev="$curr"
    curr="$( <here you output the new text> )"
    [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    sleep 4
done

For the general case you want to monitor a file, and send a desktop notification with the new file content only when the content has changed, you can use inotifywait (from inotify-tools) with the -m, --monitor option to execute indefinitely.

--format "%e" will print only the type of the event to the next command.

notify-send, from libnotify for desktop notifications, is being used to send the notification only if the file content is modified.

#!/bin/bash

f="filename"
curr=$(<"$f")

inotifywait -m -e modify "$f" --format "%e" | while read -r event; do
    if [ "$event" == "MODIFY" ]; then
        prev="$curr"
        curr=$(<"$f")
        [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    fi
done

For your specific case, I would not monitor changes to files, if your goal is to display a desktop notification with a text like "you are connected" or "you are disconnected". I would modify the place where you print that text (every N seconds as you say) into that file, to something like this:

while true; do
    prev="$curr"
    curr=$( <here you output the new text> )
    [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    sleep 4
done
added 78 characters in body
Source Link
thanasisp
  • 8.5k
  • 2
  • 29
  • 40

For the general case you want to monitor a file, which is being frequently updated, and send a desktop notification with the new file content only when the content has changed, you can use inotifywait (from inotify-tools) with the -m, --monitor option to execute indefinitely.

--format "%e" will print only the type of the event to the next command.

notify-send, from libnotify for desktop notifications, is being used to send the notification only if the file content is modified.

#!/bin/bash

f="file"
curr="$(<"$f")"

inotifywait -m -e modify "$f" --format "%e" | while read -r event; do
    if [ "$event" == "MODIFY" ]; then
        prev="$curr"
        curr="$(<"$f")"
        [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    fi
done

For your specific case, I would not monitor changes to files, if your goal is to display a desktop notification, for anything with a text like "you are connected" or "you are disconnected". For this last output, you could use notify-send, which is a aprt of libnotify for desktop notifications. So,I would modify the place where you modifyprint that text (every N seconds as you say) into that file, could be modified to something like this:

while true; do
    prev="$curr"
    curr="$( <here you output the new text> )"
    [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    sleep 4
done

For the general case you want to monitor a file, which is being frequently updated, and send a desktop notification with the new file content only when the content has changed, you can use inotifywait (from inotify-tools) with the -m, --monitor option to execute indefinitely.

#!/bin/bash

f="file"
curr="$(<"$f")"

inotifywait -m -e modify "$f" --format "%e" | while read -r event; do
    if [ "$event" == "MODIFY" ]; then
        prev="$curr"
        curr="$(<"$f")"
        [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    fi
done

For your specific case, I would not monitor changes to files, if your goal is to display a desktop notification, for anything like "you are connected" or "you are disconnected". For this last output, you could use notify-send, which is a aprt of libnotify for desktop notifications. So, the place where you modify (every N seconds as you say) that file, could be modified to something like this:

while true; do
    prev="$curr"
    curr="$( <here you output the new text> )"
    [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    sleep 4
done

For the general case you want to monitor a file, and send a desktop notification with the new file content only when the content has changed, you can use inotifywait (from inotify-tools) with the -m, --monitor option to execute indefinitely.

--format "%e" will print only the type of the event to the next command.

notify-send, from libnotify for desktop notifications, is being used to send the notification only if the file content is modified.

#!/bin/bash

f="file"
curr="$(<"$f")"

inotifywait -m -e modify "$f" --format "%e" | while read -r event; do
    if [ "$event" == "MODIFY" ]; then
        prev="$curr"
        curr="$(<"$f")"
        [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    fi
done

For your specific case, I would not monitor changes to files, if your goal is to display a desktop notification with a text like "you are connected" or "you are disconnected". I would modify the place where you print that text (every N seconds as you say) into that file, to something like this:

while true; do
    prev="$curr"
    curr="$( <here you output the new text> )"
    [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    sleep 4
done
deleted 57 characters in body
Source Link
thanasisp
  • 8.5k
  • 2
  • 29
  • 40

Maybe you won't need to monitor changes to files, if your goal is to display a desktop notification. For this last output, you could use notify-send which I guess is standard and portable for all popular OSs and DEs, here I see it works for ubuntu which is similar to yours.

So, the place where you modify (every N seconds as you say) that file, could be like this:

while true; do
    prev="$curr"
    curr="$( <here you output the new text> )"
    [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    sleep 4
done

For the general case you want to monitor a file, which is being frequently updated, and send a desktop notification with the new file content only when the content has changed, you can use inotifywait (you can installfrom inotify-tools with your package manager if it's not available already) with the -m, --monitor option to execute indefinitely.

#!/bin/bash

f="file"
curr="$(<"$f")"

inotifywait -m -e modify "$f" --format "%e" | while read -r event; do
    if [ "$event" == "MODIFY" ]; then
        prev="$curr"
        curr="$(<"$f")"
        [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    fi
done

For your specific case, I would not monitor changes to files, if your goal is to display a desktop notification, for anything like "you are connected" or "you are disconnected". For this last output, you could use notify-send, which is a aprt of libnotify for desktop notifications. So, the place where you modify (every N seconds as you say) that file, could be modified to something like this:

while true; do
    prev="$curr"
    curr="$( <here you output the new text> )"
    [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    sleep 4
done

Maybe you won't need to monitor changes to files, if your goal is to display a desktop notification. For this last output, you could use notify-send which I guess is standard and portable for all popular OSs and DEs, here I see it works for ubuntu which is similar to yours.

So, the place where you modify (every N seconds as you say) that file, could be like this:

while true; do
    prev="$curr"
    curr="$( <here you output the new text> )"
    [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    sleep 4
done

For the general case you want to monitor a file, which is being frequently updated, and send a desktop notification with the new file content only when the content has changed, you can use inotifywait (you can install inotify-tools with your package manager if it's not available already) with the -m, --monitor option to execute indefinitely.

#!/bin/bash

f="file"
curr="$(<"$f")"

inotifywait -m -e modify "$f" --format "%e" | while read -r event; do
    if [ "$event" == "MODIFY" ]; then
        prev="$curr"
        curr="$(<"$f")"
        [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    fi
done

For the general case you want to monitor a file, which is being frequently updated, and send a desktop notification with the new file content only when the content has changed, you can use inotifywait (from inotify-tools) with the -m, --monitor option to execute indefinitely.

#!/bin/bash

f="file"
curr="$(<"$f")"

inotifywait -m -e modify "$f" --format "%e" | while read -r event; do
    if [ "$event" == "MODIFY" ]; then
        prev="$curr"
        curr="$(<"$f")"
        [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    fi
done

For your specific case, I would not monitor changes to files, if your goal is to display a desktop notification, for anything like "you are connected" or "you are disconnected". For this last output, you could use notify-send, which is a aprt of libnotify for desktop notifications. So, the place where you modify (every N seconds as you say) that file, could be modified to something like this:

while true; do
    prev="$curr"
    curr="$( <here you output the new text> )"
    [ "$curr" == "$prev" ] || notify-send "Title" "$curr"
    sleep 4
done
Source Link
thanasisp
  • 8.5k
  • 2
  • 29
  • 40
Loading