Re: distributing files with bash



On 2007-05-09, osiris@xxxxxxxxxx wrote:
I wrote this bash script in order to distribute my .bashrc
and .bash_profile to other hosts that I regularly use
but it doesn't seem to work. One problem is that
somehow "${#HOSTS}" which should contain the number of
elements in the array entitled HOSTS takes a value of 17
for some odd reason. It also doesn't copy the files
even though it seems to execute them when I run the script
with:

bash -x myscript


HOSTS=( isis@xxxxxxxxxxxx horus@xxxxxxxxxxxx aha@xxxxxxxxxxxx )
I=0
while [ "$I" -le "${#HOSTS}" ] ; do
echo "scp of .bashrc and .bash_profile to ${HOSTS[$I]}"
scp ~/.bashrc ${HOSTS[$I]}
scp ~/.bash_profile ${HOSTS[$I]}
I=$(( $I + 1 ))
done

You are missing a colon after the hostname.

And you don't need two calls to scp for each host:

for host in "${HOSTS[@]}"
do
echo "scp of .bashrc and .bash_profile to $host"
scp ~/.bashrc ~/.bash_profile "$host:"
done


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.