【发布时间】:2017-04-24 19:31:43
【问题描述】:
我有一个 bash 脚本
foo.sh:
#!/bin/bash
echo "start"
. /path/to/bar/bar.ksh
echo "after bar"
getoravariables $1
bar.ksh(目的只是设置oracle变量):
#!/bin/ksh
echo "b4 getora"
getoravariables () {
echo "1"
if [ $# -ne 1 ]
then
echo "2"
echo "Usage: getoravariables sid"
exit 1
fi
grep -w ${1} ${ORATAB_LOC} | grep -v "#" | sed "s/:/ /g" | read SID ORAHOME ASK
if [ $? -ne 0 ]
then
print "Error: Please enter a vaild SID and check the ${orafile} file for correct input"
return 1
fi
ps -ef|grep pmon|grep ${SID} >> /dev/null
if [ $? -ne 0 ]
then
print "Error: SID ${SID} does not seem to be started. "
return 2
fi
export ORACLE_SID=${SID}
export ORACLE_HOME="${ORAHOME}"
export ORAENV_ASK=NO
. $ORACLE_HOME/bin/oraenv > /dev/null
}
echo "after getora"
执行 foo.sh 后我得到:
>./foo.sh validsid
start
b4 getora
after getora
所以我可以说,简单地调用 ksh 脚本没有问题,因为“b4 getora”和“getora 之后”的回显命令有效。但是执行该函数存在一些问题,因为我没有得到预期的 echo "1" 或 echo "2"。
此外,如果我将 foo.sh 作为 ksh 脚本运行,一切正常。
因此,我可以假设两种语言之间的语法存在某种我没有理解的差异。有人可以帮忙吗?
更新:
我在完整的 ksh 脚本顶部添加了set -x(我在问题中包含的只是一个子集)。我发现脚本在我发布的函数的稍后位置挂起:
++ echo
++ grep -v '#'
++ read VAR VALUE
(hanging here)
这部分脚本的代码是:
echo $GLOBPARFIL
grep -v "#" ${GLOBPARFIL}|while read VAR VALUE
do
export ${VAR}=${VALUE}
done
所以$GLOBPARFIL 没有正确设置。它也恰好是一个从getoravariables () 拉入和设置的变量。调试 tat 的输出证明了这一点:
++ read VAR VALUE
++ export GLOBPARFIL=/home/local/par/global.par
++ GLOBPARFIL=/home/local/par/global.par
这与下面的@jlliagre 答案一致,声明变量未正确设置。但是,我尝试了解决方法,但结果相同。
更新 2:
根据所提供的信息和逻辑,我能够找到真正的来源并创建解决方法:
问题:
grep -v "#" ${file_location}| tr "^" " " | while read VAR VALUE
解决方案:
while read VAR VALUE
do
export ${VAR}=${VALUE}
done <<%
$(grep -v "#" ${file_location}| tr "^" " ")
%
我将标记为完成。但是,如果我能收到更多关于为什么这个解决方法能解决问题的更多信息,我将不胜感激。
【问题讨论】: