On Fri, 20 Feb 2009, David C. Rankin wrote:-
Listmates,
I have run into another bash caveat I don't understand. Evidently, the ssh call I am making is returning an "exit" following execution of the first pass and never getting to the rest of the hostnames I want to test. It is part of a script that will take a new or modified file and scp it to the same directory on my list of remote hosts. The relevant portions of the script are:
FILENAME=`basename $1` if [[ ${DIRNAME:0:1} == '/' ]]; then DIRNAME=`dirname $1` else DIRNAME=${PWD}/`dirname $1` fi
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'. 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.
if scp ${DIRNAME}/${FILENAME} $DESTHOST:${DIRNAME}/${FILENAME}; then
Purely out of curiosity, why are you using scp rather than rsync?
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 < /home/david/linux/scripts/network/dsshosts
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}" == '/' ] then DIRNAME=$(dirname $1) else DIRNAME=${PWD}/$(dirname $1) fi TESTSTRING="[ -d \"$DIRNAME\" ] || mkdir -p \"$DIRNAME\"" for DESTHOST in $(<${HOME}/testhosts) 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 -- Team Acorn: http://www.distributed.net/ OGR-NG @ ~100Mnodes RC5-72 @ ~1Mkeys/s | openSUSE 10.3 32b | openSUSE 11.0 32b | openSUSE 10.2 64b | openSUSE 10.3 64b | openSUSE 11.0 64b | openSUSE 11.1 64b TOS 4.02 | openSUSE 10.3 PPC | RISC OS 3.6 | RISC OS 3.11 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org