$ ls
testscript.sh testservers.txt
$ cat testservers.txt
serverA IMM 10.2.3.4 USERID PASSW0RD
serverB IMM 10.2.3.5 USERID PASSW0RD
$
$
$ cat testscript.sh
#!/bin/bash
# test them..
egrep -vi '^#|^$' testservers.txt | while read ONELINE; do
# variables..
SERVER=`echo "$ONELINE" | awk 'BEGIN {FS="\t"} {print $1}'`
RSATYPE=`echo "$ONELINE" | awk 'BEGIN {FS="\t"} {print $2}'`
IP=`echo "$ONELINE" | awk 'BEGIN {FS="\t"} {print $3}'`
USER=`echo "$ONELINE" | awk 'BEGIN {FS="\t"} {print $4}'`
PWD=`echo "$ONELINE" | awk 'BEGIN {FS="\t"} {print $5}'`
if [ "$RSATYPE" = "IMM" ]; then
# main testing part for IMM
timeout 5 tsocks nc -z -w 3 "${IP}" 80 > /dev/null 2>&1; if [[ $? -eq 0 ]];
then WEBINTOK="true"
else WEBINTOK="false"
fi
IMMSSH=`(timeout 5 tsocks sshpass -p "${PWD}" ssh -l "${USER}" -o StrictHostKeyChecking=no "${IP}" exit)`
if echo "${IMMSSH}" | grep -q "tty name check failed"; then
${WEBINTOK} && echo -e "${SERVER} ${USER} - OK" || echo -e "${SERVER} ${USER} - ERROR"
else
${WEBINTOK} && echo -e "${SERVER} ${USER} - ERROR" || echo -e "${SERVER} ${USER} - ERROR"
fi
fi
done
$
$ bash testscript.sh
serverA USERID - OK
$
Our quesiton: why does the script only reads the first line?? It suppose to read all the lines in the testservers.txt ..
UPDATE: the testservers.txt has correct TAB's, I don't really think that is the problem :\
$ cat -vte testservers.txt
serverA^IIMM^I10.2.3.4^IUSERID^IPASSW0RD$
serverB^IIMM^I10.2.3.5^IUSERID^IPASSW0RD$
$
echo
? – LatinSuD Aug 13 '14 at 17:12FS
inawk
anyway. It defaults to whitespace (that includes a space and a\t
). – Joseph R. Aug 13 '14 at 17:13...echo "$ONELINE" | read SERVER RSATYPE IP USER PWD...
? It doesn't solve your immediate problem, but it definitely makes for more readable code. – Joseph R. Aug 13 '14 at 17:16-e
set and the script exits prematurely due to an error? – Joseph R. Aug 13 '14 at 17:23nc
eats the whole of the standard input. (your code is awful btw). – Stéphane Chazelas Aug 13 '14 at 17:25$()
. – ctrl-alt-delor Aug 13 '14 at 17:39