I am newbie in Linux. I'd like to dynamically set the title of mail command. e.g.
我是Linux的新手。我想动态设置邮件命令的标题。例如
command 1>&2 | mail -s "error" abc@gmail.com
command 1>&2 | mail -s "success" abc@gmail.com
the previous command returns different result to the mail command. Is there any way to set the title by searching the keyword in the output of previous command? If there is 'error', the title will be set to 'error' correspondingly. Thank everyone in advance.
上一个命令将不同的结果返回给mail命令。有没有办法通过在上一个命令的输出中搜索关键字来设置标题?如果存在“错误”,则标题将相应地设置为“错误”。提前感谢大家。
1 个解决方案
#1
1
So the previous command is command
. So you want to search its output and create a subject from it, and then you want the entire contents as the body. Am I correct?
所以前面的命令是命令。因此,您希望搜索其输出并从中创建主题,然后您希望将整个内容作为正文。我对么?
What if you store the output in a variable?
如果将输出存储在变量中会怎样?
output="`command 1>&2`"
Then, you could examine it as you wish (not perfect but hopefully a start):
然后,你可以按照你的意愿检查它(不完美,但希望是一个开始):
success="`echo ${output} | sed -e 's/blah/blah/'`"
You'd still have the output contents available to mail:
您仍然可以使用邮件的输出内容:
echo ${output} | mail -s "${success}" abc@gmail.com
#1
1
So the previous command is command
. So you want to search its output and create a subject from it, and then you want the entire contents as the body. Am I correct?
所以前面的命令是命令。因此,您希望搜索其输出并从中创建主题,然后您希望将整个内容作为正文。我对么?
What if you store the output in a variable?
如果将输出存储在变量中会怎样?
output="`command 1>&2`"
Then, you could examine it as you wish (not perfect but hopefully a start):
然后,你可以按照你的意愿检查它(不完美,但希望是一个开始):
success="`echo ${output} | sed -e 's/blah/blah/'`"
You'd still have the output contents available to mail:
您仍然可以使用邮件的输出内容:
echo ${output} | mail -s "${success}" abc@gmail.com