2015-06-25 21 views
0

我寫了一個shell腳本,它將連接數據庫並檢索記錄。但是,當我超出它,它給了我錯誤:意外的關鍵字from。任何人都可以請建議我我在做什麼錯誤? 我做的代碼下面給出:shell腳本中的意外錯誤:「來自`的意外關鍵字`」。找不到錯誤

#------------------------------------------------------------------------------------------------ 
# 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/[email protected] 

        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; 
+1

粘貼你的腳本http://shellcheck.net/會很有幫助表明了錯誤的根源,但實際的錯誤信息不完全坦誠。 – tripleee

+0

我編輯了似乎是明顯的複製/粘貼錯誤。請檢查編輯以確保我沒有刪除任何重要內容。 – tripleee

+0

我希望http://shellcheck.net/也會警告明確檢查'$?',或者在同一個目錄下執行'cd'超過次數。但這些主要是文體問題。 – tripleee

回答

2

我認爲你需要把你的sqlplus命令所有在一行

DBRESULT=`sqlplus sprint2/[email protected] select * from mytable where productcode='10130' AND taccode='35710100'; quit;` 

或包括顯式換行符

DBRESULT=`sqlplus sprint2/[email protected] \ 
    select * from mytable where productcode='10130' AND taccode='35710100'; \ 
    quit;` 
+0

此外,您需要用反斜槓或(更好地)引用SQL來正確地引用'*'。另外,還有一個很好的理由,'$(command)'強烈建議'''命令\''。 – tripleee

0

Oracle sqlplus不支持內聯sql語句。它唯一的命令行支持是使用@運行一個sql腳本。解決您的問題的可行方案是使用heredoc。

你可以這樣做:

DBRESULT=$(sqlplus sprint2/[email protected] <<-SQL 
    select * from mytable where productcode='10130' AND taccode='35710100'; 
    exit; 
SQL 
) 
+0

不錯的建議。代碼正在工作。您能否告訴我是否有任何方法可以建立持久性數據庫連接。觸發多個查詢,然後關閉連接? – user286009

+0

有一種方法使用2個管道。它比你期望的簡單和容易。 – alvits