1

I get the below error on running above command from shell script in CYGWIN.

Error:

sed: -e expression #1, char 1: unknown command: `,'

IF I ran command in cmd:

$ sed -n "8937,8946 p" "/cygdrive/c/TEMP/temp_oracle/alert_cert1_copy.log" | egrep -e "ORA-|Shutting down"

It works OK - Result:

ORA-1623 signalled during: ALTER DATABASE DROP LOGFILE GROUP 1...
Shutting down database

NOTE: 8937 & 8946 are the line numbers in text file - where it is needed to check the patterns. Searching must be between these lines.

IF I run the command in from shell script - the above error appears.


Shell Script:

    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"

With regards!

  • Copy paste the contents of your script and how are you running the script? – Inian Dec 28 '16 at 05:09
  • don't make us guess, show you actual script code that fails. I bet that you are using `sed -n "$startLineNo,$endLineNo p" file` or similar and that either the vars are misspelled, or for some other reason one or both don't have values. Do you know about shell debug mode, with `set -vx` (and `set +vx` to turn off). This will show you the line as executed **with** variables replaced with their values. Good luck. – shellter Dec 28 '16 at 05:17
  • Also, please read http://stackoverflow.com/help/how-to-ask , http://stackoverflow.com/help/dont-ask , http://stackoverflow.com/help/mcve and take the [tour](http://stackoverflow.com/tour) before posting more Qs here. Good luck. – shellter Dec 28 '16 at 05:20
  • Shelter, i have read a lot of questions not corresponding the policy of the site, so please. – bulat makhmutov Dec 28 '16 at 05:43
  • @bulatmakhmutov: Can you change this line `sed -n "8937,8946 p" $alert_file` to `sed -n "8937,8946 p" "$alert_file"` with proper double-quotes areound `$alert_file`? Can you do that around all the instances in the script and try? – Inian Dec 28 '16 at 05:55
  • Indian, it works. Thanks – bulat makhmutov Dec 28 '16 at 06:22
  • @bulatmakhmutov: Since you are relatively new here, read [What does it mean when an answer is accepted](http://stackoverflow.com/help/accepted-answer) – Inian Dec 28 '16 at 06:53

1 Answers1

1

Use proper double-quotes around variables in the script to avoid shell misinterpreting your forward-slashes(/), new-lines and word-splitting. All your variables used in your script are missing it.

The corrected version of your script should be something like

 #!/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 --
Inian
  • 80,270
  • 14
  • 142
  • 161