1

I have a file with some lines in it:

/snapshot/200
/snapshot/201
/snapshot/202

Now i'm trying to use each line with a while IFS loop to do certain actions with them.

while IFS=' ' read -r test || [ -n "${test}" ];
do
    if ssh root@$TARGET "[ ! -d $test ]"
    then
        echo "${test}"
    fi
done < $filelist

The problem here I only get one result and it's not looping through the whole file? The if statement should be correct, since there are no directories with these names.

If however i remove the entire if part and just use

echo "${test}"

the loop works.

1

1 Answer 1

4

ssh inherits the standard input stream from the while loop and will read your whole $filelist file, apart from its first line which is read by read.

To stop ssh from doing this, use ssh -n, or ssh ... </dev/null.

ssh works this way to to allow you to pass data via a simple redirection or pipe to the remote system, as in

ssh remote 'cat >remotefile' <localfile

See also "Need help understanding shell command with pipes, redirections and remote connections".

1
  • Thanks! Never imagined SSH is the culprit!
    – Slimo
    Commented May 4, 2020 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .