gpt4 book ai didi

linux - bash while in while in while using variable_variable 操作

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:02:16 26 4
gpt4 key购买 nike

我正在尝试创建一个循环菜单脚本,当二级菜单变为 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 while1 exit 但 while0 也退出并且变量 selection0 不是 exit0。

最佳答案

尝试将您的变量放在本地上下文中,看看是否有帮助。 subcall 可能会修改它,并且在退出后不会将其恢复为原始值。

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
}

关于linux - bash while in while in while using variable_variable 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18793009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com