Benji
Benji

Reputation: 635

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.sh sets up the options to use the next time I run genScript.sh in the shell.

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


#!/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? 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. 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. 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 rungenScript.shand I want it to echofoo2andbar1`.

Upvotes: 0

Views: 132

Answers (1)

thom
thom

Reputation: 2332

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

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

Upvotes: 1

Related Questions