Topic: Bash at your fingertips: nifty notify-send
First the How, then the What and the Why:
How:
For the impatient:
Copy
#!/bin/bash
info=$(eval "$*" 2>/dev/null)
notify-send -t $((1000+300*`echo -n $info | wc -w`)) -u low -i gtk-dialog-info "$*" "$info" || exit 2into a text editor, then save it with a suitable name, say send.sh or note.sh. (I've named it "send", for reasons explained below.)
Make this script executable ('chmod +x send').
It also helps to put it somewhere in your $PATH. (~/bin, in my case)
Usage:
Hit Alt-F2, type in:
send your-bash-command-hereWhat:
A trivial script to run a Bash command and display the output as a Gnome notification. The time for which the notification stays is proportional to the length of the displayed output.
A typical example:
send acpi -VFor a hack this simple, it is more versatile than you may think; examples at the end of this post illustrate.
If you're turned off by the idea for some reason, do glance through the possible improvements listed at the end.
Why:
I'm a terminal junkie- I tend to gyrate towards CLI applications and commands whenever given a graphical option. Despite this, the following situation holds:
1. I spend most of my PC time in front of a (graphical flash/java enabled) browser or an IM window, or at a text editor. None of these offer easy access to terminal commands.
2. When I do need to verify or find some info at the command line, I'm forced to open a terminal emulator, enter the command, read the output (and possibly append to a file or copy to clipboard) and exit the terminal emulator.
There is thus no easy access to run non-interactive commands. Launching a terminal emulator is painful; Terminator takes about three seconds to launch on my semi-old hardware.
Several partial workarounds exist. For instance:
1. Amp up gmrun to run terminal commands. This doesn't work with non-interactive commands like 'ls'- the launched terminal quits immediately after ls exits, forcing me to type in 'ls && cat' and exiting the terminal with ^D after reading the output.
2. Use Conky to display all aspects of hardware status, frequently used files, etc. Apart from bloating up Conky, this fails to achieve the kind of utility required- see examples at the end.
3. Use Yakuake, Tilda, Guake or similar "roll-down" terminals. These eat into system resources, add bloat to #!, and finally, the temptation is strong to run interactive or 'persistent' commands (like 'tail -f' and 'htop') that effectively deny you Bash access.
4. Use Vim or Emacs. Both provide easy shell access from within the editor environment. Not a universal solution.
Hence this script.
What exactly it does is provide a querying system for your frequent needs. You ask Bash for information, it sends it across to you without interrupting your work-flow or requiring a special application. Also, it lingers exactly long enough for you to read it, so it takes away the tedium of dispensing with a window you no longer need.
Most of the script is self-explanatory. The $((1000+300*`echo -n $info | wc -w`)) part specifies the duration for which the notification should last in milliseconds. I assume the average reader takes 300 ms to read a word (180 words per minute) and have added an additional 1 second as a buffer. (Change the numbers to ones that match your reading speed.)
Examples:
It works especially well with scripts you already have. For instance, I have two Python scripts ("define" and "gcalc") that query Google for definitions of words/phrases and act as a front-end for Google Calculator, respectively. Combined with "send", these produce fantastic results:
send define rambunctioussend gcalc "24500 INR in USD"send gcalc "4/3 \* pi \* radius of sun^3 in km^3"Some other nifty uses:
send df / -Hsend mocp --infosend "ls * | grep '\.sh$' "
Note that typing in 'send ls | grep \.sh$' (without the double quotes) will not work- Bash will end up evaluating 'send ls' and piping the output to 'grep \.sh$'.
send echo 'Go to bed!' | at midnightIn this case, the quotes have been left out- because the intended behaviour is indeed what Bash will do anyway.
I suppose it's easy to find more examples.
Possible Issues & Improvements:
1. Having to type Alt-F2 and then enter the 'send' command is still too much work for us lazy ones. Perhaps someone can whip up a quick Zenity app that can be bound to a key-combination? The problem with this is that Zenity does not autocomplete entries like Gmrun (or whatever application launcher you use) does.
2. Better unicode support. Notifications fail to show if the output text contains non-ASCII characters.
3. I haven't tested this on the new notification system in use in Ubuntu 9.04 and variants. (I'm running 8.10). Let me know if it works!
4. Since the script is just two lines, it might be a good idea to define it as a function in ~/.bashrc instead of a script, assuming every system you use will run the Gnome notification daemon. It certainly beats mucking round with $PATH.
Any feedback, suggestion or improvement is welcome.
Last edited by karthik (2009-05-21 10:13:34)