LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-11-2014, 07:48 PM   #1
razorcbl
LQ Newbie
 
Registered: Mar 2014
Posts: 1

Rep: Reputation: Disabled
Lightbulb append integers until total is x integers


Working on a basic bash and i would like to have a check (if statement?) to make sure this variable is always 9 integers long..and if it is not then append 0 until it is.

sometimes this variable is: 123456789 (do nothing)
but if its: 123456 append '0' until its 123456000

I have searched online and this forum but cant seem to find the correct way to do this (looked at "if" statements").


Thanks in advance
 
Old 03-11-2014, 08:03 PM   #2
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 308

Rep: Reputation: 121Reputation: 121
Take a look at this:
http://tldp.org/LDP/abs/html/comparison-ops.html
and remember that every 9 digit number is -ge 100000000 and -lt 1000000000
 
Old 03-11-2014, 08:23 PM   #3
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 308

Rep: Reputation: 121Reputation: 121
To append zeroes, multiply by 10. Take a look at this:
http://www.tldp.org/LDP/abs/html/ops.html
There are various bash methods that work (see Example 8-2). Here are two examples:
Code:
$ a=5
$ ((a=10*a))
$ echo $a
50
$ let "a=a*10"
$ echo $a
500
$
 
Old 03-11-2014, 11:24 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,357

Rep: Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799Reputation: 2799
Whilst the forum and google have let you down, the general rule is you at least show your attempt and then we can assist.
 
Old 03-12-2014, 12:12 PM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,640

Rep: Reputation: 1252Reputation: 1252Reputation: 1252Reputation: 1252Reputation: 1252Reputation: 1252Reputation: 1252Reputation: 1252Reputation: 1252
Look like it would be simpler to just append a string of 9 0's ("000000000") then take the substring of the first 9 characters....

Code:
$value = "${value}000000000"
$value = ${value:0:9}
 
1 members found this post helpful.
Old 03-12-2014, 12:22 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Another one:
Code:
$ var=123456
$ printf "$var%0*d\n" $((9-${#var})) 0
123456000
 
Old 03-12-2014, 12:35 PM   #7
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,410
Blog Entries: 1

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
If you know that the integers will never be > 9 chars. Append 0's and chop the first 9 off.

$ echo $VAR"000000000" | head -c 9

Plus the previous bash way mentioned. Each with it's quirks. I've seen version issues where -c # for head / tail can be +/- 1 depending on distro and version. And the bash substring doesn't work on other shells like debians default shell dash.
 
Old 03-12-2014, 03:29 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,640

Rep: Reputation: 1252Reputation: 1252Reputation: 1252Reputation: 1252Reputation: 1252Reputation: 1252Reputation: 1252Reputation: 1252Reputation: 1252
Quote:
Originally Posted by Shadow_7 View Post
If you know that the integers will never be > 9 chars. Append 0's and chop the first 9 off.

$ echo $VAR"000000000" | head -c 9

Plus the previous bash way mentioned. Each with it's quirks. I've seen version issues where -c # for head / tail can be +/- 1 depending on distro and version. And the bash substring doesn't work on other shells like debians default shell dash.
True - but you should still be able to use
Code:
res = `expr substr "${var}00000000" 0 9`
for the same result.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting integers from a string teresevo1 Programming 3 11-08-2010 09:24 PM
unsigned integers in C CoderMan Programming 5 03-24-2009 08:50 PM
64-bit integers in C nodger Programming 1 03-31-2005 09:06 AM
converting integers to strings 1337 Twinkie Programming 12 08-06-2004 05:57 PM
Bash, non-integers and arithmetic causticmtl Programming 5 07-16-2003 10:15 AM


All times are GMT -5. The time now is 10:47 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration