[英]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
和genScript.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.