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 Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl - Help with a Menu script required

Status
Not open for further replies.

chris01010

Programmer
Jan 29, 2004
25
GB
Hi,

I'm trying to wrtie a menu perl script which will allow users to connect to different servers from a management server without having to know the long server name.

I have written the below code, but when I execute it I manage to get through the first 2 menus but it doesn't exit once connected to the server (I thought the Last command would cause the Loop to exit).

Any ideas?



#!/usr/bin/perl

do

{

system('clear');
print "Please Select a Server";
print <<MENU_END;

[1].................BOB
[2].................BERT
[3].................HARRY
[4].................LEE
[5].................BILL
[6].................ANDY
[Q].................Quit
MENU_END

chomp($choice = <>);

RESPONSE1:
{
$choice = '1' && do {
system('clear');
print "Which Data Centre: ";
chomp($dc = <>);
`./goto BOB$dc`; sleep 2; last RESPONSE1; };
$choice = '2' && do {
system('clear');
print "Which Data Centre: ";
chomp($dc = <>);
`./goto BERT$dc`; sleep 2; last RESPONSE1; };
$choice = '3' && do {
system('clear');
print "Which Data Centre: ";
chomp($dc = <>);
`./goto HARRY$dc`; sleep 2; last RESPONSE1; };
$choice = '4' && do {
system('clear');
print "Which Data Centre: ";
chomp($dc = <>);
`./goto LEE$dc`; sleep 2; last RESPONSE1; };
$choice = '5' && do {
system('clear');
print "Which Data Centre: ";
chomp($dc = <>);
`./goto BILL$dc`; sleep 2; last RESPONSE1; };
$choice = '6' && do {
system('clear');
print "Which Data Centre: ";
chomp($dc = <>);
`./goto ANDY$dc`; sleep 2; last RESPONSE1; };

};
} while $choice ne 'Q';
 
[tt]$choice='1'[/tt] is likely not what you meant, could be
[tt]$choice==1[/tt] or
[tt]$choice eq'1'[/tt].
Also I don't understand why you print the menu items at every while loop and the labeled block does nothing.
I would write this more or less like this:
Code:
#!/usr/bin/perl
system('clear');
print "Please Select a Server";
print <<MENU_END;

    [1].................BOB
    [2].................BERT
    [3].................HARRY
    [4].................LEE
    [5].................BILL
    [6].................ANDY
    [Q].................Quit
MENU_END
my$choice;
while(!$choice=~/^q/i){
  $choice=<>;
  for($choice){
    /^1/&&do{
      system('clear');
      print "Which Data Centre: ";
      chomp($dc = <>);
      `./goto BOB$dc`;
      sleep 2;
      last;
    };
#  other options
  }
}


Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Thanks for the post prex1, although a straight copy and paste of your code didn't work. Your suggestion did allow me to question the point of the loop and cut the size of the script down.

It's probably just the way I worded it, but basically the reason why the script didn't exit was due to the back ticks. I should have used exec instead.

New Script:

#!/usr/bin/perl

system('clear');

print <<MENU_END;
[1].................BOB
[2].................BERT
[3].................HARRY
[4].................LEE
[5].................BILL
[6].................ANDY
[Q].................Quit
MENU_END

print "Please Select a Server: ";

chomp($choice = <>);

if ($choice !~ /^q/i) {

@servers = ("dummy","bob","bert","harry","lee","bill","andy");

system('clear');
print "Which Data Centre: ";
chomp($dc = <>);
exec ("./goto $servers[$choice]$dc");
}

exit;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top