shell脚本中获取另一个脚本执行输出内容并解析

你可以使用管道(pipe)来捕获脚本a.sh的输出,并在脚本b中进行处理。以下是一个示例shell代码,实现了你描述的逻辑:

#!/bin/bash

# 定义一个函数,用于执行脚本a.sh并检查输出结果
execute_script_a() {
    # 执行脚本a.sh,并将输出通过管道传递给while循环
    ./a.sh | while IFS= read -r line; do
        # 在输出中查找是否包含 "ok" 字符串
        if [[ $line == *"ok"* ]]; then
            echo "Found 'ok' in the output of script a.sh"
            return 0  # 返回0表示找到了"ok"字符串
        fi
    done
    # 如果while循环结束仍然没有找到"ok"字符串,则返回1
    return 1
}

# 调用函数执行脚本a.sh,并检查结果
execute_script_a
result=$?

# 根据执行结果进行判断
if [ $result -eq 0 ]; then
    echo "Continue with other tasks."
    # 在这里继续执行其他任务
else
    echo "'ok' not found in the output of script a.sh. Re-executing a.sh."
    # 在这里可以重新执行a.sh脚本
fi

这个脚本首先定义了一个函数execute_script_a(),该函数执行脚本a.sh并检查输出结果是否包含"ok"字符串。然后,在主代码中调用该函数,并根据返回值来判断是否继续执行其他任务,或者重新执行脚本a.sh

猜你喜欢

转载自blog.csdn.net/zwjapple/article/details/138305504