'BASH Script output now showing all echo commands

I'm using a bash script which is run on serverA and connects to serverB to run a file. The results are saved in a variable and then echo'd. However it doesn't echo all of the data.

The script on serverA is running:

count=$(sshpass -p password ssh -t -q user@serverB cd /home/tom && ./count.sh) 
echo "Count: $count"

This echos: 341 not Count: 341

The count.sh script on serverB is looping through some folders and doing a count of files.

Eg:

total=0

count=$(ls -l | wc -l | xargs)
if [ "$count" > 0 ]; then 
    total=$(( total + count ))
fi
echo "$total"

How do I display the full echo on serverA ?

Thanks



Solution 1:[1]

You are attempting to run ./count.sh on the local machine, not the remote host. The && is a command separator that terminates the sshpass command. Use quotes to ensure your desired shell command is passed to the remote host.

count=$(sshpass -p password ssh -t -q user@serverB 'cd /home/tom && ./count.sh')

I don't see anyway of producing the reported output, unless count.sh can run locally but something (are you using set -e?) prevents the following echo from executing at all.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chepner