1

I'm trying to insert stuff in db whith a while loop, but after 2 inserts my loop break ... here the code:

x=0
while [[ "$x" < 10 ]];do
    ((x++))
    sqlite my_db.db "insert into (col) values ('$x...')
done

So I tried to do that but unsuccess too.

y=0
x=0
while [[ "$y" < 5 ]];do
    while [[ "$x" < 10 ]];do
        ((x++))
        sqlite my_db.db "insert into (col) values ('$x...')"
    done
    ((y++))
done

But once again its only insert 2 times ... So why did i have this error and how to fixe that ? thx.

1
  • Tip: ShellCheck detects common problems, including this one. Commented Jul 7, 2017 at 20:16

2 Answers 2

1

< and > are string comparison operators.
For integers you must use -lt (less than) and -gt (greater than) respectively.
Also close the query string.

x=0
while [[ "$x" -lt 10 ]];do
    ((x++))
    sqlite my_db.db "insert into (col) values ('$x...')"
done
Sign up to request clarification or add additional context in comments.

1 Comment

Thx you ... shame on me for don't trying this ..And the unclosed query is a typo (i'm on mobile).
1

Because your code never resets x to 0 (or anything less than 10). Additionally, you're using the wrong operator (which is why the first snippet isn't working).

x=0
y=0
while [[ $y -lt 5 ]];do
   x=0
   while [[ $x -lt 10 ]];do
      ((x++))
      sqlite my_db.db "insert into (col) values ('$x...')
   done
   ((y++))
done

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.