Linux - NewbieThis 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
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Please put your scripts/code inside [code][/code] tags, as that greatly improves readability by preserving whitespace and indentation.
Here's some information that you may find useful:
To prevent numbers with leading zeros from being interpreted as octal by bash, prefix them with 10#, which means "the following number is in base 10". For instance, echo $((010 + 1)) will produce 9 as octal "10" is 8 in decimal, while echo $((10#010 + 1)) returns 11.
To print integers prefixed with a specific number of zeros, use printf (for instance, printf "%04d%s" $a will add up to three zeros, ensuring that $a is at least 4 digits long)
The seq command might be an alternative. It doesn't interpret leading zeroes as octal. Unfortunately it doesn't print them, so you have to add the leading zeroes back in.
Last edited by berndbausch; 07-10-2015 at 01:41 AM.
Here is how you can loop using the arguments passed to your script. Note that you can NOT use $1 and $2 directly, this:
Code:
for i in {$1..$2}
...will NOT work.
But this works:
Code:
#!/bin/bash
BEGIN=$1
END=$2
for ((i=$BEGIN; i<=$END; i++)); do
printf "%03d%s\n" $i
done
Here's the output:
Code:
./for_loop.sh 6 12
006
007
008
009
010
011
012
Also, this:
Quote:
Originally Posted by Ser Olmy
To prevent numbers with leading zeros from being interpreted as octal by bash, prefix them with 10#, which means "the following number is in base 10". For instance, echo $((010 + 1)) will produce 9 as octal "10" is 8 in decimal, while echo $((10#010 + 1)) returns 11.
The seq command might be an alternative. It doesn't interpret leading zeroes as octal. Unfortunately it doesn't print them, so you have to add the leading zeroes back in.
That depends...
Code:
for num in $(seq -w 01 10); do echo $num; done
01
02
03
04
05
06
07
08
09
10
printf is probably better, but you can treat the number (without leading zeroes) as a string. And concatenate it with a bunch of leading zeroes, and tail the length of string you wish to use.
Code:
for COUNT in 1 2 3 4 5 6 7
do
STRING="0000000"$COUNT
USEFUL=$(cat $STRING | tail -c 4)
echo $USEFUL
done
But that's quirky as various versions of tail might be -c 4 or -c 3 for a 3 digit output. There's also length syntax and substring syntaxes built into bash that can do much of the same stuff in less intuitive ways. Which will fail if said script is not run under bash as not all distros default to bash and if you run the script with "sh script.sh" instead of "./script.sh" it will use that other default shell. Which might be dash for distros like debian which lacks those length and substring extras (or uses a different syntax).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.