gpt4 book ai didi

arrays - 使用管道运行 bash 数组

转载 作者:行者123 更新时间:2023-11-29 09:12:10 24 4
gpt4 key购买 nike

如何从包含管道的 bash 数组运行命令行?

例如,我想运行 ls | grep x 通过:

$ declare -a pipeline
$ pipeline=(ls)
$ pipeline+=("|")
$ pipeline+=(grep x)
$ "${pipeline[@]}"

但是我明白了:

ls: cannot access |: No such file or directory
ls: cannot access grep: No such file or directory
ls: cannot access x: No such file or directory

最佳答案

简短形式:你不能(不编写一些代码),这是一个功能,而不是错误。

如果您以安全的方式做事,您就是在保护您的数据不被解析为代码(语法)。然而,您在这里明确想要的是将数据视为代码,但只是以一种受控的方式。

你可以做的是迭代元素,如果它们不是管道,则使用 printf '%q ' "$element" 获取安全引用的字符串,如果它们是管道,则不替换它们.

在这样做之后,并且只有在这样做之后,您才能安全地评估输出字符串。

eval_args() {
local outstr=''
while (( $# )); do
if [[ $1 = '|' ]]; then
outstr+="| "
else
printf -v outstr '%s%q ' "$outstr" "$1"
fi
shift
done
eval "$outstr"
}
eval_args "${pipeline[@]}"

顺便说一句——不这样做会更安全。考虑一下您正在处理文件列表的情况,其中一个名为 |;攻击者可以使用此策略来注入(inject)代码。为前后数组使用单独的列表,或者仅将管道的一侧设为数组并对另一侧进行硬编码,是更好的做法。

关于arrays - 使用管道运行 bash 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16992684/

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