| View previous topic :: View next topic |
| Author |
Message |
earnstaf Voice
Joined: 19 Jul 2007 Posts: 3
|
Posted: Thu Jul 19, 2007 5:26 pm Post subject: Case Structure Equivalent? |
|
|
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 |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Jul 19, 2007 5:33 pm Post subject: |
|
|
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 |
|
 |
earnstaf Voice
Joined: 19 Jul 2007 Posts: 3
|
Posted: Thu Jul 19, 2007 6:21 pm Post subject: |
|
|
| 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 |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Jul 19, 2007 6:39 pm Post subject: |
|
|
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 |
|
 |
earnstaf Voice
Joined: 19 Jul 2007 Posts: 3
|
Posted: Thu Jul 19, 2007 6:46 pm Post subject: |
|
|
| 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 |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
|
| Back to top |
|
 |
Alchera Revered One

Joined: 11 Aug 2003 Posts: 3344 Location: Ballarat Victoria, Australia
|
|
| Back to top |
|
 |
|