LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-17-2012, 01:25 AM   #1
frambau
Member
 
Registered: Feb 2012
Posts: 32

Rep: Reputation: Disabled
Numeric Variable long in shell script


Hi to all,

I need to use a numeric incremental variable in a for stament in shell script.

now I'm using this,


declare -i MAX
MAX=1

for ....

MAX=MAX+1


My problem is with the size of the variable. when Max is upper than 33000 take again the value 1 and restart the assignement.

I need a couter until 1000000000 aprox.

Any solve????

THANKS
 
Old 02-17-2012, 02:04 AM   #2
crulat
Member
 
Registered: Sep 2011
Location: BeiJing China
Posts: 32

Rep: Reputation: Disabled
Code:
for counter in {1..1000000000}; do
    if (( ++i < 33000 )); then
        your command here
    else
            i=1
    fi    
done
 
Old 02-17-2012, 02:15 AM   #3
frambau
Member
 
Registered: Feb 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thanks Crulat

But this is not my problem.

Sorry for not having explained well.
I need is to use a variable that can take values ​​from 1 to 1000000000 one at a time, and print this variable
 
Old 02-17-2012, 02:42 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Well according to this snippet:
Quote:
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.
 
1 members found this post helpful.
Old 02-17-2012, 02:53 AM   #5
frambau
Member
 
Registered: Feb 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thanks Grail,

However, this number must be an incremental to insert into a database. How can I do?

If you remember, I was doing a program with awk to parse an xml.

Now these data csv I have to insert into the database, but one of the fields must be incremental.

I use a txt file, suppose that is incremental.txt, which contains the value 1.

I make a cat inside the shell, and then increase in the awk (+ + var) to resave the txt.

when exactly exceeds 44024, it becomes again 1.

What I can do?

thank you very much
 
Old 02-17-2012, 03:28 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Well although it takes time, the following returns for me:
Code:
awk 'BEGIN{for(i=1;i<=1000000000;i++)j++;print j}'
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.
 
Old 02-17-2012, 03:32 AM   #7
frambau
Member
 
Registered: Feb 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
ok thanks.
 
Old 02-17-2012, 03:35 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
Quote:
Originally Posted by crulat View Post
Code:
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?!
 
Old 02-17-2012, 03:41 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 332

Rep: Reputation: 118Reputation: 118
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
 
Old 02-17-2012, 07:06 AM   #10
crulat
Member
 
Registered: Sep 2011
Location: BeiJing China
Posts: 32

Rep: Reputation: Disabled
Try this, it works well, i have tested:
Code:
for((i=1; i<1000000000; ++i)); do 
     your_command_here
done
 
Old 02-17-2012, 10:22 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Last worked for me to, although I did manage to time it at over 107minutes
 
Old 02-17-2012, 10:29 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
Quote:
Originally Posted by frambau View Post
Thanks Grail,

However, this number must be an incremental to insert into a database. How can I do?

If you remember, I was doing a program with awk to parse an xml.

Now these data csv I have to insert into the database, but one of the fields must be incremental.

I use a txt file, suppose that is incremental.txt, which contains the value 1.

I make a cat inside the shell, and then increase in the awk (+ + var) to resave the txt.

when exactly exceeds 44024, it becomes again 1.
Can you post the awk code to illustrate what you mean?

AIUI you are saying that awk rolls over an incrementing integer on processing 44024+1. That sounds improbable.
 
Old 02-17-2012, 11:22 AM   #13
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,554
Blog Entries: 3

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
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.
 
Old 02-18-2012, 10:07 AM   #14
David the H.
Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 5,338

Rep: Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202
What's wrong with good old seq? According to the info page, it's good for up to at least 2^53 (gnu version).

To avoid system limitations on the generated string, use a while loop instead of a for loop, or xargs.

Code:
while read int; do
	echo "$int"
done < <( seq 1 1000000000 )
( The above uses bash's process substitution. )

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.
 
1 members found this post helpful.
Old 02-18-2012, 02:09 PM   #15
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 3,666

Rep: Reputation: 204Reputation: 204Reputation: 204
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to remove new line after numeric string grooper Linux - General 2 11-27-2011 11:30 AM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
Checking variable is numeric ijf99 Programming 2 03-28-2009 08:13 AM
shell script: redirect to variable va1damar Programming 1 02-05-2007 04:11 PM
expanding variable in shell script dipenchaudhary Programming 8 02-08-2006 05:05 PM


All times are GMT -5. The time now is 02:40 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration