Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

The default alias Alert is for the command

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

Executing alert gives an notification with text Alert and an terminal icon. Executing it with one param like alert !!!!! gives notification with text Alert !!!!! and !!!!!.

So, what's the difference between simple notify-send command and this complexed alias which uses notify-send, echo, history, tail and sed?

In which situations is this alias useful or was it just created for pun(Something like using sudo sudo sudo sudo sudo apt-get install

I'm using Ubuntu 12.10

share|improve this question
    
The only thing I can think of is just to make creating a notification easier...maybe? –  saiarcot895 Feb 20 at 13:14
    
@saiarcot895 try notify-send Linux Ubuntu it's much the same, the only difference is that alert shows a icon.But I don't understand why the command contains sed,tail and history –  Patil Aditya Feb 20 at 13:21
add comment

3 Answers

up vote 3 down vote accepted

You could use the man pages to get details about what the commands combined here do. Here's a little about the purpose of those commands here:

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

This would echo terminal or error as per to the execution status - successful or fail respectively of the last command; and the result is as the value to the -i switch of notify-send for displaying the icons.

history|tail -n1

..to get the last command executed.

and sed to parse the text to display it with notify-send message.


To understand these try the following:

true; echo "$([ $? = 0 ] && echo terminal || echo error)"

..this would echo terminal.

false; echo "$([ $? = 0 ] && echo terminal || echo error)"

..this would echo error.

notify-send -i terminal 'Please note the icon..' 'the default icon for the terminal has been used. You can specify any other icon of your choice'

And,

echo $?

..is very useful to know the exit value of the last command executed.

echo "$(echo "the output of this command enclosed within \$(...)") is being supplied here in the outer echo command where is used as an argument."

..nested echo as a simple demo for using $() in a command combo.

share|improve this answer
    
so this alias can be used to tell the user whether the command executed successfully or failed, but in an GUI way. –  Patil Aditya Feb 20 at 13:42
    
yes it would notify about both the successful or unsuccessful executions graphically using the notify-send.. the exit status of last command is checked with the value of $?..I've included this in the edit.. –  rusty Feb 20 at 13:57
add comment

Let me try to explain what is happening here:

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

1 --urgency=low

-u, --urgency=LEVEL Specifies the urgency level (low, normal, critical).

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

  -i, --icon=ICON[,ICON...]
         Specifies an icon filename or stock icon to display.

This part "$([ $? = 0 ] && echo terminal || echo error)". $? is the last error (or success) returned. So it returns text "terminal" if last command exit code was 0, without errors. Or returns "error" if exit code was not 0.

And finally we get "terminal" or "error" icon.

3 $(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')

history|tail -n1 returns the last command from history.

sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'') this can be separeted in 2 block of sed.

3.1.sed 's/^\s*[0-9]\+\s*//' remove all preceding spaces and tabs, all numerical after that, and also remove spaces and tabs at the end.

3.2.s/[;&|]\s*alert$// removes preceding symbols ; & |, any tabs and spaces and word "alert".

It is just clean last executed command from symbols, and the word "alert" at the end.

So if you use some thing like this:

echo "Hello alert" | alert

It will show alert with previous command.

share|improve this answer
add comment

The reason sed, tail, and history are there is because it needs to get the message that you typed. (For some reason,) what you typed isn't directly available through any variable. Therefore, it has to use history to get the list of everything you've typed, tail -n1 to get the last thing you typed, and some regex to get rid of the "alert" command at the start.

Just to expand on this, there is a conditional command in there that tests to see if $? = 0 is true. From what I can tell, if it is true, then it is coming from the terminal, and should be regarded as a normal message. Otherwise, it should be considered an error, and an error icon will appear.

share|improve this answer
    
It doesn't get rid of alert, it will remain in the notification. –  Patil Aditya Feb 20 at 13:35
    
If you answer with "for some reason" it is often not a very good answer ;) –  Requist Feb 20 at 13:39
    
@Requist: I added "for some reason" for that specific statement because I was under the impression that $0 gives you the first argument for a command. It might be different in the case of an alias, though. –  saiarcot895 Feb 20 at 14:11
add comment

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.