1

Below shell script on SSH mode is not working.

variable end_pos is getting value "Stop", but when it comes in IF loop , it doesn't check the condition and loop is getting failed.

Output of command :

 /mysql/mysql/bin/mysqlbinlog mysql-bin.000001 |tail -10| grep -w -A1 154 | grep -w "Stop" |awk '{print \$10}' 

is STOP

echo \$end_pos; print STOP

 #!/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 \$10}')
echo \$end_pos;

 if  [ "\$end_pos" == "*Stop*" ];
  then
     echo "Loop succeed" >>./log_master.txt
 break
  else
      echo "loop failed" >>./log_master.txt
 fi
exit;
exit;
ENDSSH

After ilkKachu comment i put quotes in "ENDSSH" ( this is the only change i made) and executed .It threw error. Then i used second method of putting double brackets as shown below .

But when executed below script , i was getting this : if [[ "" = "Stop" ]];

  #!/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 \$10}')
 echo \$end_pos;

if  [[ "$end_pos" == "*Stop*" ]];
 then
 echo "Loop succeed" >>./log_master.txt
 break
 else
  echo "loop failed" >>./log_master.txt
fi
exit;
ENDSSH
2
ssh -t -t username@hostname << ENDSSH
...
echo \$end_pos;

If you don't need to expand any variables from the outer shell to the here-doc that goes to SSH, you can use << "ENDSSH" with quotes around the delimiter, to have the here-doc taken as if a single-quoted string. Then you don't need to escape every $ sign within it.

if [ "$end_pos" == "*Stop*" ]; then

If you want to compare the value against *Stop* as a pattern, and not as a literal string, you need to use [[ .. ]] or the more standard case:

if [[ $end_pos = *Stop* ]] ; then
    ...

# or
case "$end_pos" in 
    *Stop*) echo "stop found" ;;
    *) echo "stop not found" ;;
esac

Also, that break looks out of place, there is no loop. Bash will complain about that, but only if that part of the if-statement is executed. Two exits in a row seem a bit redundant, too.

  • Thanks for reply . but both ways it is not working. I have shown in edit part what i did after reading your comment – user21546 May 18 '17 at 6:08
  • @user21546, you don't have the quotes around the ENDSSH keyword, you only removed the escapes from the $s. Which means the variable gets expanded by the script calling ssh, and it doesn't have a value there, so you get empty. I meant you could write ssh -t ... << "ENDSSH" (or << 'ENDSSH'), literally, with the quotes. But it doesn't matter, if you leave the terminator unquoted, then just escape the $s, as in your original code. That part is just a style issue. – ilkkachu May 18 '17 at 7:38
0

I have done this in below way :

#!/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 \$10}')
 echo \$end_pos;

if  [[ "$end_pos" =~ Stop ]];
 then
 echo "Loop succeed" >>./log_master.txt
 else
 echo "loop failed" >>./log_master.txt
fi   
exit;
ENDSSH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.