1

I'm trying to create a loop menu script and my main-while turn to false when secondary menu turn to false... what do you think that I need fix?

#!/bin/bash

function f_menu_main () {
  echo "1) option1";
  echo "2) option2";
  echo "3) option3";
  echo "4) option4";
  echo "99) exit";
}

function f_case_main () {
  case $selection0 in
    "1" ) function1; selection0="1"; ;;
    "2" ) function2; selection0="2"; ;;
    "3" ) function3; selection0="3"; ;;
    "4" ) f_run_app packages 1; ;;
    "99" | "q" | "exit" | "quit") selection0="exitl0"; ;;
    *) f_menu_main; ;;
  esac
}

function f_case_packages () {
  case $selection1 in
    "1" ) function1; selection1="1"; ;;
    "2" ) function2; selection1="2"; ;;
    "99" | "q" | "exitl" | "quit") selection1="exitl1"; ;;
    *) f_menu_packages; ;;
  esac
}

function f_menu_packages () {
  echo "1)  options";
  echo "2) options";
  echo "99) exit";
}

function f_run_app () {
  selection="selection"$2;
  exitlv="exitl"$2;
  while [ "${!selection}" != "$exitlv" ]; do
    echo "";
    f_menu_$1;
    echo "Last selection: \""${!selection}" "$2"\".";
    echo -n "Select a item from menu: "; read "selection"$2;
    f_case_$1;
  done
}

f_run_app main 0;

My supposition is when selection1 is exit1 while1 exit but while0 exit too and the variable selection0 is not exit0.

3
  • 2
    what doesn't exit should exit otherwise make it exit by exiting the exit... Commented Sep 13, 2013 at 18:43
  • Sorry, but your code looks terrible, both in design and in style. Maybe this will help, though: use local variables: add local selection and local exitlv at the top of f_run_app. Commented Sep 13, 2013 at 19:42
  • 4
    Do not try to reinvent select. gnu.org/software/bash/manual/… Commented Sep 13, 2013 at 19:46

1 Answer 1

1

Try placing your variables in local context and see if that would help. The subcall probably modifies it and doesn't put it back to the original value after exiting.

function f_run_app () {
  local selection="selection"$2;
  local exitlv="exitl"$2;
  while [ "${!selection}" != "$exitlv" ]; do
    echo "";
    f_menu_$1;
    echo "Last selection: \""${!selection}" "$2"\".";
    echo -n "Select a item from menu: "; read "selection"$2;
    f_case_$1;
  done
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 thanks konsolebox, "local" context works i also see when it exit from while(level1) i need turn selection(1) value to "null" because it's saved. -----> "4" ) f_run_app packages 1; selection1=null; ;;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.