我有两个脚本,configScript.sh
和genScript.sh
,它们完全按照我的意愿运行。 configScript.sh
设置了下次在shell中运行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
我的问题是,我想将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
我可以在哪里放入段以将所选选项写入options.sh
?这部分:
. ./genScript.sh one
cat << EOF >options.sh
OPTION=$OPTION
EOF
如果我将它们保留在foo()
和bar()
之内,则应该意味着如果我配置为回显foo1
然后回显bar2
我的genScript.sh
应该只输出其中一个。我该如何解决这个问题?
更新*
我会试着让它更清楚一些。如果我没有运行任何脚本,options.sh
将为空。如果我然后运行configScript.sh
并选择回显foo2
的选项,然后选择回显bar1
的选项,然后关闭'configscript.sh . After that I run
genScript.sh {{1 }} {foo2的{1}} bar1`。
答案 0 :(得分:1)
你的配置:
#!/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
您的genScript.sh
#!/bin/bash -x
#genScript.sh
FOO=""
BAR=""
. options.sh
[ "$1" ] && FOO=$1
[ "$2" ] && BAR=$2
func2()
{
echo $FOO
echo $BAR
}
func2