0

I need to reference the output of this mysql code

mysql -h xx.xx.x.xx -u xxxxxx -pxxxxxx xxxx_xxxx < cmd.sql

that does a count(*) to check if that db table has been filled or not. Trying to grab the return for an if statement later in the script.

I've tried both

count=$(mysql -h xx.xx.x.xx -u xxxxxx -pxxxxxx xxxx_xxxx < cmd.sql)
if [ $count > 0 ]; then
    echo "record found\n"
fi

as well as

count=$(mysql -h xx.xx.x.xx -u xxxxxx -pxxxxxx xxxx_xxxx "select count(*) from xxxx_x")
if [ $count > 0 ]; then
    echo "record found\n"
fi

but they both got a "[: count(*): unary operator expected" error.

I even tried to "select count(specific_column) from xxx_xx" with the same error.

N Schleder
  • 51
  • 8

1 Answers1

1

You want

if [ $count -gt 0 ]; then

The > is the output redirection operator (analogous to <, which you're using above)

To capture the last line, just pipe your results through tail -1 (see here)

Community
  • 1
  • 1
Brian Agnew
  • 261,477
  • 36
  • 323
  • 432
  • Thanks. I knew that was a problem but since the error wasn't on that line I didn't figure that it would be the issue. It fixed that error but now I get a "too many arguments" error. I suspect because the mysql command outputs 2 lines "count(*) | 0" How would I edit my code to only read the second line? – N Schleder Jul 19 '13 at 15:52