Ayuda con ciclos while
Publicado: 2017-04-20 10:50 @493
Estaba programando, practicando los ciclos while cuando se me ocurrió emular lo que sería una shell. Una buena manera de hacer un ciclo while que haga que el prompt vaya saliendo siempre, pero llegamos a un problema, y es que se me ocurrió poner unos comandos por defecto, y al introducirlos funcionan, pero al salir otra vez el prompt y meter otro comando sale exactamente lo mismo que el primero pese a ser diferentes:
El código es este:
He buscado información por Internet, en foros especializados y no he conseguido hallar la respuesta. He probado poniendo "next" en cada if y elsif pero solo consigo que la salida salga sin final:
Un saludo y ¡gracias por adelantado!
- C:\Users\TheSt\Desktop\proyecto firestorm>perl init.pl
- Bourne Shell version 1.0 started at 17:45:59 by
- [Bourne Shell]>> #DefCommands
- #First_time , #DefCommands , #Mkdir , #Preview , #Exit , #Shutdown , #Restart , #Documentation , #License , #About
- [Bourne Shell]>> #First_time
- #First_time , #DefCommands , #Mkdir , #Preview , #Exit , #Shutdown , #Restart , #Documentation , #License , #About
- [Bourne Shell]>>
El código es este:
Using perl Syntax Highlighting
- #!/usr/bin/perl
- use POSIX ;
- my $SH_VERS = "1.0" ;
- my $SH = "Bourne Shell" ;
- #my $USER = POSIX::cuserid();
- ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
- printf ("$SH version $SH_VERS started at %02d:%02d:%02d by $USER \n", $hour, $min, $sec);
- print "[$SH]>> " ;
- my $CMD = <STDIN> ;
- chomp $CMD ;
- while ($CMD ne "#Exit") {
- if ($CMD eq "#First_time") {
- print "Hi!, so you are new here!, well, this shell....\n" ;
- }
- elsif ($CMD eq "#DefCommands") {
- print "#First_time , #DefCommands , #Mkdir , #Preview , #Exit , #Shutdown , #Restart , #Documentation , #License , #About \n" ;
- }
- print "[$SH]>> " ;
- my $CMD = <STDIN> ;
- chomp $CMD ;
- }
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4
He buscado información por Internet, en foros especializados y no he conseguido hallar la respuesta. He probado poniendo "next" en cada if y elsif pero solo consigo que la salida salga sin final:
Using text Syntax Highlighting
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Terminating on signal SIGINT(2)
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Terminating on signal SIGINT(2)
Hi!, so you are new here!, well, this shell....
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4
Un saludo y ¡gracias por adelantado!