David Bolt wrote:
while read DESTHOST; do if ssh $DESTHOST '[[ -d "$DIRNAME" || $(mkdir -p "DIRNAME") -eq 0 ]]'; then
You're missing a '$' from the front of DIRNAME. You can take the mkdir out of the test as well. Just test for the directory and, if it's not present do the mkdir. If either the check for the directory or the mkdir is successful, the ssh will return '0'.
I shouldn't code after midnight ;-)
Also, one thing that's very likely to be breaking it is that you've wrapped the string in single quotes, $DIRNAME is passed to the shell on $DESTHOST as is, which means that the test and mkdir will fail because $DIRNAME will be an empty variable. That is unless it is defined on $DESTHOST as a part of the environment.
That hit the nail on the head! I caught that after Pit's comments, and could have shot myself.
if scp ${DIRNAME}/${FILENAME} $DESTHOST:${DIRNAME}/${FILENAME}; then
Purely out of curiosity, why are you using scp rather than rsync?
No reason. I use rsync for just about everything else and scp was already in my old script I started modifying last night.
Okay. I did a rewrite and this does what you appear to want to do:
davjam@playing:~> cat testscript ; ls -l temp/testscript ; bash ./testscript temp/testscript #!/bin/bash
#set -x
# should be a check in case no arguments passed # [ "$#" -eq 0 ] && exit
red='\e[0;31m' # ${red} lightgreen='\e[1;32m' # ${lightgreen} nc='\e[0m' # ${nc}
FILENAME=$(basename $1) if [ "${DIRNAME::1}" == '/' ]
Now that's a new trick -- no 0.
then DIRNAME=$(dirname $1) else DIRNAME=${PWD}/$(dirname $1) fi
TESTSTRING="[ -d \"$DIRNAME\" ] || mkdir -p \"$DIRNAME\""
for DESTHOST in $(<${HOME}/testhosts)
Also a new trick, Thanks!
do if ssh $DESTHOST "$TESTSTRING" then rsync -aP ${DIRNAME}/${FILENAME} $DESTHOST:${DIRNAME}/ &>/dev/null if [ "$?" -eq 0 ] then echo -e "Transfer to $DESTHOST ${lightgreen}[OK]${nc}" else echo -e "Transfer to $DESTHOST ${red}[Failed]${nc}" fi else echo "$DESTHOST is Unavailable" fi done -rw-r--r-- 1 davjam users 733 2009-02-21 10:08 temp/testscript Transfer to adder.davjam.org [OK] Transfer to thargon.davjam.org [OK] Transfer to lion.davjam.org [OK] Transfer to cobra-mk3.davjam.org [OK]
Regards, David Bolt
David, thank you! The veil of fog has been lifted. The key was TESTSTRING to expand $DIRNAME before making the ssh call. That's what had me wrapped around the axle. Good to be with you. -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org