egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Case Structure Equivalent?

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
earnstaf
Voice


Joined: 19 Jul 2007
Posts: 3

PostPosted: Thu Jul 19, 2007 5:26 pm    Post subject: Case Structure Equivalent? Reply with quote

Hi all,
I'm very new to tcl/expect and I'm looking for a push in the right direction on this issue. I'm writing an expect script to log into a router and push a file that varies. I was thinking of using something like a case structure in shell scripting:
Code:

echo -n "What file would you like to push? Press 1 for file1, 2 for file2 or 3 for both: "; read file
case $file in
  1) [i]expect and send commands for file1[/i] ;;
  2) [i]expect and send commands for file2[/i];;
  3) etc...
esac


Is there an efficient way to do this with expect/tcl? Or can I write the script as a shell script, call the Expect script and use the shell variables within the expect script?

Thanks for any guidance you can provide.
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Thu Jul 19, 2007 5:33 pm    Post subject: Reply with quote

Roughly something like this:
Code:
switch $file {
 1 {command 1}
 2 {command 2}
 3 -
 4 {command for 3 or 4}
 default {any other command}
}


Also check the manual for switch for more info/hints...
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
earnstaf
Voice


Joined: 19 Jul 2007
Posts: 3

PostPosted: Thu Jul 19, 2007 6:21 pm    Post subject: Reply with quote

nml375 wrote:
Roughly something like this:
Code:
switch $file {
 1 {command 1}
 2 {command 2}
 3 -
 4 {command for 3 or 4}
 default {any other command}
}


Also check the manual for switch for more info/hints...


Thanks nml. That looks like exactly what I need.

How do I prompt the user to enter the 1, 2 or 3? Is it something like this?
Code:

send_user "What file would you like to push? Press 1 for file1, 2 for file2 or 3 for both: "
expect_user -re "(.*)\n" {set FILE $expect_out(1,string)}


Seems to be working the way I have it setup.. if there is a better way, please let me know.

Thanks.


Last edited by earnstaf on Thu Jul 19, 2007 6:39 pm; edited 1 time in total
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Thu Jul 19, 2007 6:39 pm    Post subject: Reply with quote

Well, if you're coding for eggdrop, you'll have to use several procs and bindings, each triggering and handling each step: ie, one proc to ask the user for input, and another for handling the response.
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
earnstaf
Voice


Joined: 19 Jul 2007
Posts: 3

PostPosted: Thu Jul 19, 2007 6:46 pm    Post subject: Reply with quote

nml375 wrote:
Well, if you're coding for eggdrop, you'll have to use several procs and bindings, each triggering and handling each step: ie, one proc to ask the user for input, and another for handling the response.


I'm not real sure what eggdrop is... sorry! Your guidance in tcl/expect scripting was helpful though ...

I get the user response via this code
Code:

send_user "What file would you like to push? Press 1 for file1, 2 for file2 or 3 for both: "
expect_user -re "(.*)\n" {set FILE $expect_out(1,string)}

then I setup the switch as you outlined:
Code:

switch $FILE {
  1 { lots of commands }
  2 { lots of commands }
  3 { lots of commands }
}


One the user enters a number 1-3, and it's saved to the $FILE variable, it seems to work with switch to jump to the appropriate code.

Thanks for your help... it's definitely a big learning curve when going from shell scripting to tcl.
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Thu Jul 19, 2007 7:22 pm    Post subject: Reply with quote

What is an Eggdrop? This is a forum specifically for Eggdrop Tcl scripting but of course other Tcl related help requests are welcome Wink
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
Alchera
Revered One


Joined: 11 Aug 2003
Posts: 3344
Location: Ballarat Victoria, Australia

PostPosted: Thu Jul 19, 2007 8:41 pm    Post subject: Reply with quote

The Expect Home Page
_________________
Add [SOLVED] to the thread title if your issue has been.
Search | FAQ | RTM
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber