简体   繁体   English

使用源代码的bash脚本精心制作菜单

[英]Elaborating menu with bash scripts that use source

I got these two scripts, configScript.sh and genScript.sh , which works exactly as I want. 我得到了这两个脚本configScript.shgenScript.sh ,它们完全可以按照我的要求工作。 configScript.sh sets up the options to use the next time I run genScript.sh in the shell. configScript.sh设置了我genScript.sh在外壳中运行genScript.sh时使用的选项。

#!/bin/bash -x
#configScript.sh
func()
{
echo "
Choose
1 - Option 1
2 - Option 2
"
echo -n "   Enter selection: "
read select
case $select in
            1 ) 
            echo "  Option 1 chosen"
            . ./genScript.sh one
            cat << EOF >options.sh
OPTION=$OPTION
EOF
            ;;
            2 )
            echo "  Option 2 chosen"
            . ./genScript.sh two
            cat << EOF >options.sh
OPTION=$OPTION
EOF
            ;;

esac
}
func

#!/bin/bash -x
#genScript.sh
OPTION=""
. options.sh
[ "$1" ] && OPTION=$1
func2()
{
    if [ "$OPTION" == one ] ; then
        echo "Option one"
    elif [ "$OPTION" == two ] ; then
        echo "Option two"
    else
        echo "null"
    fi
}
func2   

My problem is that I want to change my menu in configScript.sh into something like this: 我的问题是我想将configScript.sh菜单configScript.sh为如下所示:


#!/bin/bash -x
#configScript.sh
func()
{
echo "
Choose
1 - foo
2 - bar 
"
echo -n "   Enter selection: "
read select
case $select in
            1 ) 
            echo "  foo chosen"
            foo
            ;;
            2 )
            echo "  bar chosen"
            bar
            ;;      
esac
}

foo()
{
    echo "
    Choose
    1 - foo1
    2 - foo2
    "
    echo -n "    Enter Selection: "
    read fooSelect
    case $fooSelect in
        1 )
        echo "foo1 chosen"
        ;;
        2 )
        echo "foo2 chosen"
        ;;
    esac
}

bar()
{
        echo "
    Choose
    1 - bar1
    2 - bar2
    "
    echo -n " Enter Selection: "
    read barSelect
    case $barSelect in
        1 )
        echo "bar1 chosen"
        ;;
        2 )
        echo "bar2 chosen"
        ;;
    esac
}
func

Where can I put in the segment to write the chosen option over to options.sh ? 我在哪里可以将所选的选项写到options.sh This part: 这部分:

. ./genScript.sh one
cat << EOF >options.sh
OPTION=$OPTION
EOF

If I keep them within the foo() and bar() it should mean that if I configure to echo foo1 and then to echo bar2 my genScript.sh should only output one of them. 如果将它们保留在foo()bar() ,则意味着如果我配置了回显foo1然后回bar2我的genScript.sh应该只输出其中之一。 How can I solve this? 我该如何解决?

Update* I'll try and make it a bit more clear. 更新*我将尝试使其更加清晰。 If I haven't run any scripts options.sh will be empty. 如果我还没有运行任何脚本,则options.sh将为空。 If I then run configScript.sh and choose the option that echoes foo2 and then the option that echoes bar1 and then close down 'configscript.sh . After that I run 如果然后运行configScript.sh并选择回显foo2的选项,然后选择bar1的选项,然后关闭'configscript.sh . After that I run . After that I run genScript.sh and I want it to echo foo2 and bar1`. . After that I run genScript.sh and I want it to echo foo2 and bar1`。

Your configscript: 您的配置脚本:

#!/bin/bash -x
#configScript.sh
func()
{
    . options.sh
    echo "
    Choose
    1 - foo
    2 - bar 
    3 - update config
    "
    echo -n "   Enter selection: "
    read select
    case $select in
        1 ) echo "  foo chosen"    ; foo ;;
        2 ) echo "  bar chosen"    ; bar ;;      
        3 ) echo "  update chosen" ; update ;; 
    esac
}

update()
{
cat <<EOF >options.sh
FOO=$FOO
BAR=$BAR 
EOF

. ./genScript.sh
}

foo()
{
    echo "
    Choose
    1 - foo1
    2 - foo2
    "
    echo -n "    Enter Selection: "
    read fooSelect
    case $fooSelect in
        1 ) echo "foo1 chosen" ; FOO="foo1" ;;
        2 ) echo "foo2 chosen" ; FOO="foo2" ;;
    esac
}

bar()
{
    echo "
    Choose
    1 - bar1
    2 - bar2
    "
    echo -n " Enter Selection: "
    read barSelect
    case $barSelect in
        1 ) echo "bar1 chosen" ; BAR="bar1" ;;
        2 ) echo "bar2 chosen" ; BAR="bar2" ;;
    esac
}
func

Your genScript.sh 您的genScript.sh

#!/bin/bash -x
#genScript.sh
FOO=""
BAR=""
. options.sh
[ "$1" ] && FOO=$1
[ "$2" ] && BAR=$2
func2()
{
    echo $FOO
    echo $BAR
}
func2   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM