简体   繁体   English

正确的 BASH 管道语法

[英]Proper BASH piping syntax

I'd like to do a one line bash command to ping a server, then send an email if it fails.我想做一个单行 bash 命令来 ping 服务器,然后在失败时发送一封电子邮件。

I was thinking it ought to look something like:我在想它应该看起来像:

ping whatever.com || echo 'Subject: VPN Failed" | sendmail me@me.com

But my BASH skills are "growing" and I'm apparently missing something.但是我的 BASH 技能正在“增长”,我显然错过了一些东西。 What would be the proper way of doing that?这样做的正确方法是什么?

You mixed single/double quotes.您混合了单引号/双引号。

In addition, if you want to use instruction in case of failure, I recommend using the !另外,如果你想在失败的情况下使用指令,我建议使用! and && operators, which will allow you to perform several instructions.&&运算符,这将允许您执行多个指令。

For instance:例如:

! ping whatever.com && echo "An error occured, system will send an e-mail" >&2 && echo "Subject: VPN Failed" |sendmail me@me.com

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

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