1
do
{
    print"CHOOSE ANY OF THE FOLLOWING OPTIONS:\n";
    print"==========================================\n";
    print"1-LOGIN & LOGOUT\n";
    print"2-MAKE CALL\n";
    print"3-EXIT\n";
    print"==========================================\n";
    print("\nENTER YOUR OPTION: ");
    $option=<>;
    if($option==1)
    {
        print("IN THE LOGIN & LOGOUT SCENARIO\n");
        &Login_logout();
    }
    elsif($option==2)
    {
        print("IN THE MAKE CALL SCENARIO\n");
    }
    elsif($option==3)
    {
        print("\nEXITING...\n");
        exit(0);
    }
    else
    {
        print"\nINSERT A VALID OPTION...!!!\n";
    }
}while(1);

Here the subroutine Login_logout() calls a SIPp instance(command line instance). After the successful completion of the command line instance the the scalar $option takes some garbage value and hits the else condition and prints the line "INSERT A VALID OPTION...!!!". This process continues infinetly until force closing the Konsole.

Can anybody tell me where I am wrong in the script.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Shantanu
  • 27
  • 6
  • $option takes some garbage value? What garbage value? – zpmorgan Feb 10 '12 at 05:38
  • Don't know what it takes, I tried to print it out but nothing is displayed. May be when command line exits it returns some value and the $option takes it as $ARG[0] value. It may I m not sure. – Shantanu Feb 10 '12 at 06:06
  • I tried your code (removed the call to `Login_logout()`) and it works fine for me. What does `Login_logout()` do? Can you update your post to include its source? – Jim Garrison Feb 10 '12 at 06:43
  • Login_logout actually calls a SIPp instance. The instance is "./sipp -sf Reg_UAS.xml -i my_host_ip -p 5060".For this you have to install SIPp in your machine. – Shantanu Feb 10 '12 at 09:08

3 Answers3

2

Remember that <> takes a line rather then a string, so removing return (CR/LF, etc.) is needed.

...
$option=<>;
chomp $option; ## chomp removes the tailing return
if($option eq '1')
...
ZhangChn
  • 3,154
  • 21
  • 41
  • While `chomp ($option = <>)` is usually a good idea, it isn't strictly necessary in this case. – flesk Feb 10 '12 at 09:19
1

I think the problem that your external program call modify/redirects the STDIN, that way it is reads some garbage.

Set autoflush:

$|=1;

If you do not need the stdin/stderr on your external call, close explicitly it like this or redirects it to a file:

`sip.sh >&- 2>&- <&-`

or close just the stdin

`sih.sh <&-`

If I am correct this trick is works under recent ksh and bash only. At least under ksh :-)

regards,

user1126070
  • 5,059
  • 1
  • 16
  • 15
-1

do { }while(1); this is nothing but infinite loop only, no condition check so it will loop infinitely, more over try using $option=<STDIN>;

run
  • 1,170
  • 7
  • 14
  • But still it should wait for the user input in the statement $option=<> then go for the if-else statement. And also I had tried the $option= statement instead of simple $option=<>. But didn't worked out. Loop is running infinitely without waiting for user input. – Shantanu Feb 10 '12 at 05:44
  • try to put your code under `if($option =~ m/^\d+$/)` by which you accept only integers..i.e `$option=;if($option =~ m/^\d+$/) {if($option==1) {print("IN THE LOGIN & LOGOUT SCENARIO\n");&Login_logout();}.....` – run Feb 10 '12 at 06:46
  • There is a condition check, if option==3 its makes an exit – user1126070 Feb 10 '12 at 10:33