简体   繁体   English

如何在新行上获取diff命令的输出

[英]How to get output of diff command on new lines

I'm new to the world of scripting and and currently using a script to perform a health check on the san devices that I manage. 我是脚本世界的新手,目前正在使用脚本在我管理的san设备上执行运行状况检查。

The script is simple and redirects output of commands to a current file from remote device to my local host. 该脚本很简单,可以将命令输出重定向到当前文件,该文件从远程设备到我的本地主机。 I then use a diff command to compare output of current file and reference file and get a email alert of the difference. 然后,我使用diff命令比较当前文件和参考文件的输出,并获得有关差异的电子邮件警报。 The output of diff is passed to a variable "variable" and then using a if loop if get the result diff的输出传递给变量“ variable”,然后使用if循环获取结果

I have so far got the autologin and email part working ( i get a email only if there is a difference from DIFF command). 到目前为止,我已经实现了自动登录和电子邮件部分的工作(只有与DIFF命令有区别的情况下,我才会收到电子邮件)。 However the email that i now get has its test concatenated into one long string, which is bad for viewing and not easy to understand for the people who will be getting it. 但是,我现在收到的电子邮件将其测试连接到一个长字符串中,这对查看不利,对于将要接收它的人来说也不容易理解。

My current script is pasted below 我当前的脚本粘贴在下面

ssh test@ip_of_device switchshow > switchshow_results
variable=diff switchshow_reference switchshow_results
if [[ $variable -eq 0 ]]
then
    echo $"nothing"
else
    echo $variable | mail -s switch_HARDWARE_CHECK recipeint_email_address
fi

The output of the above script is blank and i do not get any email when the diff command does not find any difference. 上面的脚本的输出为空,并且当diff命令没有任何区别时,我没有收到任何电子邮件。

However when the diff commands finds a difference, i get a email as pasted below 但是,当diff命令发现差异时,我收到一封如下所示的电子邮件

16c16 < 0 0 010000 id N4 Online FC F-Port 1 N Port + 1 NPIV public --- > 0 0 010000 id N4 No_Light FC F-Port 1 N Port + 1 NPIV public 26c26 < 10 10 010a00 id N4 Online FC F-Port 1     N Port + 1 NPIV public --- > 10 10 010a00 id N4 No_Light FC F-Port 1 N Port + 1 NPIV public 29c29 < 13 13 010d00 id N4 Online FC F-Port 50:06:01:67:3b:20:23:0a --- > 13 13 010d00 id N4 No_Light FC F-Port 50:06:01:67:3b:20:23:0a

This should ideally look like this in the email.. 理想情况下,它应该像电子邮件中的样子。

16c16 < 0 0 010000 id N4 Online FC F-Port 1 N Port + 1 NPIV public --- > 0 0 010000 id N4 No_Light FC F-Port 1 N Port + 1 NPIV public

26c26 < 10 10 010a00 id N4 Online FC F-Port 1 N Port + 1 NPIV public --- > 10 10 010a00 id N4 No_Light FC F-Port 1 N Port + 1 NPIV public

29c29 < 13 13 010d00 id N4 Online FC F-Port 50:06:01:67:3b:20:23:0a --- > 13 13 010d00 id N4 No_Light FC F-Port 50:06:01:67:3b:20:23:0a

output should display one line at a time in the email. 输出应在电子邮件中一次显示一行。 Any suggestions? 有什么建议么? This works fine when there is only one line which has the difference, however multiple line difference gives me the above issue. 当只有一行存在差异时,这可以很好地工作,但是多行差异给了我上述问题。 Also my requirement is that diff output has to be emailed to me. 另外,我的要求是必须将差异输出通过电子邮件发送给我。 I checked the various options in diff command, however i was not able to figure out any that would help me. 我检查了diff命令中的各种选项,但是我找不到能帮助我的任何选项。

Any help would be much appriciated. 任何帮助将不胜枚举。

You need to run a sed command to replace '/n' with '' in the email conteent that is being sent. 您需要运行sed命令,以在发送的电子邮件内容中将“ / n”替换为“”。

Emails are rendered as HTML text, hence the /n is ommited. 电子邮件以HTML文本的形式呈现,因此/ n被省略。
makes the html show it on next line. 使html在下一行显示它。

You forgot to place quotes around $variable 您忘记将报价放在$variable周围

Use this: 用这个:

echo "$variable" | mail -s switch_HARDWARE_CHECK recipeint_email_address

The reason for that is explained here 原因在这里解释

What's supposed to be in "variable"? “变量”应该是什么?

the output of the diff? 差异的输出? (use backticks) (使用反引号)

or diff's exit code? 或diff的退出代码? (use $?) (使用$?)

Because what you've got right now gives me an error. 因为您现在所掌握的一切给了我一个错误。

-bash: switchshow_reference: command not found

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM