I'm writing a bash script that uses notify-send (from the notify-osd package) to show popup notifications to the user, e.g. if there is an error during runtime. These popups can be decorated with icons, like so:
notify-send -i $icon $headline $message
In order to make my script as lean as possible, I want to use the default icons that come with the system. So far, I have resorted to hard-coded filenames, like so:
icon="/usr/share/icons/Mint-Y/status/48/dialog-error.png"
This is obviously a pretty bad solution, since the icon depends on a theme that may not be installed on a given system (in the example above, it's the "Mint Y" theme). I'm looking for a way to tell my script: "Ask the system for the error icon in the currently-set icon theme, and use that."
The question is: how do I do that?
Don't specify anything like a path. You should be specifying a generic filename fragment, and letting the system search for the best value for you.
So, if there is a size or filetype or something in the filename, drop it, and do not specify the leading path.
Use something like notify-send -i info test or notify-send -i error test ...
Or notify-send -i printer-error test, which will find something like /usr/share/icons/Adwaita/256x256/status/printer-error.png under the hood.
Related
Apparently, one can refer to a program's icon solely by its name, like here:
zenity --question --text "Hello <b>world</b>" --icon-name "baobab"
Try replacing baobab with gnome-terminal and the icon will change, you may even drop the quotes. Obviously there is some kind of abstraction at work here (I like those) with a database. (I know about .desktop files, and the xdg tools]1. But is there a CLI to display (of even more hefpul: don't display it if not found, throwing an error)) those icons in such a way, like eog baobab? (doesn't work.) I want to do that, display the icon of the program I' displaying, sometimes said programs come from larger packages, so I have no real way to know if it's installed.
How can I know that the icon has not been found (important)?
How can I get a list of all those "installed" icons?
How can I test like chk_icon "baobab" ; $?=0OK
How can I test like chk_icon "installed stuff" ; $?=0OK
Thanks!
I was able to add a new recognized icon name by copying my icons to the various /usr/share/icons/gnome directories, by size, and THEN running
sudo gtk-update-icon-cache /usr/share/icons/gnome
without which the icon will still not be recognized.
EDIT, the question might have not been totally clear, short version:
How to popup a dialog asking for user input if the script is running in the background and not in an active console?
/EDIT
When I run a bash script from udev or cron, it usually runs quietly somewhere in the background. Example could be plugging in an external harddrive runs rsync for data backup. So not every time I plug in the harddrive do I want to launch this action.
What is the most minimal way to fire up some user input dialog and ask yes or no? I could write some interface with PyQt but I want as little dependencies as possible, ideally cross window manager and maybe even without window manager.
Thanks!
EDIT 2: The lightweight (and hence as ugly as expected) version is xmessage, this would probably be the answer to the question, unless you have a better one:
xmessage "Do you want to run the backup script?" -buttons yes,no
http://linux.byexamples.com/archives/87/using-gui-dialog-box/
EDIT: So there's KDialog for kde, is there something really lightweight for X?
http://www.linux-magazine.com/Issues/2009/99/Zenity-and-KDialog
kdialog --title "Do you want to run the backup script?"
--yesno "Do you want to run the backup script?"
I'm currently leaning towards zenity, only trouble is it pulls in a whole array of gtk dependencies on kde, but is cross platform and works on windows. In essence, this is exaclty what I was looking for:
if zenity --question --text="Please press a button."; then
zenity --info --text="You pressed Yes\!"
else
zenity --error --text="You pressed No\!"
fi
But was hoping it would be a lot lighter on the resources / dependencies. Any alternative suggestions?
A simple approach would be for the automated script to simply email you, or perhaps alert with wall with instructions (containing the location of the real script).
Another approach would be for the automated script to touch a file in /var/lib and proceed if the file is still present after 5 minutes. This gives the user the option to stop the process (by removing the file), but maintains some automation.
This is not something you should have cron or udev handling in the first place. Write a userland daemon that listens for the appropriate D-Bus messages and performs the appropriate actions.
This should be post in linux or superuser or serverfault.
But, the number one google search on linux read user input links back here to StackOverflow:
https://www.google.com/search?q=linux+read+user+input&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=fflb
so I guess it's not too off base.
Again, the third or so google result for linux invoke command usb device plugin links to ServerFault:
https://serverfault.com/questions/399698/execute-a-command-when-a-device-is-connected-via-usb
I'll leave combining those two as an exercise.
So I just found out I can create log files of everything I do in screen (C-a H). Sounds like a nice way to keep track of potential goofs in a particular screen session. However, when I went to try it out the logfile is reported as being a binary file (and can't be viewed like a regular text as such). So am I missing something? A quick man page looksee and searching Google (and SO) turns up nothing about this.
So my question is: How do I generate plain text log files in screen?
Assuming the answer is "What a noob... how about you try making them? RTFM." my question becomes: How do I use less to view screen logfiles I've created (since less screenlog.0 does not work on a binary file)?
EDIT: So cat works fine but less complains that the file is binary... why?
SOLUTION: as jcomeau_ictx helpfully pointed out, you can view these logfiles fine with cat or more but with less you must add the -r flag less -r screenlog.0
I just found a screenlog.0 on the net; it is plain text, with some escape sequences. Just 'cat' the file, you should be able to view it just fine.
[after more checking]
Control-A H is what generates the screenlog on my system. And though 'cat' works, you'll miss a lot of data. Use 'more' instead of 'less' to interpolate the escape codes.
I found neither less nor more nor cat to be an ideal solution for viewing screenlog files. All "replay" some of the control character so that e.g. screen deletions as produced by "clear" (don't remember the corresponding control character) are beeing shown, hiding what has been cleared.
What i know works great is: use "view" or "vi", it just shows the control character in escaped notation. Probably any other text editor works, too (not tested).
-L logs to file,
tail -f 'logfilename' to monitor this file
I currently use a color scheme based on which directory that I'm working in. I manually open up a Konsole shell and then cd into a directory and got to Settings and change the color scheme.
What I would like to do is have Konsole automatically set its foreground and background colors based on which directory I'm in. Basically if I'm in any subdirectory below /home/me/src/java then I would like to use text white, background blue, for example. If I'm below /home/me/src/documentation I want text black, background white, for example. I would like the color change to occur automatically, programmatically, when I call the "cd" command.
Is this possible? If so, can you provide me some direction as to how?
The way I see it I will need to be able to do a couple of things:
Be able to detect which Konsole the shell process (bash) is running in.
Be able to tell Konsole, probably via API call, to change the color scheme on the fly.
To get you started, here's a little information about using D-Bus to script Konsole.
You don't say which shell you're using, but if it's Bash you may want to use the $PROMPT_COMMAND variable which holds a command to be executed each time the $PS1 prompt is issued. The Z shell has a similar facility that's probably a bit more powerful (see man zshmisc for chpwd and precmd).
Otherwise, you might be able to use xterm escape sequences.
Context
Actually, Konsole has support for what they profiles. A profile is a group of settings (not only background), which you can manually define under Settings | Manage Profiles and around.
Also, there exists a command line utility called konsoleprofile which allows for programmatic changing of the profiles.
Actual answer
Go to Settings | Edit Current Profile... | Appearance
Define new Color Scheme for each of the directories you want to have special background for, e.g. myprofile1, myprofile2, mystandard
Make sure you can manually call konsoleprofile ColorScheme=myprofile1, konsoleprofile ColorScheme=standard, etc.
Plug in the calls to konsoleprofile into your $PROMPT_COMMAND, e.g. add this to your .bashrc:
PROMPT_COMMAND='[[ "$PWD" = /home/me/src/java* ]] && konsoleprofile ColorScheme=myprofile1 || konsoleprofile ColorScheme=mystandard'";$PROMPT_COMMAND"
I like to keep my shell sessions named with useful titles as I work, this helps me keep track of what I'm using each of the many tabs for.
Currently to rename a session I double click its name on the tabbed part of the console - is there any command that I can use to do this from within the shell? It would save me a bit of time.
thanks in advance
edit :-
I am using KDE's Konsole shell.
The article How to change the title of an xterm should help.
Currently to rename a session I double click its name on the tabbed part of the console
This sounds like you're using KDE's Konsole. Is this true?
If so, in KDE 3:
dcop $KONSOLE_DCOP_SESSION renameSession "I am renamed!"
In KDE 4, the old DCOP interfaces haven't been ported over to the new D-BUS IPC yet, but you can change the settings for tabnames to follow the window name set by each screen, and set the window name as described by the other answers.
According to this page, you should be able to use something like this:
echo -n "\033]0;New Window Title\007"
I'm not in Linux at the moment, so this is untested. I do know that it is possible to change the window title under program control, so this seems likely to work.
For /usr/bin/konsole
you can change the title of a konsole terminal from the menu:
Settings->Edit Current Profile->Tabs
edit "Tab title format" to be whatever you want. After interacting with the shell, the title will reset to what you put.
for /usr/bin/xterm running in xorg-server 2:1.10.1-1ubuntu1
echo -ne "\033]0;My Fun X-Terminal\007"
The answer to this really depends on the terminal program you're using.
However, I'll just assume it's sensible, and emulates an xterm enough that it respects xterm escape codes - in which case, you probably want to look here : http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3
Note: unwind's example below requires echo to be called like this "echo -ne", otherwise the '\' characters are echoed literally.
For the default terminal on Ubuntu (I'm still on 10.04) try xtitle.
$> sudo apt-get install xtitle
...
$> xtitle --title wow it worked!
or simply
$> xtitle this is great