-1

I want to display a warning message when a test in my ~/.profile (actually, in another file that I source in ~/.profile) fails, e.g.:

if [ -f /usr/bin/vim ]; then                                                                   
  export VISUAL=/usr/bin/vim
else
  echosomehow File not found
fi

The command should be portable and it should display the message as soon as possible when I log into a graphical session.

I've thought about using the $PROMPT_COMMAND to issue an echo command, but I'm not sure it is a proper way.

  • 3
    What's the issue with echo "File not found" >&2? – Kusalananda Jul 2 '18 at 20:48
  • command not found echosomehow, are you trying to say echo somehow file not found. oh I can not work it out. Out of here, I am. – ctrl-alt-delor Jul 2 '18 at 21:44
  • @Kusalananda I wasn't clear in my question, but I want a portable way to display the warning in a graphical session. Your solution works, but only on a tty. I had thought about using Zenity but discarded it because it's not really portable. – Arch Stanton Jul 3 '18 at 8:47
  • @ArchStanton Rather than adding more info in comments, edit your question and clarify it. – Kusalananda Jul 3 '18 at 8:49
  • @ctrl-alt-delor Sorry, that was a dummy command :-) – Arch Stanton Jul 3 '18 at 8:54
2

You should use notify-send to display graphical messages. DEs should have a notification area to show such messages.

notify-send - a program to send desktop notifications

If you use notify-send outside of X11 or if you want to send a notification to another X11 server, you have to populate the $DISPLAY variable as described in the answers of this question on superuser.com.

  • 1
    It may also be possible that there are better places to do this from than from ~/.profile (which is processed whenever a login shell starts). The ~/.xsession, ~/.xinitrc or equivalent file may be a better place for a one off thing like this. – Kusalananda Jul 3 '18 at 10:50
  • Thanks ctx, but I've tested it and while it works if I run it on a terminal, it doesn't if it's called from ~/.profile. – Arch Stanton Jul 3 '18 at 14:52
  • Are you sure that ~/.profile is executed? What happens if you run bash ~/.profile? Why do you want to use ~/.profile? Did you try the files suggested by @Kusalananda ? Please edit your question, if you don't care for ~/.profile but want to 'display the message as soon as possible when you log into a graphical session'. – ctx Jul 3 '18 at 18:09
  • @ctx "Are you sure that ~/.profile is executed?" Yes, because the environment variables defined there are set. "What happens if you run bash ~/.profile?" The notification shows up (however, nothing is shown at startup with the same ~/.profile). "Why do you want to use ~/.profile?" Because I want to be warned when an environment variable refers to a nonexistent file, and ~/.profile is where environment variable definitions go, to the best of my knowledge. (Are ~/.xsession and ~/.xinitrc executed on login to a tty?) I'm sorry for the late response, I wasn't notified of your comment – Arch Stanton Jul 9 '18 at 12:51
  • If you run notify-send outside of X11, you have to set the DISPLAY variable. – ctx Jul 10 '18 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.