Eu fiz isso de maneira abaixo:
#!/bin/bash
ssh -t -t username@hostname << ENDSSH
cd /logs
echo "8. We are in SSH mode now" >./log_master.txt
end_pos=\$(/mysql/mysql/bin/mysqlbinlog mysql-bin.000001 |tail -10| grep -w -A1 154 | grep -w "Stop" |awk '{print \}')
echo \$end_pos;
if [[ "$end_pos" =~ Stop ]];
then
echo "Loop succeed" >>./log_master.txt
else
echo "loop failed" >>./log_master.txt
fi
exit;
ENDSSH