crankshaft
crankshaft

Reputation: 2677

Capture Error and output to log in Bash script

I have a script which is checking a database for errors based on a text file of record ids.

When an error occurs such as the connection is lost,it writes the offending id ($line) to a log file as well as the error message ($OP).

Problem is the error message is always null although the error is displayed on the console, only the id is written to the log file.

How can I capture the error and also output that to the log file ??

while read line
do
    OP=$(mysql $DB -u root --password=$PW -e "select * from properties where hierarchyid = $line")
    printf "\r$line"
    #if [ $? -gt 0 ]; then
    if [[ ! $OP =~ "hierarchyid" ]]; then
        echo
        echo $line $OP >> errors.log
        sleep 10    
        echo
    fi
done < $IDS

Upvotes: 0

Views: 1729

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

Redirect stderr to stdout.

OP=$(mysql ... 2>&1)

Upvotes: 3

Related Questions