Today I wanted to take all the elements of a bash array and append /** to each one. This was for a script that syncs selected photo albums to cloud storage using rclone. As you know, rclone syncs directories, so to limit the transfer to a subset of of the directories, I used an --include pattern. Say the albums are cny13 fiji13 misc13 under archived. Then the command required is:
rclone sync archived mydrive:Photos --include "{cny13/**,fiji13/**,misc13/**}"
So the question is how to get this from an array containing:
declare -a ALBUMS=(cny13 fiji13 misc13)
Of course, I could run a small loop where I append /** to each element and accumulate in another array. This does work and efficiency isn't really an issue. But I guess the old APL fan in me was awakened and I wondered if I could transform the whole array in one fell swoop.
I tried:
echo "${ALBUMS[@]}/**"
but this only got me:
cny13 fiji13 misc13/**
The clue was supplied in an online tutorial on bash arrays in which an example was shown of parameter substitution. This in fact works just as well on arrays element by element.
echo "${ALBUMS[@]/%/\/**}"
This substitutes the end of line with /** escaping the leading slash, so we get:
cny13/** fiji13/** misc13/**
I haven't explained how the , is inserted between items, but the entire script using another trick to create a join function shows it:
#!/bin/bash
function join { local IFS="$1"; shift; echo "$*"; }
declare -a ALBUMS=(cny13 fiji13 misc13)
declare -a includes=("${ALBUMS[@]/%/\/**}")
cd ~/Albums || exit 1
albums=$(join , ${includes[@]})
rclone sync archived mydrive:Photos --include "{$albums}"
No comments:
Post a Comment