LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 08-20-2007, 10:07 AM   #1
RobertoBech
Member
 
Registered: Dec 2003
Location: Rio de Janeiro
Distribution: Debian Squeeze
Posts: 54

Rep: Reputation: 15
Incrementing 001 value in shell script


Hi everybody. I'm a beginner in shell-scripting, and I'm stuck at a certain point...

I know how to increment by one the value of a variable. here's an example:

count=1
((count++))
echo $count
2

That's ok. But suppose I have a variable like this:

count=001
((count++))
echo $count
2

That's not what I want at all... I want $count to be 002, not 2. How do I solve this? Thank you all.
 
Old 08-20-2007, 10:24 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
That's *presentation*:
Code:
case ${#count} in 1) count="00${count}";; 2) count="0${count}";; *) ;; esac; echo "$count"
 
Old 08-20-2007, 10:33 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
or, simpler:
Code:
printf '%0.3d\n' $count
and if you want it to have more digits, just increase the '3' in the above command.
 
Old 08-20-2007, 10:41 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
fundamentally, the numbers 002 and 2 are the same. Why 002 gets stored differently I don't know. (Maybe because numbers are stored as strings...)

for example:
Code:
sh-3.1$ a=002
sh-3.1$ echo $a
002
sh-3.1$ b=3
sh-3.1$ echo $b
3
sh-3.1$ echo $((a+b))
5
 
Old 08-20-2007, 11:56 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
or, another way:
Code:
> count=1001
> ((count++))
> echo ${count:1}
002
 
Old 08-20-2007, 03:55 PM   #6
RobertoBech
Member
 
Registered: Dec 2003
Location: Rio de Janeiro
Distribution: Debian Squeeze
Posts: 54

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
or, another way:
Code:
> count=1001
> ((count++))
> echo ${count:1}
002
Thank you all for your suggestions. I've found this one to be the most interesting. Thanks Colucix.
 
Old 08-20-2007, 07:38 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Pixellany, the 'values' are stored as assigned, but as soon as you do a numeric operation, they get treated as nums (cast to int), which as you know, ignores leading zeroes.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
A client has "NUM 1 FDX 001-001 08:02a" displaying on her screen Goslowsky Linux - Software 3 03-07-2006 04:43 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration