我有一个 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
[编辑]更好的例子:https://stackoverflow.com/a/52539364/1888983 https://stackoverflow.com/a/52539364/1888983
这非常有用,特别是当我有多个可以成为直通的函数时。是的,我可以移动 CHECK_OUTPUT 条件并创建一个带有或不带有 check_output 的管道,但我需要为每个组合编写行以获得更多功能。如果有更好的方法来动态构建管道我想知道。
问题是“猫的无用”。这可以避免吗?check_output
就像它根本不在管道中一样?
是的,您可以做到这一点 - 通过使您的函数成为有条件注入管道元素的包装器,而不是成为无条件管道元素本身。例如:
maybe_checked() {
if [[ $CHECK_OUTPUT != "--check" ]]; then
"$@" # just run our arguments as a command, as if we weren't here
else
# run our arguments in a process substitution, reading from stdout of same.
# ...some changes from the original code:
# IFS= stops leading or trailing whitespace from being stripped
# read -r prevents backslashes from being processed
local line # avoid modifying $line outside our function
while IFS= read -r line; do
[[ -e "/$line" ]] || { echo "Error: /$line does not exist" >&2; return 1; }
printf '%s\n' "$line" # see https://unix.stackexchange.com/questions/65803
done < <("$@")
fi
}
ls /usr | maybe_checked grep '^b'
上述代码的警告:如果pipefail
设置选项后,您需要检查进程替换的退出状态,以便与其他情况下的行为完全一致。在 bash 版本 4.3 或更高版本 (IIRC) 中,$?
通过进程替换来修改以获得相关的PID,可以是wait
ed 用于检索退出状态。
也就是说,这也是一个使用案例cat
是可以接受的,我是作为 UUOC 人群中的持卡成员这么说的。 :)
采用约翰·库格曼对相关问题的回答中的示例:
maybe_sort() {
if (( sort )); then
"$@" | sort
else
"$@"
fi
}
maybe_limit() {
if [[ -n $limit ]]; then
"$@" | head -n "$limit"
else
"$@"
fi
}
printf '%s\n' "${haikus[@]}" | maybe_limit maybe_sort sed -e 's/^[ \t]*//'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)