views:

542

answers:

2

I have a SQlite database gets populated when ever someone adds a filename and dir to it, then I want the script to read the newest 3 entries (done with the "LIMIT 3") then I want this script to take those 3 entries and start the "script1.sh" for each of them, then once the script1 has finished one of the 3, I want it to look back into the SQlite database and check if there are any new entires and repeat. (so kinda like a queue) NOTE, at the end of script1.sh there is a command that will delete it's entry from the SQlite DB.

So basically I want the script to check the SQlite DB each time one script1.sh script finishes.

So far I have:

#!/bin/bash

sqlite3 /database.db  "SELECT * FROM main ORDER BY mKey ASC LIMIT 3" | while read file
do
fileName=`echo "$file" | awk '{split($0,a,"|"); print a[2]}'`
echo "$fileName"

    #Run script
    ./script1.sh "$fileName" "$file"


done
+1  A: 

You don't need to use echo and awk, Bash can do that for you:

saveIFS=$IFS
IFS='|'
fileName=(${file})        # make an array
fileName=${fileName[2]}   # get an element
IFS=$saveIFS              # put it back like you found it

You are missing a "done", by the way.

Dennis Williamson
+1  A: 
#!/bin/bash

key=0
while :; do
  sqlite3 -column ./test.db \
   "SELECT * FROM main WHERE mKey > $key ORDER BY mKey ASC LIMIT 3" > tmpFile
  lastKey=$key
  while read k f; do
      key=$k
      echo start script1 on $f here current key is $k
  done < tmpFile
  [ $key -eq $lastKey ] && sleep 1
done

I wasn't sure you really wanted the newest 3, since you also said you wanted a queue, so I got the next 3 each time, starting with the first.

DigitalRoss
Ok, thanks. This is half of what I need, I also needed it to run the 3 concurrently, not one after another.Will see what I can make out of this.
Mint
BTW, my prototype script was tested on an sqlite3 database with the following schema: `CREATE TABLE main (mKey INTEGER PRIMARY KEY AUTOINCREMENT, pathName);`
DigitalRoss
Cheers.Ended up not using this method, seemed way to complicated.Just using your infinite while loop, and then using sqlite3 to do the rest.
Mint