LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-19-2011, 10:31 AM   #1
auma78
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Rep: Reputation: 0
Smile how to change the interperter in the middle of the script


hi
i am running a script that reads some inuts and do some text modification in it, then i will have to telnet to a remote server to execute some commands within the same script. the problem is on the first part i am under #!/bin/bash. Later when i have to connect to the remote server i have to switch to #!/usr/bin/expect.... as the default shell does not recongnize (expect and send commands).. also the /bin/expect shell does not recognize some commands like echo. is there a way to switch the shell in the middle of a running script?

#! /bin/bash
echo "enter the id"
read id
...
....
...
#!/usr/bin/expect
spawn telnet 172.20.64.133
expect "ENTER USERNAME <"
send "kkkk\r"
expect "ENTER PASSWORD <"
..
...
...
 
Old 01-19-2011, 10:53 AM   #2
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
I'm not aware of anyway to do that, but is there any reason why you can't have two separate scripts, one interpreted by bash that passes the necessary parameters to the one interpreted by expect?

[edit]
Alternatively, you can use a "here document" to supply the commands to expect, which is called by the bash script.

eg.:
Code:
#! /bin/bash
echo "enter the id"
read id
...
...
...
/usr/bin/expect <<LimitString
spawn telnet 172.20.64.133
expect "ENTER USERNAME <"
send "kkkk/r"
expect "ENTER PASSWORD<"
...
...
...
LimitString

Last edited by tsg; 01-19-2011 at 11:14 AM. Reason: another idea
 
1 members found this post helpful.
Old 01-19-2011, 11:14 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
You can use expect for all of it, then any lines which need to be in bash can be run using
Code:
#!/usr/bin/expect
bash -c "echo foo"
There's probably a similar option for expect, but I don't currently have that installed on my system
 
Old 01-19-2011, 03:34 PM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Or you could do it the other way, which IMO would be easier...

Code:
#!/bin/bash
...
...
path1=`which expect`
`$path1 spawn telnet 172.20.64.133`
Something like that... I would test it, but I'm at work.

And snark, I gave you rep yesterday just so you can love me
 
Old 01-20-2011, 06:04 AM   #5
auma78
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Original Poster
Rep: Reputation: 0
thanks guys,
tsg way worked fine
the second one gives me (bash : unknown command name)
i will test the 3rd one as well.
 
Old 01-20-2011, 09:28 AM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Yeah, the limitstring way would work out just as well. Props to him :P
 
Old 01-20-2011, 09:42 AM   #7
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
Quote:
Originally Posted by corp769 View Post
Yeah, the limitstring way would work out just as well. Props to him :P
Am I missing something?
 
Old 01-20-2011, 09:46 AM   #8
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Am I missing something?
No, I think he was just saying your solution was as good as his

@auma78:Well done for giving rep to tsg - if it's solved, then it's also polite to mark the thread as '[SOLVED]' (using the Thread Tools menu).

@corp769: it's okay, I love you already
 
Old 01-20-2011, 09:50 AM   #9
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
Quote:
Originally Posted by Snark1994 View Post
No, I think he was just saying your solution was as good as his
Ah, okay. My sarcasm detector is on the fritz again
 
Old 01-20-2011, 09:55 AM   #10
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Teehee, snark, .... ehh, I would get banned if I said what I wanted to say... LOL
 
Old 01-20-2011, 11:09 AM   #11
auma78
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Original Poster
Rep: Reputation: 0
sorry i am new here
i will mark it as [sloved]
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Start script from middle liny1984 Linux - Newbie 7 11-15-2010 05:17 PM
script to add text in middle of file Plato Linux - Newbie 3 07-22-2009 05:39 PM
cron stops in the middle of the script djzanni Linux - Newbie 6 06-02-2005 05:29 PM
Change batch script to shell script alan.belizario Programming 5 03-31-2005 12:41 AM
Change batch script to shell script alan.belizario Linux - Software 1 03-30-2005 01:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:37 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