正如评论中所说,您不需要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"