简体   繁体   中英

Looping through list with parameters in Bash: how?

This is more of a theoretical question, but let's say I have a master Rsync script that I'd like to use to backup multiple remote hosts to the same destination.

I'd like to have the script read a text file with each line containing hostname, source, destination, along the lines of this:

host-one.example.com /Volumes/data /Volumes/host-one-backup
host-two.example.com /Volumes/Users /Volumes/host-two-backup
host-three.example.com /Volume/apps /Volumes/host-three-backup

And then a simple Rsync script that will pop in each of the three values accordingly, run that Rsync job, then go to the next, etc., within a looping script.

I'm good with everything except how to read the three values as variables within a script.

Your ideas or suggestions are most appreciated!

Cheers, Dan

while read hostname source destination; do
    rsync -a "$hostname:$source" "$destination"
done < backup-list.txt

The cool/tricky part is treating the whole loop as a compound command and applying a redirection to it. This causes each read call to read a line from the file since stdin is redirected for the duration of the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM