LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2013, 05:03 AM   #1
georgian
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Rep: Reputation: Disabled
"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.

Last edited by georgian; 01-09-2013 at 05:05 AM.
 
Old 01-10-2013, 12:44 PM   #2
steelneck
Member
 
Registered: Nov 2005
Distribution: Slackware, Arch
Posts: 43

Rep: Reputation: 8
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'

Last edited by steelneck; 01-10-2013 at 12:48 PM.
 
Old 01-11-2013, 08:42 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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" ;
 
Old 01-12-2013, 06:01 AM   #4
georgian
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I use "expect" to supply details after 5 line intro appears, e.g. password for ms1 Linux - Newbie 11 07-06-2012 05:50 AM
[SOLVED] The expect -c "spawn ls" check in section 6.12 of the book gives a segmentation fault ezekielrage Linux From Scratch 1 05-18-2011 08:58 AM
EXPECT/TCL: the "source" command and passing arguments qmqmqm Programming 4 04-02-2009 05:36 PM
My "expect" script is not working... Won't "send" commands... edomingox Programming 4 04-02-2009 03:25 PM
Didn't install "expect-5.43" by mistake - how big a problem is it? drut Linux From Scratch 2 05-05-2008 08:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration