簡體   English   中英

Shell腳本在ksh中給出錯誤

[英]Shell script giving errors in ksh

我有一個腳本,該腳本在Bash shell(與Red Hat Linux)中執行時運行良好,但是,該腳本在使用ksh執行該腳本的Solaris 10(DB)服務器上失敗。 該腳本基本上從文件中逐行讀取並執行存儲的proc(在Oracle中)。 下面是我的腳本:

#/bin/sh

for i in $(cat subscriber.txt); do

        SUBSCRIBER_ID="'$i'"
        sqlplus -s myuser/myuser  <<EOF
        execute delete_learnings($SUBSCRIBER_ID);
        commit;
        EXIT    
EOF
done

我得到的錯誤是:

./removeLearnings.sh: syntax error at line 3: `$' unexpected

任何想法可能出了什么問題嗎? 我應該將腳本更改為具有ksh嗎? 由於這是客戶環境(我無權訪問),因此我無法在這台機器上進行調試。

問題是$(...)構造與POSIX兼容,但舊的Bourne shell不支持它, /bin/sh在Solaris 10和更早版本上。

您可以替換shebang來調用與Solaris POSIX兼容的shell:

#!/usr/xpg4/bin/sh

或使用以下舊式語法(不建議使用):

for i in `cat subscriber.txt`; do

您正在嘗試在ksh(Korn Shell)上執行sh(bourne shell)腳本。 嘗試將shebang(#!/ bin / bash)更改為(#!/ bin / ksh)

無論如何,使用for循環文本文件是一個壞主意。 請參閱http://mywiki.wooledge.org/BashFAQ/001-推薦的語法也更便於移植:

while read stuff; do
    : things with "$stuff"
done <subscriber.txt

通常,您將使用read -r但是我不知道Solaris上是否可用。

但是,shell循環通常是完全錯誤的方法。 單個SQL調用會更好,更強大:

( sed 's/.*/execute delete_learnings(&);/'
  printf "commit;\nEXIT\n" ) |
sqlplus -s myuser/myuser

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM