0

I'm on FreeBSD11. I have one or more zfs pool on my system. I want a script to check pool status and update the database, my code is:

pool=$(/sbin/zpool status | grep pool |awk '{print $2}')
for i in $pool
do

    status=$(/sbin/zpool status ${i} | egrep -i '(ONLINE|DEGRADED|FAULTED|OFFLINE|UNAVAIL|REMOVED|FAIL|DESTROYED|corrupt|cannot|unrecover)')

    sqlite3 <address>/my.db <<EOS
            update myTable set status = $status where name = ${i};

    EOS

    echo $status
   done 

This code has error, and doesn't update my database. Can you help me figure out the mistake?

codeforester
  • 39,467
  • 16
  • 112
  • 140
F.M
  • 463
  • 2
  • 7
  • 20

1 Answers1

1

I see two issues in your code:

  1. The heredoc marker "EOF" should not be indented
  2. You need single quotes in your SQL

Rewrite it as:

    sqlite3 <address>/my.db <<EOS
            update myTable set status = '$status' where name = '$i';
EOS

See also:

Community
  • 1
  • 1
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • thank you for your reply, actually I searched about sqlite in shell script and found that code in stackoverflow. Now I replce the code with your answer, it has error`Syntax error: end of file unexpected (expecting "done")`,. I placed `done` at the end of the code. – F.M Mar 14 '17 at 06:48
  • This is my correct code: `#!/bin/sh pool=$(/sbin/zpool status | grep pool |awk '{print $2}') for i in $pool do status=$(/sbin/zpool status ${i} |grep state|awk '{print $2}') echo 'update myTble set status = '\'''$status''\'';'|sqlite3
    mydb.db done`
    – F.M Mar 14 '17 at 08:12