What is causing this script to exit the loop at line "program is exiting loop here"? In the context it will loop twice but it won't if i have the CM_CHECK_STATUS
where i have it.
RUNLOG="log"
OUTPUTFILE="output"
LIST=$(/usr/local/bin/sshpass -f $PASSWORD_FILE ssh -o StrictHostKeyChecking=no $USER@$IP "ls -al" 2>> $RUNLOG > $OUTPUTFILE)
PS_VALS=$(cat $OUTPUTFILE)
if [[ $? != 0 ]]
then
echo "There was an error at:" date
>> $RUNLOG
else
while IFS= read -r line;
do
# Check ls
echo "... $line ..."
CM_CHECK_STATUS=$(/usr/local/bin/sshpass -f $PASSWORD_FILE ssh -o StrictHostKeyChecking=no $USER@$IP "ls -al")
#program is exiting loop here
done <<< "$PS_VALS"
echo "finished"
fi
If i remove CM_CHECK_STATUS=$(/usr/local/bin/sshpass -f $PASSWORD_FILE ssh -o StrictHostKeyChecking=no $USER@$IP "ls -al")
and place it anywhere else outside the loop that command works fine and the loop runs as expected. Is there something wrong with using that command inside a loop?
set -x
to generate debug output. A few comments that are not directly related to your question: When you check $?, you check the success of thecat
command. I wonder if that is your intention. Why do you use<<<
instead of just redirecting input from $OUTPUTFILE. And processing text files withwhile... read
can be a bad idea, one being that something in the loop body may consume the input, leading to a premature EOF. Which may well be the case here, but right now my mind is unable to cut through thesshpass
commands. – berndbausch Apr 18 '21 at 04:14sshpass
instead of ssh with key is i'm connecting to a embedded ESXi system running on a flashdrive in a dell server. The default setup doesn't allow me to easily persist ssh public key or otherwise I would use regular ssh.sshpass -f
lets me secure pw in file on machine running this script in a cron job. – jtlindsey Apr 18 '21 at 05:02ssh
inheriting standard input from the loop and reading all that is left to read. The solution to this is to redirect/dev/null
intossh
, ro to startssh
with its-N
option (which has the same effect). There are several other issue with your script that you may want to ask separate questions about, or brows question and answers about on this site. For example, how to feed the output of a command into a loop (without storing the result in an intermediate variable), and what command substitutions are usually used for (not to get the "status" from a command). – Kusalananda Apr 18 '21 at 09:49ssh
was causing the problem.set -x
just confirmed it was in fact exiting the loop where the ssh command was but i didn't consider it being the problem because that same line works in other places. adding</dev/null
to end of the command fixed the problem. – jtlindsey Apr 18 '21 at 12:03