Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8382527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:56:53+00:00 2026-06-09T16:56:53+00:00

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

  • 0
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 works in my bash script in a function.I need an exception when the password is wrong. My way is;

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"

But it gives the error

couldn't read file "denied,": no such file or directory

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

EDIT: ok this time it is better to have single quotes and it works.but what it can be done the if clause;

 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

‘)

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:56:54+00:00Added an answer on June 9, 2026 at 4:56 pm

    You either need to escape your other uses of ” characters within the string

    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
    ")
    

    Or you could switch to using single quotes, but you will need to exit the quotes to expand your variables

     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
    ')
    

    Basically, switching the types of quote used to quote the command within a single command argument. This works because a bash word argument can include a mix of quotes. Simple example

    ‘password='”$password”

    and you can combine as many quote switches as you like and as long as there is no space between the two different quotes, it will all be treated as part of the same word or command argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code, I would expect var to get resolved to an Int64
Given the following code, what should I expect to see in the alert? var
I would expect a Dictionary object of the form: var dict = new Dictionary<string,string>()
I wrote a regular expression which I expect should work but it doesn't. var
I don't expect you to understand what was meant with this piece of code:
In this code I would expect a call to ReleaseDC to happen in MyCanvas.Free
The following code shows what I expect in Firefox and Chrome: a small white
I'm having some issues with the following code: var tmpArray = new Array(); for(var
I am having trouble getting the following code to render as I expect it
I have instantiated a WCF binding in code: var binding = new WSHttpBinding(); binding.Secuity.Mode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.