ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Integer variables in older versions of Bash were signed long (32-bit) integers, in the range of -2147483648 to 2147483647. An operation that took a variable outside these limits gave an erroneous result.
this alone would seem to mean you should have no issue, but there is an addendum:
Quote:
As of version >= 2.05b, Bash supports 64-bit integers.
I believe a bash one will return as well just it may take longer. So I would guess that something else is actually stopping your code from progressing past the figure you have found.
for counter in {1..1000000000}; do
if (( ++i < 33000 )); then
your command here
else
i=1
fi
done
When tested as ...
Code:
for counter in {1..1000000000}; do
echo $counter
done
..., in both a terminal emulator (urxvt) and in a virtual terminal it crashed bash itself. The terminal emulator exited. The virtual terminal's bash session crashed and the login prompt re-appeared. A real bug?!
that's probably because bash expands braces into list of numbers in the range {1..bilion} which would be a very long string (my guess is about ~8--9 GB) and probably runs out of memory in the process
This is a bit off topic, but you can do arbitrarily large counters by using bc:
Code:
# Initialize to a really large, >128 bit value, just for illustration
counter=999999999999999999999999999999999999
# Increment counter by one.
counter=`echo "$counter + 1;" | bc`
# Show counter.
echo $counter
Since bc is a traditional utility also standardized in POSIX, it should be always available on all Unix/Linux/BSD systems, even the older ones.
No matter what technique you use, however, doing this through the shell is going to be slooooow. For faster results you really need to switch to a more efficient language like perl or C.
And, if you're using a data base, and using those numbers as a unique identifier, check your db documentation. Most db system include a provision for automatically generating unique identifiers. And, of course, if that number is intended for use as a record number, that would, for most db, be redundant, since record numbers are usually automatically generated.
Perhaps you could rephrase your question as a data base specific one.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.