bash - 使用 mapfile 调用 rsync
问题描述
使用以下 bash 代码来处理随 mapfile 随机传递的文件名。我想调用rsync
每个文件并发送到目标路径dpath
。
mapfile -d '' fl < <(
find "$dpath" -maxdepth 1 -type f "${inclnm[@]}" |
shuf -z -n "$nf"
)
或者shuf
直接处理参数
mapfile -d '' fl < <( shuf -z -n "$nf" -e "${inclnm[@]}" )
如何修改两个替代方案以rsync
在每个文件上运行并发送到目的地?
解决方案
正如评论中所说,您不需要mapfile
中间数组。只需流式传输以空分隔的文件选择,rsync
如下所示:
#!/usr/bin/env bash
nf=4
inclnm=( a* b* )
# For testing purpose, destination is local host destfolder inside
# user home directory
destination="$USER@localhost:destfolder"
# Pipe the null delimited shuffled selection of files into rsync
shuf -z -n "$nf" -e "${inclnm[@]}" |
# rsync reads the null-delimited selection of from files from standard input
rsync -a -0 --files-from=- . "$destination"
如果您想收集随机选择的文件并使用它,rsync
请执行以下操作:
#!/usr/bin/env bash
nf=4
inclnm=( a* b* )
# For testing purpose, destination is local host destfolder inside
# user home directory
destination="$USER@localhost:destfolder"
# Capture the selection of files into the fl array
mapfile -d '' fl < <( shuf -z -n "$nf" -e "${inclnm[@]}" )
# Pass the fl array elements as sources to the rsync command
rsync -a "${fl[@]}" "$destination"
推荐阅读
- azure - 在逻辑应用程序中运行存储过程的 GatewayTimeout 问题
- sql-server - 使用 OPENXML 在 SQL Server 中获取标记和属性的名称
- angular - Angular ag-Grid 中的编辑、保存和删除按钮
- webstorm - WebStorm 列出未解析导入的文件
- ramda.js - 将函数列表应用于参数
- excel - Excel将参数作为文本转换为查询名称?
- flutter - 如何在单个列表中添加垂直和水平列表
- java - 从 kafka 组件调用控制器
- ios - 通过单击电子邮件链接查找是否安装了 iOS 应用程序 - IOS
- mongodb - 与记录的值相比,mongodb 聚合返回不同的值