LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   "rm $0" in expect (https://www.linuxquestions.org/questions/programming-9/rm-%240-in-expect-4175444790/)

georgian 01-09-2013 05:03 AM

"rm $0" in expect
 
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 .

Code:

var1="rm \$0"
expect -c 'spawn ssh user@192.168.1.200 ;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.

steelneck 01-10-2013 12:44 PM

I think it is bash that interprets the $0, try single-quoting the var1 and skip the same in the expect command. (have never used expect, so...)

Code:

var1='rm $0'

David the H. 01-11-2013 08:42 AM

Actually, these two lines should work the same.
Code:

var1="rm \$0"
var1='rm $0'

A backslash inside double quotes will escapes "$", so it wouldn't be seen as a variable. I'd still prefer the second one for readability purposes, however.


Now I'm not familiar with expect either, but in any case when you have a long string of commands to pass to an external program, it usually pays to store them in a separate variable first. It also helps readability to separate each command out in a separate line.

Code:

var1='rm $0'
commands='
  spawn ssh user@192.168.1.200 ;
  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
'
expect -c "$commands"

Now we can see things more clearly. The line in question is this:

Code:

'...
send "echo '$var1'>>/home/user/config-script\n" ;
...'

Which after shell parsing is received by expect as this:

Code:

...
send "echo rm $0>>/home/user/config-script\n" ;
...

So you are correct. It leaves $0 unprotected and subject to interpretation by expect.

It took me a bit of trying to get it right, but all you really need to do is ensure that expect gets the values properly backslashed:

Code:

var1='rm \$0'

commands='
  spawn ssh user@192.168.1.200 ;
  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
'
expect -c "$commands"

It seems to work in my tests, at least. YMMV.

BTW, do you really need the extra $var1 step in the first place? Can you not just hard-code it in with the rest of the commands?

Code:

send "echo \"rm \$0\" >>/home/user/config-script\n" ;

georgian 01-12-2013 06:01 AM

Quote:

Originally Posted by David the H. (Post 4867751)
Actually, these two lines should work the same.
Code:

var1='rm \$0'

commands='
  spawn ssh user@192.168.1.200 ;
  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
'
expect -c "$commands"

It seems to work in my tests, at least. YMMV.

BTW, do you really need the extra $var1 step in the first place? Can you not just hard-code it in with the rest of the commands?

Code:

send "echo \"rm \$0\" >>/home/user/config-script\n" ;

Thanks @David the H.

For me only works with:
Code:

var1='rm \\\$0'
send "echo  '"$var1"' >>/home/user/config-script\n" ;

or hardcoded:
Code:

send "echo  '"'rm \\\$0'"' >>/home/user/config-script\n" ;
Thank you very much again


All times are GMT -5. The time now is 02:23 PM.