Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to insert rm $0 in a script (/home/user/config-script) located on remote server with echo "rm $0" >> /home/user/config-script with expect. But fail because expect interprets $0.

var1="rm \$0"
expect -c 'spawn ssh [email protected] ;expect "password" ; send "123456\n"; \
  expect "@"; send "sudo -k\n"; expect "@"; send "sudo su\n"; expect "password" ; \
  send "123456\n" ;expect "@"; send "echo '$var1'>>/home/user/config-script\n"; \
  expect "@"; send "exit\n"; send "logout\n"; interact'

Any help? Thanks in advance.


missing "
    while executing
"send "echo rm"
couldn't read file "\$0>>/home/user/config-script\n"; expect "@"; send "exit\n"; send "logout\n"; interact": no such file or directory

In fact, on remote server I have an file called " /home/user/config-script". Like this :

#!/bin/bash
echo "my script"

finally, I want this script to be :

#!/bin/bash
echo "my script"
rm $0
share|improve this question

1 Answer 1

up vote 2 down vote accepted

var1="rm \$0" makes var1 take on the value rm $0. Then expect tries to expand the $0 when you run it.

To solve the problem, don't assign var1 the value rm $0. Assign it a value that expect will expand to rm $0:

var1="rm \\\$0"

(Or equivalently: var1='rm \$0')

This makes var1 take on a value of rm \$0 (which is perhaps what you had intended). Then the send "echo '$var1'>>/home/user/config-script\n"; statement will cause the correct command to run on the remote machine:

echo 'rm $0'>>/home/user/config-script

The final result would look like the following:

var1='rm \\\$0'
expect -c 'spawn ssh [email protected] ;expect "password" ; send "123456\n"; \
      expect "@"; send "sudo -k\n"; expect "@"; send "sudo su\n"; expect "password" ; \
      send "123456\n" ;expect "@"; send "echo '"$var1"'>>/home/user/config-script\n"; \
      expect "@"; send "exit\n"; send "logout\n"; interact'
share|improve this answer
    
I posted bellow so I can use the <code> –  georgian Jan 9 '13 at 11:32
    
@georgian I'm guessing that means the "answer" you posted is a reply to this, detailing the error that occurred when you tried the method I suggested. (By the way, instead of posting an answer--unless you really have found the solution--you should instead edit your question. You can still comment on answers to notify their authors, of course, just as you did here.) I'll take a look... –  Eliah Kagan Jan 9 '13 at 11:37
    
you assumed correctly. –  georgian Jan 9 '13 at 12:31
1  
problem solved. var1='rm \\\$0' and send "echo '"$var1"'>> . thanks –  georgian Jan 9 '13 at 13:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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