jremio
January 9, 2012, 9:08am
1
Hi there,
I need to loop some values,
for i in $(seq $first $last)
do
does something here
done
for $first and $last, i need it to be of fixed length 5. so if the input is 1, i need to add zeros in front such that it becomes 00001. It loops till 99999 for example, but the length has to be 5.
eg 00002, 00042, 00212, 012312 and so forth.
any idea on how i can do that? thanks!
zaxxon
January 9, 2012, 9:14am
2
$> read first
432
$>printf "%05d\n" $first
00432
for i in {00001..99999}
do
does something here
done
or
for i in $(seq -w ${first} ${last})
do
does something here
done
drl
January 9, 2012, 10:09am
4
Hi.
Similarly to c:
#!/usr/bin/env bash
# @(#) s1 Demonstrate c-like for-loop in bash.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C
pe
first=17
last=21
for (( i=first ; i<=last ; i++ ))
do
printf "%05i\n" $i
done
exit 0
producing:
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0.8 (lenny)
GNU bash 3.2.39
00017
00018
00019
00020
00021
See man bash for details ... cheers, drl
Another alternative,
for i in `seq -f%05g $first $last`