bash - 循环遍历一组随机文件
问题描述
我有一组要循环的随机文件(例如,我可以复制或移动)。我正在使用shuf
使用 bash 函数的命令。
inclnm
通常包含特定文件的通配符,例如
( -name A\* -o -name B\*.mse )
.
这是我想循环的随机列表命令。
shuf -n $nf < <( find $dpath -maxdepth 1 -type f "${inclnm[@]}" )
解决方案
最不受欢迎的选项是使用空分隔记录:
#!/usr/bin/env bash
dpath=./
nf=10
inclnm=( -name a\* -o -name b\* )
mapfile -d '' random_files < <(
find "$dpath" -maxdepth 1 -type f "${inclnm[@]}" -print0 |
shuf -z -n "$nf"
)
# Debug
declare -p random_files
或者替换find
为printf
:
inclnm=( a* b* )
cd "$dpath" || :
mapfile -d '' random_files < <( printf %s\\0 "${inclnm[@]}" | shuf -z -n "$nf" )
或者更好的是,让我们shuf
直接处理参数而不需要printf
or find
:
mapfile -d '' random_files < <( shuf -z -n "$nf" -e "${inclnm[@]}" )
请注意,最后一种方法将受到最大参数长度限制。
推荐阅读
- excel - VBA 运行时错误“1004”中的 GoalSeek:应用程序定义或对象定义错误
- javascript - 无法在 typescript 和 javascript 中以相同的方式导入库
- python - 如何使用 DFS 在有向图中递归查找路径
- java - 化妆品礼帽。
(RenderPlayer) 行:23?无法在 Minecraft 中渲染 Java 对象 - reactjs - 为什么我的字体真棒 LinkedIn 图标在我的反应网页上显示为这样?
- javascript - 当我的 JSON 代码没有错误时,为什么 JavaScript 引擎会返回语法错误?
- python - python中递归函数的问题
- c# - 在视图之间切换 wpf 窗口的最佳方法是什么?
- javascript - 为什么即使我使用了 useEffect 我的组件也不会重新渲染自己?(React.js)
- arrays - 获取循环 Swift 的当前位置