Hello everyone,
I'm trying to set four variables while in one ssh connection.
The problem I'm running into is with my first variable. I want to know whether or not a file called 'shins' is present in the root directory.
When the file IS present, the variables work fine. But when the file is NOT present, there is nothing to put into it, so the script puts the value from the command below it in $VarZro.
I guess the best option would be to assign a "not present" status to $VarZro, like a "file not found" exit status. Can someone let me know how I would go about doing this?
Thanks.
Code:
#!/bin/bash
for STORE in `cat hostlist.txt`
do
ping -c 1 $STORE > /dev/null 2>/dev/null
PingResult=$?
if [ "$PingResult" -eq 0 ]; then
allVars=( $( ssh root@$STORE "\
ls shins > /dev/null 2>/dev/null; \
pwd ; \
ls /etc/hosts ; \
ls /etc/sysconfig/network ; " ) )
varZro="${allVars[0]}"
VarOne="${allVars[1]}"
VarTwo="${allVars[2]}"
VarThr="${allVars[3]}"
echo "=================="
echo "Hostname - $STORE"
echo "shins - $varZro"
echo "pwd - $VarOne"
echo "etc/host - $VarTwo"
echo "network - $VarThr"
elif [ "$PingResult" -eq 1 ]; then
echo "============================"
echo "$STORE"
echo "-------- Store Down --------"
elif [ "$PingResult" -eq 2 ]; then
echo "============================"
echo "$STORE"
echo "--- $STORE: UNKNOWN HOST ---"
fi
done
Output for command:
Code:
[techs@ts-01 bash]$ ./var-test.sh
==================
Hostname - ft9587
shins - /root
pwd - /etc/hosts
etc/host - /etc/sysconfig/network
network -
As you can see, the 'shins' echo shows the value for the pwd command, which assigns incorrect variables for all the variables that follow, and leaves the last variable blank.