我已经编写了一个shell脚本,该脚本将连接数据库并检索记录。但当我对它进行优化时,它会给我一个错误:Unexpected关键字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;
我认为您需要将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;`
Oracle sqlplus
不支持内联sql语句。它唯一的命令行支持是使用@
运行sql脚本。解决您问题的可行方法是使用heredoc。
你可以做一些类似的事情:
DBRESULT=$(sqlplus sprint2/sh3rl0ck@SPRXP03 <<-SQL
select * from mytable where productcode='10130' AND taccode='35710100';
exit;
SQL
)