title: expect exception handling for ssh password in bash
body: <pre><code>VAR=$(expect -c "
       spawn ssh-copy-id -i $SSH_KEY_PATH_PUB $REMOTE_HOST_USER@$REMOTE_HOST_IP    
       expect "*?assword:*"
       send \"$REMOTE_HOST_PASSWD\r\";  
       send -- "\r"
       expect eof
      ")
  
  #echo "$VAR"
  </code></pre>
  
  <p>Code works in my bash script in a function.I need an exception when the password is wrong. My way is;</p>
  
  <pre><code>VAR=$(expect -c "
       spawn ssh-copy-id -i $SSH_KEY_PATH_PUB $REMOTE_HOST_USER@$REMOTE_HOST_IP    
       expect "*?assword:*"
       send \"$REMOTE_HOST_PASSWD\r\"; 
       expect "Permission denied, please try again."
       send user "Wrong pass"
       exit
       send -- "\r"
       expect eof
      ")
  
  #echo "$VAR"
  </code></pre>
  
  <p>But it gives the error</p>
  
  <pre><code>couldn't read file "denied,": no such file or directory
  </code></pre>
  
  <p>I think it is about quotoing.when I change the expect -c " to single quotes expect -c ', this time it gives error that can not find $SSH_KEY_PATH_PUB cause it is a variable in script.SO I think it goes out the scope.what is the solution</p>
  
  <p>EDIT: ok this time it is better to have single quotes and it works.but what it can be done the if clause;</p>
  
  <pre><code> AR=$(expect -c '
   spawn ssh-copy-id -i '"$SSH_KEY_PATH_PUB $REMOTE_HOST_USER@$REMOTE_HOST_IP"'
   expect "*?assword:*"
   send "'"$REMOTE_HOST_PASSWD"'\r";
   expect "Permission denied, please try again."
   send user "Wrong pass"
   exit
   send -- "\r"
   expect eof
  </code></pre>
  
  <p>')</p>
  
  <p>this time, if pass is ok it makes job and if pass not correct it does not. but when the pass is correct it writes the "wrong pass" stage. what it can be done so if the pass is correct it must exit and not continue.Sorry I am not familiar with expect and its syntax</p>