Home > Enterprise >  Query returns 0, but my conditional statement isn't recognizing it as a 0
Query returns 0, but my conditional statement isn't recognizing it as a 0

Time:10-28

My Bash script uses SQLite to store data. I'm comparing id's (column jid) from table tester for which id's are in table test2:

sqlite3 jail.db "SELECT jid FROM tester" | while read jid; do
   if sqlite3 jail.db "select exists(select 1 from test2 where jid=$jid limit 1)"; then
   echo "i found it"
   else
   echo "i did not find it"
   fi
done

Going through id's query returns 0 or 1, but my Bash script executes the 1 part of If/then/else regardless:

0
i found it
0
i found it
0
i found it
0
i found it
1
i found it
1
i found it
1
i found it

CodePudding user response:

I guess you are looking for a left outer join, something like this

for r in $(sqlite3 jail.db 'select case when test2.jid != "" then 1 else 0 end from tester left outer join test2 on tester.jid = test2.jid;'); do
    if [[ "$r" == 1 ]]; then
        echo "i found it"
    else
        echo "i did not find it"
    fi
done

notice the use of $() to get the output of the command not its exit value.

  • Related