Reg*_*ser 10 notification bash
默认别名Alert用于命令
notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"
Run Code Online (Sandbox Code Playgroud)
执行alert给出带有文本警报和终端图标的通知。用一个参数执行它就像alert !!!!!给出带有文本警报的通知!!!!!!和!!!!!! .
那么,简单notify-send命令和这个使用通知发送、回声、历史、尾和 sed 的复杂别名之间有什么区别?
在什么情况下这个别名有用还是它只是为双关语创建的(就像使用 sudo sudo sudo sudo sudo apt-get install
我正在使用 Ubuntu 12.10
您可以使用手册页获取有关此处组合的命令的详细信息。这里有一些关于这些命令的目的:
"$([ $? = 0 ] && echo terminal || echo error)"
Run Code Online (Sandbox Code Playgroud)
这将根据执行状态回显终端或错误-分别是最后一个命令的成功或失败;结果作为显示图标的-i开关的值notify-send。
history|tail -n1
Run Code Online (Sandbox Code Playgroud)
..获取执行的最后一个命令。
并sed解析文本以显示notify-send消息。
要了解这些,请尝试以下操作:
true; echo "$([ $? = 0 ] && echo terminal || echo error)"
Run Code Online (Sandbox Code Playgroud)
..这将回显终端。
false; echo "$([ $? = 0 ] && echo terminal || echo error)"
Run Code Online (Sandbox Code Playgroud)
..这会回显错误。
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'
Run Code Online (Sandbox Code Playgroud)
和,
echo $?
Run Code Online (Sandbox Code Playgroud)
..对于知道最后执行的命令的退出值非常有用。
echo "$(echo "the output of this command enclosed within \$(...)") is being supplied here in the outer echo command where is used as an argument."
Run Code Online (Sandbox Code Playgroud)
..echo作为一个简单的演示嵌套$()在命令组合中使用。
让我试着解释一下这里发生了什么:
notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"
Run Code Online (Sandbox Code Playgroud)
1 --urgency=low
-u, --urgency=LEVEL 指定紧急程度(低、正常、严重)。
2 -i "$([ $? = 0 ] && echo terminal || echo error)"
.
Run Code Online (Sandbox Code Playgroud)-i, --icon=ICON[,ICON...] Specifies an icon filename or stock icon to display.
这部分"$([ $? = 0 ] && echo terminal || echo error)"。$?是返回的最后一个错误(或成功)。因此,如果最后一个命令退出代码为 0,则它返回文本“终端”,没有错误。如果退出代码不为 0,则返回“错误”。
最后我们得到“终端”或“错误”图标。
3 $(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')
history|tail -n1 返回历史记录中的最后一个命令。
sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'') 这可以在 2 个 sed 块中分离。
3.1. sed 's/^\s*[0-9]\+\s*//'删除所有前面的空格和制表符,之后的所有数字,并删除末尾的空格和制表符。
3.2. s/[;&|]\s*alert$//删除前面的符号; & |、任何制表符和空格以及单词“alert”。
它只是从符号中清除最后执行的命令,最后是“警报”一词。
所以如果你使用这样的东西:
echo "Hello alert" | alert
Run Code Online (Sandbox Code Playgroud)
它将显示带有先前命令的警报。