Question

Script read file line by line and check folders on remote server using command ls

But my do-while - is work only 1 time, and ; for example: if try use rsync - all fine, while work correct, problem only with ssh user@server ls $SERVER_FOLDER >> $LOG i try use incorrect syntax? Error from console: syntax error near unexpected token 'done'

LOG="/path_to_log/log.txt"
FILE="/path_to_file/projects_id.txt"

cat $FILE | while read -r line || [[ -n $line ]]

do 

ID=$(echo $line | cut -d' ' -f3)

SERVER_FOLDER=`echo "/path_to_id/$ID/"`

echo "SERVER_FOLDER:" $SERVER_FOLDER

ssh user@server ls $SERVER_FOLDER >> $LOG

sleep 20

done
Was it helpful?

Solution

Add the -n option to ssh to prevent it from reading stdin. What is happening is that ssh is consuming all the input from the file (that is coming through stdin), so the while loop terminates after the first line because there is nothing left for it to read.

Change your code to:

while read -r line || [[ -n $line ]]
do 
    ID=$(cut -d' ' -f3 <<< "$line")
    SERVER_FOLDER="/path_to_id/$ID/"
    echo "SERVER_FOLDER: $SERVER_FOLDER"
    ssh -n user@server ls $SERVER_FOLDER >> $LOG
    sleep 20
done < "$FILE"

I have also made some other improvements such as changing the way you are reading the file (cat is not necessary).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top