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
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:
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 ofnotify-send
for displaying the icons...to get the last command executed.
and
sed
to parse the text to display it withnotify-send
message.To understand these try the following:
..this would echo terminal.
..this would echo error.
And,
..is very useful to know the exit value of the last command executed.
..nested
echo
as a simple demo for using$()
in a command combo.Let me try to explain what is happening here:
--urgency=low
-i "$([ $? = 0 ] && echo terminal || echo error)"
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.
$(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 separated in 2 block of sed.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.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 something like this:
It will show alert with previous command.
I think the current answers explain how the inner workings of alert work (which is what I wanted to find out of curiosity and which got me here). But I think the original OP asks for what it is useful for which I'll try to explain as I understand from the commends above its declaration.
It is basically used to alert you when a command has finished when you can't sit watching the terminal the whole time waiting for it to finish. As per the commend example
sleep 10; alert
will show a notification of the command (sleep 10
in this case) with a terminal icon if it is successful (sleep 10 will take 10 seconds to complete).From this you can see that it should be used as
<command>; alert
where you replace command with your command. I personally have to download a video everyday via wget because it fails periodically. So I just append the download command with alert to immediately notify me when it failed so that it can be continued again (my alert is modified to also beep to get my attention).The reason
sed
,tail
, andhistory
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 usehistory
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.