Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

System: Lubuntu 14.04 LTS

I want to know if it's possible to modify the alert alias provided in ~/.bashrc in a particular way. This is the original alert:

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

Use of default alias “alert” explains what it does.

And here is an example of the default notification on my system (using xfce4-notify) when I run ls; alert which, of course, completes successfully:

01Default-alert

If I run a nonsensical command, asdfgh; alert, I see this:

02Nonsense-command

The terminal icon has been replaced with the error icon because of

-i "$([ $? = 0 ] && echo terminal || echo error)"

As of now, the notification has just two items:
- the icon which is terminal or error depending on the success of the "previous" command
- the command itself (ls in the first case and asdfgh in the second)

I can modify the existing alias to include a line of text above the command like this:

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "Task finished" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

Then, if I run ls; alert and asdfgh; alert I get the notifications shown below.

Task finished successfully

Something went wrong

My question is this: Both notifications show "Task finished" but can the same logic that is used to decide whether the terminal icon or the error icon is shown be applied again to determine which text string is shown above the command? So, if the terminal icon displays, the text would be "Task finished" but if the error icon displays, the text could something like "Something went wrong!"

share|improve this question
up vote 5 down vote accepted

Tl;dr

Use this:

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && (echo terminal; exit 0) || (echo error; exit 1))" "$([ $? = 0 ] && echo Task finished || echo Something went wrong!)" "$(history | sed -n "\$s/^\s*[0-9]\+\s*\(.*\)[;&|]\s*alert\$/\1/p")"'

Simply evaluating $? again won't work, because by the time $? would be evaluated the second time it would contain the first command subsitution's ($([ $? = 0 ] && echo terminal || echo error)'s) exit status (always 0 since the two echos would always succed). A trick to go around this would be making the first command substitution exit with an equivalent exit status again:

$([ $? = 0 ] && (echo terminal; exit 0) || (echo error; exit 1))

This way $? could be evaluated a second time in order to decide which string to print:

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && (echo terminal; exit 0) || (echo error; exit 1))" "$([ $? = 0 ] && echo Task finished || echo Something went wrong!)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]+\s*//;s/[;&|]\s*alert$//'\'')"'

On another note your history|tail -n1|sed -e '\''s/^\s*[0-9]+\s*//;s/[;&|]\s*alert$//'\'' is a bit over-convoluted. This would be way more readable:

history | sed -n "\$s/^\s*[0-9]\+\s*\(.*\)[;&|]\s*alert\$/\1/p"

So, final command:

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && (echo terminal; exit 0) || (echo error; exit 1))" "$([ $? = 0 ] && echo Task finished || echo Something went wrong!)" "$(history | sed -n "\$s/^\s*[0-9]\+\s*\(.*\)[;&|]\s*alert\$/\1/p")"'

You can test by running (exit 0); alert and (exit 1); alert.

share|improve this answer
    
I'll try it out and let you know ASAP. Thanks! – DK Bose Mar 23 at 6:26
    
@DKBose Sure. Check out the update also, there was a problem with your sed command (I should have fixed it). – kos Mar 23 at 6:27
    
Just to clarify, I don't think I messed with the sed part at all. You can check in your ~/.bashrc. I'll also check in /etc/skel. BTW, your final command is what I want. Thanks again. – DK Bose Mar 23 at 6:30
    
@DKBose Right, I see it works for you, however that's weird. Your original alias doesn't work for me if I copy it verbatim (meaning: I get the number before the command). That would be the expected behavior, since GNU sed by default interprets patterns as EREs, which require escaping +. In fact escaping + on your original command works for me. Just curious, which sed are you using? – kos Mar 23 at 6:35
    
I'm using the default sed provided by Lubuntu which is an official Ubuntu flavor. sed --version gives me sed (GNU sed) 4.2.2 – DK Bose Mar 23 at 7:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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