scripting - Differentiate between 2 and 02 in bash script -


i have bash script takes date, month , year separate arguments. using that, url constructed uses wget fetch content , store in html file (say t.html). now, user may enter 2 digit month/date (as in 02 instead of 2 , vice-versa). how distinguish between above 2 formats , correct within script?

the url works follows:
date: 2 digit input needed. 7 must supplied 07 url constructed properly. here looking check append 0 date in case less 10 , not have 0 in front. so, 7 should become 07 date field before url constructed.
month: 2 digit input needed, here url automatically appends 0 in case month < 10. so, if user enters 2, url forms 02, if user enters 02, url forms 002. here, 0 may need appended or removed.

p.s: know method followed url s*c&s, need work it.

thanks,
sriram

it's little unclear you're asking for. if you're looking strip 1 leading zero, do:

month=${month#0} 

this turn 02 2, , 12 remains 12.

if need remove more 1 0 (above 002 turn 02), you'll need different, using regular expressions

while [ -z "${month##0*}" ]; month=${month#0}; done 

or use regular expressions, sed

month=$(sed -e 's/^0*//'<<<"$month") 

hth

edit
per edit; has been suggested, use printf

month=$(printf %02d "$month") 

this turn 2 02, 12 remains is, 123. if want force 2 digit number or don't have printf (which shell built-in in bash, , available otherwise too, chances pretty low), sed can again

month=$(sed -e 's/.*\(..\)$/\1/'<<<"00$month") 

which prepend 2 zeros (002) , keep last 2 characters (02), turning empty string 00 well. turn a 0a though. come think of it, don't need sed this

month="00$month" month="${month:0-2}" 

(the 0- required disambiguate default value expansion)


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -