bli*_*ann 2 command-line bash arguments
我需要运行一个看起来像这样的命令
mycli --file test.zip --file another_test.zip
如何使用目录中的所有 zip 文件动态运行它?我确定我可以从 find 命令通过管道传输文件,但我不知道如何将它们作为参数附加到另一个命令中,而且我的 bash-fu 不是很好
使用数组:
unset -v args
declare -a args
for file in *.zip
do
args+=( --file "$file" )
done
mycli "${args[@]}"
Run Code Online (Sandbox Code Playgroud)
或者,POSIXly:
set --
for file in *.zip
do
set -- "$@" --file "$file"
done
mycli "$@"
Run Code Online (Sandbox Code Playgroud)
或者,假设 GNU 工具:
find . -maxdepth 1 -name '*.zip' -printf '--file\0%f\0' |
xargs -0 -- mycli
Run Code Online (Sandbox Code Playgroud)
基于数组的方法和xargs基于数组的方法之间的相关区别:虽然前者可能会因“参数列表太长”错误而失败(假设mycli不是内置命令),但后者不会,mycli而是会运行多次. 但是请注意,在最后一种情况下,除了最后一个调用的参数列表之外的所有参数列表都可能以--file(并且下一个以文件名开头)。根据您的用例,您可以使用xargs' 选项(例如-n和-x)的组合来防止这种情况。
另外,请注意,find它将在其输出中包含隐藏文件,而基于数组的替代品不会,除非dotglob在 Bash 中设置了shell 选项,或者在 POSIX shell 中,同时使用了 the*.zip和.*.zipglobbing 表达式。有关详细信息和警告:
如何将所有文件(包括隐藏文件)从一个目录移动到另一个目录?.
| 归档时间: |
|
| 查看次数: |
77 次 |
| 最近记录: |