break: cannot break

hi guys

I am working on a menu for linux... some basic stuff.

but I have an issue. I got 1 server where something is working and the same thing does not work in the same way in another linux box

Basically I am simulating a command line where user insert some commands and to end and go back to the previous menu that person has to press Control + C and Enter Key

all my server are centos 5.0

I am adding my menu here

basically you execute ./menu

After that go to option

  1. Opciones
  2. Ejecutar comando
    and execute a command like pwd
    after that
    Press Control + C and Enter Key

but I got error ./menu: break: cannot break
but in other servers pressing Control + C and Enter Key goes back to previous menu

any idea how to fix that?
or another way to exit from that simulated command line and go back to previous menu where I selected 7. Ejecutar comando

Basically this is the part that has issues

##Shell simulada para ejecutar commandos 
ejecutacmd ()
{
header
echo -e "${bold}Press Control+C and Enter Key to go back to previous menu${offbold}"
echo -e " "
while true ; do
     read whichcmd?"Enter Command: "
     if [ $(sudo -l | grep -c "$whichcmd") -gt 0 ] ; then
          sudo $whichcmd
     else
          $whichcmd
     fi
done
}

menu (code) is attached so you can see all options
thanks a lot

Update
Uploaded as menu.txt just remove .txt and test it

Zip files can potentially include viruses, etc... Further, they complicate the whole process of assisting in solving the problems.
If your example is too large, simply cut/paste a few records/rows of data into your post (making sure to enclose it inside codetags).

I will add it as txt

---------- Post updated at 07:26 PM ---------- Previous update was at 02:03 PM ----------

well guys I ended doing this

I don't know if it's the best way but that's the only thing that came to my mind

ejecutacmd ()
{
header
echo -e "Digite ${bold}end${offbold} para finalizar"
echo -e " "
while [ "$whichcmd" != end ]
do
     read whichcmd?"Enter Command: "
     $whichcmd
done
whichcmd=0

I would just prompt the user to press enter when done. The read will set whichcmd to an empty string, and you can test that to break the loop.

read whichcmd
[ "$whichcmd" ] || break

Regards,
Alister

not possible since like I said it is a basic command line so user can execute

pwd cd ifconfig ......

so he needs to press enter every time he runs a command

Yes. I understand that. But if the user presses enter without typing a command, that can be use to signal the break in the while loop.

now I get it but I need it in a loop... :smiley:

Obviously. One can't use "break" if not within a loop. Those two lines were intended for you to use within a while loop whose condition is always true.