原程式語法 如下:
sed_function=''
sed_function=${sed_function}"sed 's/Messages reached \([0-9][0-9][0-9]\) which/Messages reached xxx which/g'"
出現下列錯誤:
sed: -e expression #1, char 1: unknown command: `''
解決方法 修正如下: 將 " " 改為 ${ }
sed_function=''
sed_function=${sed_function}${ sed 's/Messages reached \([0-9][0-9][0-9]\) which/Messages reached xxx which/g' }
-------------------------------------------------------------------------------------------------------------------------
解決方法 來自下列文章
文章轉自 https://stackoverflow.com/questions/41355635/sed-e-expression-1-char-1-unknown-command
1個
這個問題已經在這裡有了答案:
- 何時將引號括在shell變量周圍? 5個答案
從CYGWIN中的shell腳本運行上述命令時,出現以下錯誤。
錯誤:
sed:-e表達式#1,字符1:未知命令:`,'
如果我在cmd中運行了命令:
$ sed -n "8937,8946 p" "/cygdrive/c/TEMP/temp_oracle/alert_cert1_copy.log" | egrep -e "ORA-|Shutting down"
它可以正常工作-結果:
ORA-1623 signalled during: ALTER DATABASE DROP LOGFILE GROUP 1...
Shutting down database
注意:
8937
&8946
是文本文件中的行號-需要在其中檢查模式。搜索必須在這些行之間。
如果我從shell腳本中運行命令-出現上述錯誤。
Shell腳本:
export alert_file="/cygdrive/c/TEMP/temp_oracle/alert_cert1_copy.log"
export alert_output="/cygdrive/c/cygwin64/home/alert_out.log"
function searchAlertByFilterLN() {
#err1=$(sed -n "${lastLineNum},${totLines} p" $alert_file | egrep -e "ORA-")
err1=$(sed -n "8937,8946 p" $alert_file | egrep -e "ORA-|Shutting down")
if [ -n "$err1" ]; then
echo -e "Errors found:" > $alert_output
echo ------------ >> $alert_output
sed -n "8937,8946 p" $alert_file | egrep -e "ORA-|Shutting down" >> $alert_output
echo "" >> $alert_output
echo "" >> $alert_output
echo -e "Check the details of the errors below. (Details means the surroundig lines of the error message only)" >> $alert_output
echo "-------------------------------------" >> $alert_output
sed -n "8937,8946 p" $alert_file | /usr/bin/egrep -A 5 -B 2 "ORA-|Shutting down" >> $alert_output
fi
}
searchAlertByFilterLN --
echo "function was executed"
帶著敬意!
1個
在腳本中的變量周圍使用正確的雙引號,以避免
shell
誤解您的正斜杠(/
),換行符和單詞拆分。您的腳本中使用的所有變量都丟失了它。
您的腳本的更正版本應類似於
#!/bin/bash
export alert_file="/cygdrive/c/TEMP/temp_oracle/alert_cert1_copy.log"
export alert_output="/cygdrive/c/cygwin64/home/alert_out.log"
function searchAlertByFilterLN() {
#err1=$(sed -n "${lastLineNum},${totLines} p" $alert_file | egrep -e "ORA-")
err1=$(sed -n "8937,8946 p" "$alert_file" | egrep -e "ORA-|Shutting down" )
if [ -n "$err1" ]; then
echo -e "Errors found:" > "$alert_output"
echo ------------ >> "$alert_output"
sed -n "8937,8946 p" "$alert_file" | egrep -e "ORA-|Shutting down" >> $alert_output
echo "" >> "$alert_output"
echo "" >> "$alert_output"
echo -e "Check the details of the errors below. (Details means the surroundig lines of the error message only)" >> "$alert_output"
echo "-------------------------------------" >> "$alert_output"
sed -n "8937,8946 p" "$alert_file" | /usr/bin/egrep -A 5 -B 2 "ORA-|Shutting down" >> "$alert_output"
fi
}
searchAlertByFilterLN --
sed -n "$startLineNo,$endLineNo p" file
或類似方法,或者var拼寫錯誤,或者由於其他原因一個或兩個都沒有值。您是否了解Shell調試模式,並帶有set -vx
(和set +vx
關閉)。作為執行這將顯示你的線與它們的值代替變量。祝好運。 – shellter 16年 12月28日在5:17sed -n "8937,8946 p" $alert_file
為sed -n "8937,8946 p" "$alert_file"
帶有適當的雙引號引起的聲音$alert_file
嗎?您可以圍繞腳本中的所有實例執行此操作,然後嘗試嗎? – 伊尼安 16年 12月28日5:55