Using loops in SH script?

Hello

I wrote a simple script to backup a bunch of folders from one place to another over my LAN using rsync.

Although this works, I now want to change it so it backs up the same folder set to TWO places (or more in the future).

For now, I have literally copied and pasted the rsync commands (and in the second iteration, changed the destination server) as per the example below:-


 copy folder 1 to server1
 copy folder 2 to server1
 ...
 copy folder N to server1


 copy folder 1 to server2
 copy folder 2 to server2
 ...
 copy folder N to server2

Is there a way using SH script to do arrays & loops so it would be:-


server_array(0) = "server1"
server_array(1) = "server2"

for each item in server_array
 copy folder 1 to item
 copy folder 2 to item
 ...
 copy folder N to item
next item

That way, if I change the list of folders to copy, I don’t need to make lots of changes, I just make one change (in the main loop).

Thank you for your time in reading this and any ideas you may have to help me :slight_smile:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sure… ‘for’ loops (like many others) are valid. For example:

<code interpreter="/bin/bash">
for i in a b c d e f; do echo $i; done
</code>

Good luck.

badger fruit wrote:
> Hello
>
> I wrote a simple script to backup a bunch of folders from one place to
> another over my LAN using rsync.
>
> Although this works, I now want to change it so it backs up the same
> folder set to TWO places (or more in the future).
>
> For now, I have literally copied and pasted the rsync commands (and in
> the second iteration, changed the destination server) as per the example
> below:-
>
>
> Code:
> --------------------
>
> copy folder 1 to server1
> copy folder 2 to server1
> …
> copy folder N to server1
>
>
> copy folder 1 to server2
> copy folder 2 to server2
> …
> copy folder N to server2
>
> --------------------
>
>
> Is there a way using SH script to do arrays & loops so it would be:-
>
>
> Code:
> --------------------
>
> server_array(0) = “server1”
> server_array(1) = “server2”
>
> for each item in server_array
> copy folder 1 to item
> copy folder 2 to item
> …
> copy folder N to item
> next item
>
> --------------------
>
>
> That way, if I change the list of folders to copy, I don’t need to make
> lots of changes, I just make one change (in the main loop).
>
> Thank you for your time in reading this and any ideas you may have to
> help me :slight_smile:
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJKfFCPAAoJEF+XTK08PnB5cjUP/2Hb0Ithk+OF/e2jfWAbbPvO
chSnliiwwlJMfyo8sndGFY9u/RRuAHWzcmLBF1lu87MO5gXCntWIM+/yx5HPNqjC
UfT/oc0+ydGnBapa6eYsWojhKxIwZM+HlXJJzOQjpy16/Ms5EjcCNPfpQPOzkmMn
aUNROS5OA5no7VGL8Y+TRweJha1brt7uJpY1ZPH0H2jjC2zdMZ5xFumKOqNr6BLp
aSzV7NNr4UFjCgot65fIcD6/WmebVvS4RN15nbpvTEWwhi+2QjLTAXNSQ7KEys0Q
OK1j5hTKYE57qApuo6GqGeAXc7N9DAhpMwbFJ/WWN3OcriURc+apUykQmxRzcT3x
SIDvZrDnBuNkXr9yZfPPMZjqi92kB+9RN7IuEebSJR2Fa9/wKrxQx86ICEEtVqOs
ls1BT6RAOY1+11gNkBxEW9FTfh7EBXfBy8peu9syp1LNZSCvehtazSQlN6pCA69u
ziMwpCKDRkkAb3A92E4FsiOYyFR/t1peOdYkwRxN84kraTNjYdEdgTuJiB8Z0Y1C
i/ionVGy5eGdVWA9ZBN73xFuBpqJbQ9D/n8M0+4gAepaV7kdAA1rZbhlpyuW2nRE
1+Oa9EFlaN79Ya6SIZWMFWgdpdv7JdQvrydGGcgeeD4tfNE0Z0VRm4cNUuAoVuHt
NklVWSm15Kv3m2I5AQvF
=dvzb
-----END PGP SIGNATURE-----

Provided you don’t have any whitespace in your server names, you can even set the names from a string, useful for editing the list in one place.


SERVERS="alice bob charlie"

for s in $SERVERS
do
  rsync blah blah $s:
done