相关疑难解决方法(0)

我可以在不使用 cat 的情况下将 shell 函数作为管道有条件地“消失”吗?

我有一个 bash 脚本,它从命令管道中生成一些文本。基于命令行选项,我想对输出进行一些验证。对于一个人为的例子......

CHECK_OUTPUT=$1
...
check_output()
{
    if [[ "$CHECK_OUTPUT" != "--check" ]]; then
        # Don't check the output. Passthrough and return.
        cat
        return 0
    fi

    # Check each line exists in the fs root
    while read line; do
        if [[ ! -e "/$line" ]]; then
            echo "Error: /$line does not exist"
            return 1
        fi
        echo "$line"
    done
    return 0
}

ls /usr | grep '^b' | check_output
Run Code Online (Sandbox Code Playgroud)

[编辑] 更好的例子:https : //stackoverflow.com/a/52539364/1888983

这真的很有用,特别是如果我有多个可以成为直通的函数。是的,我可以移动 CHECK_OUTPUT 条件并创建一个带有或不带有 check_output 的管道,但我需要为每个组合编写行以获得更多功能。如果有更好的方法来动态构建管道,我想知道。

问题是“猫的无用使用”。可以避免这种情况并使其check_output …

bash shell pipe cat

2
推荐指数
1
解决办法
90
查看次数

标签 统计

bash ×1

cat ×1

pipe ×1

shell ×1