I learned from the following sources:
curl -O: Download a file with curl on Linux / Unix command line- jq: How to urlencode data for curl command?
- Multiple files and
curl -J: download pdf files from using curl - Condition
forloop: Shell: how to use 2 variables with for condition and Unable to download data using curl for loop
Description of the script:
Variables, required by GitLab's Repository files API:
branch="master" repo="my-dotfiles" private_token="XXY_wwwwwx-qQQQRRSSS" username="gusbemacbe"I used a declaration for multiple files:
declare -a context_dirs=( "home/.config/Code - Insiders/Preferences" "home/.config/Code - Insiders/languagepacks.json" "home/.config/Code - Insiders/rapid_render.json" "home/.config/Code - Insiders/storage.json" )I used the condition
forloop withjqto convert all files from the declarationcontext_dirsto encoded URLs:for urlencode in "${context_dirs[@]}"; do paths=$(jq -nr --arg v "$urlencode" '$v|@uri') doneI used the condition
forloop to download withcurlmultiple files taken frompathsconverted byjq. It is important that I used-0and-Jto output the file name, and-Hfor"PRIVATE-TOKEN: $private_token":for file in "${paths[@]}"; do curl -sLOJH "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$username%2F$repo/repository/files/$file/raw?ref=$branch" done
Complete source code:
branch="master"
id="1911000X"
repo="my-dotfiles"
private_token="XXY_wwwwwx-qQQQRRSSS"
username="gusbemacbe"
declare -a context_dirs=(
"home/.config/Code - Insiders/Preferences"
"home/.config/Code - Insiders/languagepacks.json"
"home/.config/Code - Insiders/rapid_render.json"
"home/.config/Code - Insiders/storage.json"
)
for urlencode in "${context_dirs[@]}"; do
paths=$(jq -nr --arg v "$urlencode" '$v|@uri')
done
for file in "${paths[@]}"; do
curl -sLOJH "PRIVATE-TOKEN: $private_token" "https://gitlab.com/api/v4/projects/$username%2F$repo/repository/files/$file/raw?ref=$branch"
done
But the two conditions for loop output only an encoded path and downloaded only a file.