我坚持一种奇怪的readarray命令行为。
的man bash状态:
readarray
Read lines from the standard input into the indexed array variable array
Run Code Online (Sandbox Code Playgroud)
但这些脚本不起作用(数组为空):
unset arr; (echo a; echo b; echo c) | readarray arr; echo ${#arr[@]}
unset arr; cat /etc/passwd | readarray arr; echo ${#arr[@]}
Run Code Online (Sandbox Code Playgroud)
这些工作:
unset arr; readarray arr < /etc/passwd ; echo ${#arr[@]}
unset arr; mkfifo /tmp/fifo; (echo a; echo b; echo c) > /tmp/fifo & mapfile arr < /tmp/fifo ; echo ${#arr[@]}
Run Code Online (Sandbox Code Playgroud)
管道有什么问题?
我正在尝试使Bash 自动完成工作并编写这段代码来演示我遇到的问题:
$ cat completion.sh
function _completion_command() {
compopt +o bashdefault +o default +o dirnames +o filenames +o nospace +o plusdirs
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
case "$prev" in
-u)
compopt -o nospace
COMPREPLY=($(compgen -S\= -W "parm" -- $cur))
return 0
;;
parm)
COMPREPLY=($(compgen -W "a b c" -- $cur))
return 0
;;
esac
COMPREPLY=($(compgen -W "-u" -- $cur))
return 0
}
complete -F _completion_command command
Run Code Online (Sandbox Code Playgroud)
我希望完成为名为“parm”的参数提供可能的参数:
. completion.sh
./command -u parm=
Run Code Online (Sandbox Code Playgroud)
Tab Tab
a b c
Run Code Online (Sandbox Code Playgroud)
但就我而言,自动完成功能对我没有任何帮助。