gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:02:39 24 4
gpt4 key购买 nike

我想运行一个给出以下输出的命令并解析它:

[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

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

伪代码如下:

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,并且在循环退出时在循环中设置的任何变量都将丢失。

关于linux - 在 Bash 脚本中解析命令输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4276924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com