使用mapfile调用rsync

Nig*_*gel 0 bash rsync map-files

使用以下 bash 代码来处理使用 mapfile 随机传递的文件名。我想调用rsync每个文件并将其发送到目标路径dpath

mapfile -d '' fl < <(
  find "$dpath" -maxdepth 1 -type f "${inclnm[@]}" |
  shuf -z -n "$nf"
)
Run Code Online (Sandbox Code Playgroud)

或者shuf直接处理参数

mapfile -d '' fl < <( shuf -z -n "$nf" -e "${inclnm[@]}" )
Run Code Online (Sandbox Code Playgroud)

如何修改两个替代方案以rsync在每个文件上运行并发送到目的地?

Léa*_*ris 5

正如评论中所述,您不需要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"
Run Code Online (Sandbox Code Playgroud)

如果您想收集随机选择的文件并将其用于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"
Run Code Online (Sandbox Code Playgroud)