加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 在Bash脚本中解析命令输出

发布时间:2020-12-14 00:33:33 所属栏目:Linux 来源:网络整理
导读:我想运行一个命令,提供以下输出并解析它: [VDB VIEW][VDB] vhctest [BACKEND] domain.computername: ENABLED:RW:CONSISTENT [BACKEND] domain.computername: ENABLED:RW:CONSISTENT ... 我只对一些关键作品感兴趣,例如’ENABLED’等.我不能只搜索ENABLED,因
我想运行一个命令,提供以下输出并解析它:

[VDB VIEW]
[VDB] vhctest
        [BACKEND] domain.computername: ENABLED:RW:CONSISTENT
        [BACKEND] domain.computername: ENABLED:RW:CONSISTENT
        ...

我只对一些关键作品感兴趣,例如’ENABLED’等.我不能只搜索ENABLED,因为我需要一次解析每一行.

这是我的第一个脚本,我想知道是否有人可以帮助我?

编辑:
我现在有:

cmdout=`mycommand`

while read -r line
do
   #check for key words in $line
done < $cmdout

我认为这样做了我想要的但是它似乎总是在命令输出之前输出以下内容.

./myscript.sh: 29: cannot open … : No such file

我不想写一个文件来实现这一点.

这是psudo代码:

cmdout=`mycommand`

loop each line in $cmdout
   if line contains $1
       if line contains $2
            output 1
       else
            output 0

解决方法

错误的原因是

done < $cmdout

认为$cmdout的内容是文件名.

你可以这样做:

done <<< $cmdout

要么

done <<EOF
$cmdout
EOF

要么

done < <(mycommand)    # without using the variable at all

要么

done <<< $(mycommand)

要么

done <<EOF
$(mycommand)
EOF

要么

mycommand | while
...
done

但是,最后一个创建一个子shell,当循环退出时,循环中设置的任何变量都将丢失.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读