小编qma*_*cro的帖子

Bash 在基于字符串的超时选项规范上读取内置错误,而不是基于数组的超时选项规范。为什么?

在阅读fff的源代码以了解有关 Bash 编程的更多信息时,我看到一个超时选项read作为数组传递给这里

read "${read_flags[@]}" -srn 1 && key "$REPLY"
Run Code Online (Sandbox Code Playgroud)

的值read_flags设置如下

read_flags=(-t 0.05)
Run Code Online (Sandbox Code Playgroud)

read因此,预期的调用是read -t 0.05 -srn 1)。

我不太明白为什么不能使用字符串,即:

read_flags="-t 0.05"
read "$read_flags" -srn 1 && key "$REPLY"
Run Code Online (Sandbox Code Playgroud)

这种基于字符串的方法会导致“无效的超时规范”。

经过调查,我想出了一个测试脚本parmtest

show() {
  for i in "$@"; do printf '[%s]' "$i"; done
  printf '\n'
}

opt_string="-t 1"
opt_array=(-t 1)

echo 'Using string-based option...'
show string "$opt_string" x y z
read "$opt_string"
echo
echo 'Using array-based …
Run Code Online (Sandbox Code Playgroud)

bash shell-script quoting shell-builtin bash-array

4
推荐指数
1
解决办法
265
查看次数

标签 统计

bash ×1

bash-array ×1

quoting ×1

shell-builtin ×1

shell-script ×1