bash - Adding a zero to single digit variable -
trying add 0 before varaible if it's less 10 , create said directory. can't seem 0 add correctly. keeps resulting in making 02.1.2011
, 02.2.2011
etc,etc.
i=0 in {01..31} if $i > 10 mkdir $path/02.0$i.2011 else mkdir $path/02.$i.2011 fi done
you can replace whole lot with:
for in 0{1..9} {10..31} ; mkdir $path/02.$i.2011 done
while still not having start external processes (other may in loop body).
that's not important here since mkdir
not 1 of things tend lot of in tight loop will important if write lot of quick , dirty code in bash
.
process creation expensive when you're doing hundreds of thousands of times of scripts have done :-)
example can see in action:
pax$ in 0{7..9} {10..12}; echo $i; done 07 08 09 10 11 12
and, if have recent-enough version of bash
, honor request leading digits:
a sequence expression takes form
{x..y[..incr]}
,x
,y
either integers or single characters, ,incr
, optional increment, integer. when integers supplied, expression expands each number betweenx
,y
, inclusive. supplied integers may prefixed0
force each term have same width. when eitherx
ory
begins zero, shell attempts force generated terms contain same number of digits, zero-padding necessary.
so, on debian 6 box, bash
version 4.1.5:
pax$ in {07..11} ; echo $i ; done 07 08 09 10 11
Comments
Post a Comment