Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

expect script - nested loop or case type statement.

Status
Not open for further replies.

davidbelarus

Programmer
Oct 21, 2011
26
US
i need to do a classic case statement, i wanted some suggestions of how to implement it.

my pseudocode is like this:

if version is 1200 or below, then
{
machine1, use this line

machine2, use this line

machine3, use this line

... 6 unique machines like that.
}
else
#if buld is more than 1300
{
machine1-6
{ go to file, use buildrootmemdata variable and concatinate it with "boot -z -elf flash1.kernel: '[buildrootmemdata]' " to make one string that i can use later.

 
The switch command is Tcl's version of Case:
help said:
The switch command can match against variables and not just literals, as shown here (the result is 2):
set foo "abc"
switch abc a - b {expr {1}} $foo {expr {2}} default {expr {3}}

Using glob matching and the fall-through body is an alternative to writing regular expressions with alternations, as can be seen here (this returns 1):

switch -glob aaab {
a*b -
b {expr {1}}
a* {expr {2}}
default {expr {3}}
}

Whenever nothing matches, the default clause (which must be last) is taken. This example has a result of 3:

switch xyz {
a -
b {
# Correct Comment Placement
expr {1}
}
c {
expr {2}
}
default {
expr {3}
}
}

When matching against regular expressions, information about what exactly matched is easily obtained using the -matchvar option:

switch -regexp -matchvar foo -- $bar {
a(b*)c {
puts "Found [string length [lindex $foo 1]] 'b's"
}
d(e*)f(g*)h {
puts "Found [string length [lindex $foo 1]] 'e's and [string length [lindex $foo 2]] 'g's"
}
}

_________________
Bob Rashkin
 
i am not having success with a proof of concept case statement.
This simple program does not like the "switch" line
i tried:
switch -glob $name
switch -- $name
switch $name
switch name
among others.

if you see what is causing the hangup, pleas let me know.
david

#!/usr/bin/expect -f

#case statement syntax
set name "sam"

#send "The name is $name\n"
#expect "cfe> "

switch -glob $name
david{ send "Hello David, its $name, right?\n"
expect "cfe>" }
sam { send "Hello Sam, its $name, right?\n"
expect "cfe>" }
default { send "What is your name? $name, I did not say that\n"
expect "cfe>" }
 
i figured it out.
its \ after each case, except the default.
the syntax in a number of online guides and exploring expect book was wrong.


#!/usr/bin/expect -f

#case statement syntax
set name "david"

#send "The name is $name\n"
#expect "cfe> "

switch -glob $name david { send "Hello David, its $name, right?\n"
expect "cfe>" } sam { send "Hello Sam, its $name, right?\n"
expect "cfe>" } default { send "What is your name? $name, I did not say that\n"
expect "cfe>" }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top