KSEltar
KSEltar

Reputation: 11

bash while in while using variable_variable operation

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.

Upvotes: 1

Views: 107

Answers (1)

konsolebox
konsolebox

Reputation: 75488

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
}

Upvotes: 1

Related Questions