如果!=,则从输出/文件中提取值并通过管道传递到电子邮件

时间:2019-03-28 16:14:17

标签: bash awk sed

我有一台装有hwraid(megaclisas)https://hwraid.le-vert.net/wiki/DebianPackages

的服务器

示例输出如下:

-- Controller information --
-- ID | H/W Model      | RAM    | Temp | BBU    | Firmware     
c0    | PERC H310 Mini | 0MB    | 59C  | Absent | FW: 20.13.3-0001 

-- Array information --
-- ID | Type    |    Size |  Strpsz | Flags | DskCache |   Status |  OS Path | CacheCade |InProgress   
c0u0  | RAID-10 |   3272G |   64 KB | RA,WT |  Default |  Optimal | /dev/sda | None      |None         

-- Disk information --
-- ID    | Type | Drive Model                      | Size     | Status          | Speed    | Temp | Slot ID  | LSI ID  
c0u0s0p0 | HDD  | SEAGATE ST900MM0006 LS0AS0N3Bxxx | 837. Gb  | Online, Spun Up | 6.0Gb/s  | 31C  | [32:0]   | 0       
c0u0s0p1 | HDD  | SEAGATE ST900MM0006 LS0AS0N3Bxxx | 837. Gb  | Online, Spun Up | 6.0Gb/s  | 31C  | [32:1]   | 1       
c0u0s1p0 | HDD  | SEAGATE ST900MM0006 LS0AS0N3Bxxx | 837. Gb  | Online, Spun Up | 6.0Gb/s  | 28C  | [32:2]   | 2       
c0u0s1p1 | HDD  | SEAGATE ST900MM0006 LS0AS0N3Bxxx | 837. Gb  | Online, Spun Up | 6.0Gb/s  | 30C  | [32:3]   | 3       
c0u0s2p0 | HDD  | SEAGATE ST900MM0006 LS0AS0N3Bxxx | 837. Gb  | Online, Spun Up | 6.0Gb/s  | 29C  | [32:4]   | 4       
c0u0s2p1 | HDD  | SEAGATE ST900MM0006 LS0AS0N3Bxxx | 837. Gb  | Online, Spun Up | 6.0Gb/s  | 31C  | [32:5]   | 5       
c0u0s3p0 | HDD  | SEAGATE ST900MM0006 LS0AS0N3Bxxx | 837. Gb  | Online, Spun Up | 6.0Gb/s  | 30C  | [32:7]   | 7       
c0u0s3p1 | HDD  | SEAGATE ST900MM0006 LS0AS0N3Bxxx | 837. Gb  | Online, Spun Up | 6.0Gb/s  | 28C  | [32:6]   | 6   

我要实现的是,如果grep Status值不等于Optimal或Online,则将其设置为grep,然后通过管道发送给电子邮件。我这里的问题是如何使用sed或awk来获取该信息。

1 个答案:

答案 0 :(得分:0)

这里是解决方法

data=$(mktemp)
externalprogram > $data
RESULT=$(
    grep "| RAID" $data | sed -n '/Optimal/!p'
    grep "| HDD" $data | sed -n '/Online,/!p'
)
rm $data
echo "$RESULT"

解释

  • grep感兴趣的行
  • 如果未找到搜索模式,则打印