gpt4 book ai didi

Bash 读取输出?

转载 作者:IT王子 更新时间:2023-10-28 23:52:13 27 4
gpt4 key购买 nike

假设我执行了一个 bash 脚本,输出是:

Test1: Some text...
Test2: Some text...
Test3: Some text...

在同一个 bash 脚本中,我如何将上述输出存储为一个或多个变量?

理想的解决方案是让它准备好在这样的条件下使用:(输出的第一行将存储在 $ln1 等中)

if [ $ln1 = "Test1: Some text..." ] ; then

最佳答案

所以你想要

output=$(command)
while IFS= read -r line; do
process "$line"
done <<< "$output"

参见 Bash manual 中的“这里的字符串” .

process substitution

while IFS= read -r line; do
process "$line"
done < <(command)

几年后回到这个话题,现在我将命令的输出读入一个数组:

readarray -t lines < <(command)
for line in "${lines[@]}"; do
do-something-with "$line"
done

关于Bash 读取输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5853400/

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