`sed:-e表达式#1,char 1:未知命令:`,'`

时间:2016-12-28 05:03:50

标签: linux bash shell sed cygwin

我在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 个答案:

答案 0 :(得分: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 --