This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Case Structure Equivalent?

Help for those learning Tcl or writing their own scripts.
Post Reply
e
earnstaf
Voice
Posts: 3
Joined: Thu Jul 19, 2007 5:15 pm

Case Structure Equivalent?

Post by earnstaf »

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: Select all

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.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Roughly something like this:

Code: Select all

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
e
earnstaf
Voice
Posts: 3
Joined: Thu Jul 19, 2007 5:15 pm

Post by earnstaf »

nml375 wrote:Roughly something like this:

Code: Select all

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: Select all

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.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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
e
earnstaf
Voice
Posts: 3
Joined: Thu Jul 19, 2007 5:15 pm

Post by earnstaf »

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: Select all

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: Select all

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.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

What is an Eggdrop? This is a forum specifically for Eggdrop Tcl scripting but of course other Tcl related help requests are welcome ;)
User avatar
Alchera
Revered One
Posts: 3344
Joined: Mon Aug 11, 2003 12:42 pm
Location: Ballarat Victoria, Australia
Contact:

Post by Alchera »

Add [SOLVED] to the thread title if your issue has been.
Search | FAQ | RTM
Post Reply