# Question about if structure and loops Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Question about if structure and loops without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am new in programing with bash script. Here is my problem: I am going to open a sort of data whose file name includes the date (format: file_yyyymmddhh.nc). There are some requirements:

• mm is from 01 to 12. This must be a two-digit integer.

• dd is from 01 to 28, 30, or 31, depending on the what month it is.

I tried to solve the problem with if structure and loops. I know that I could use something like this so that I can apply \${dd} to my filename.

if [\${mm} == 01] ; then
for ((i=1; i<=31; i=i+1))
do
\${dd}=i
done
fi

But I don’t know how to specify \${dd} to be a 2-digit integer especially when \${dd} <= 9. Is there any way to fix the code above?

You can use printf to format your numbers. Here the %02d denotes a two digit integer with leading zeros if appropriate.

dd=\$(printf "%02d" \$i)

You can extend this so that if \$y, \$m, \$d, and \$h contain your year, month, day, and hour numbers the construct could become this

file=\$(printf "file_%04d%02d%02d%02d.nc" \$y \$m \$d \$h)

While we’re here, your construct \${dd}=i is incorrect. The \$ symbol is prefixed in front of a variable name to get that variable’s value (in your case, i is the variable and \$i equates to its value). So in your case you would instead have written dd=\$i.

We are here to answer your question about Question about if structure and loops - If you find the proper solution, please don't forgot to share this with your team members.