1

i'm trying to load some tables to mysql into a bash script so i have the followin code

DOMY="$MYSQL --user=xxxxx --password=xxxxx --database=$DBNAME"

for filename in $(cat $HPATH/toload.tables)
do

    $DOMY < $filename 2>/dev/null

    if [ $? -ne 0 ]
    then
        echo "#003|Error loading $filename"
        exit 1
    fi

done

if i see $? (echo $?) it give me 0 (zero) but the exit 1 is executed.

What i'm doin wrong?

2
  • 5
    Why not get rid of the error silencing and find out? Commented Jan 24, 2011 at 1:20
  • Please see BashFAQ/050 (putting commands in variables fails). Where are you putting echo $?? If another command, such as the if, is executed then $? holds the exit value of that command. Commented Jan 24, 2011 at 1:40

1 Answer 1

2

You can't see what's wrong because you have /dev/null-ed standard error.

Also, you are using an unnecessarily complex and somewhat error-prone code pattern where you examine $? later. It would be better to just write something like:

for i in "$@"; do
  if mysql -u root < $i; then
    echo ok # do "ok" processing here
  else
    echo not so ok # error path
  fi
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.