1

我正在尝试创建一个循环菜单脚本,并且当二级菜单变为 false 时,我的 main-while 变为 false ...您认为我需要修复什么?

#!/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;

我的假设是当 selection1 是 exit1 而 1 退出但 while0 也退出并且变量 selection0 不是 exit0 时。

4

1 回答 1

1

尝试将变量放在本地上下文中,看看是否有帮助。子调用可能会对其进行修改,退出后不会将其恢复为原始值。

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
}
于 2013-09-13T20:49:32.640 回答