我编写了一个shell脚本,它将与数据库连接并检索记录。但是当我对它表示赞赏时,它会给出错误:意外的关键字from
。谁能告诉我我在做什么错误?
我所做的代码如下:
#------------------------------------------------------------------------------------------------
# Define Script and Log Location
# ------------------------------------------------------------------------------------------------
SCRIPTHOME=/opt/psoft/scripts
SCRIPTINPUT=/opt/psoft/scripts/tac/input
SCRIPTLOG=/opt/psoft/scripts/tac/log
SCRIPTOUTPUT=/opt/psoft/scripts/tac/output
SCRIPTNOTPROCESSED=/opt/psoft/scripts/tac/notprocessed
# ------------------------------------------------------------------------------------------------
# Define Oracle Environment
# ------------------------------------------------------------------------------------------------
export ORACLE_HOME=/opt/oracle/product/9.2.0
export TNS_ADMIN=/var/opt/oracle/admin/network
export PATH=$PATH:$ORACLE_HOME/bin:$HOME/scripts:.
# ------------------------------------------------------------------------------------------------
# Main Program
# ------------------------------------------------------------------------------------------------
incno=$1;
if test ${#incno} -lt 6
then
echo "Please provide 6 digit incident no";
echo "TAC script has not been run";
exit;
fi;
cd ${SCRIPTINPUT}
if test -e *.csv
then
#cd ${SCRIPTINPUT}
for f in *.csv
do
dos2unix $f $f # To remove control M characters in the input file
echo " $f - Control M characters removed sucessfully " >> input.log
done
echo " Control M characters present in all files in input folder has been removed " >>input.log
cd ${SCRIPTINPUT}
for INP in *.csv
do
log_file="${SCRIPTLOG}/${INP}.log"
# To check if input file for Taccode or Not
cd ${SCRIPTINPUT}
echo "Taccode to be executed for the file $INP"
count=0;
while read line
do
pcode=`echo $line | cut -d "," -f "1"`
tcode=`echo $line | cut -d "," -f "2"`
cpcode=${#pcode}
ctcode=${#tcode}
#cpcode=`echo ${pcode} | grep -oE [[:digit:]] | wc -l`
#ctcode=`echo ${tcode} | grep -oE [[:digit:]] | wc -l`
if test $cpcode -eq 5
then
DBRESULT=`sqlplus sprint2/sh3rl0ck@SPRXP03
select * from mytable where productcode='10130' AND taccode='35710100';
quit;`
echo "Hello $count:$pcode:$tcode:$DBRESULT"
#here the database result should be checked for errors
if test $? -ne 0
then
echo "Query execution failed.Check ${log_file} file for errors!"
mv ${SCRIPTINPUT}/$INP ${SCRIPTNOTPROCESSED}
exit;
else
count=$(expr $count + 1)
echo "Record No:${count} ${pcode}:${tcode}" >>${log_file}
fi;
else
echo "Problem with product code or tac code. Check log file for errors"
echo "Record No:${count} ${pcode}:${tcode}:" >>${log_file}
mv ${SCRIPTINPUT}/$INP ${SCRIPTNOTPROCESSED}
exit;
fi;
done <${INP} #end file reading while loop
echo "Script excution succeeded" >>${log_file}
echo "${count} records inserted"
echo "Script excution succeeded";
done #end outer for loop
else
echo "No csv files found in input directory. -TAC script has not been run."
fi;
答案 0 :(得分:2)
我认为您需要将sqlplus命令全部放在一行
DBRESULT=`sqlplus sprint2/sh3rl0ck@SPRXP03 select * from mytable where productcode='10130' AND taccode='35710100'; quit;`
或包含显式换行符
DBRESULT=`sqlplus sprint2/sh3rl0ck@SPRXP03 \
select * from mytable where productcode='10130' AND taccode='35710100'; \
quit;`
答案 1 :(得分:0)
Oracle sqlplus
不支持内联sql语句。它唯一支持的命令行是使用@
运行sql脚本。解决问题的可行方法是使用heredoc。
您可以执行以下操作:
DBRESULT=$(sqlplus sprint2/sh3rl0ck@SPRXP03 <<-SQL
select * from mytable where productcode='10130' AND taccode='35710100';
exit;
SQL
)