Number series in for loop in specific format
Hi,
I have a requirement that i will pass a number as input to the script and i need output as below. Input: 4 Expected Output: WEEK04, WEEK05, WEEK06 For this i have the below script which giving the output without ZERO. Actual Output: WEEK4, WEEK5, WEEK6 Script used: ----------------------------------- i=$1 j=1 for j in {01..3..01} do echo WEEK$j=WEEK$i i=`expr $i + 1` done ----------------------------------- Also please consider the below scenario Input: 9 Expected Output: WEEK09, WEEK10, WEEK11 Can anyone please help me get desired output. |
Hi,
Bash strips the leading zero when you use normal outputting (print or echo), you need to use printf (formatted printing) to accomplish this. Based on your original post: Code:
#!/bin/bash EDIT: Here's a link to an online printf page, which explains it all: The printf command |
thanks a lot druuna...
it was very much helpful.... :) but i have to enhancemwnt in that. I am getting the values, which i need to capture in variables. Exmple: WEEK1=Week09 WEEK2=Week10 WEEK3=Week11 where WEEK1, WEEK2, WEEK3 are the variable names which will be static always and Week09, Week10, Week10 are the values in case we pass the value to the script as "9". |
You're welcome :)
BTW: Can you put up the [SOLVED] tag (first post -> Thread Tools). |
I would probably make your WEEK variable an array ... it would make the scripting trivial.
|
@druuna, can you please help me in gettin the output as described above in my previous post....
anyway posting it again.... --------------------------------------------- Exmple: WEEK1=Week09 WEEK2=Week10 WEEK3=Week11 --------------------------------------------- |
Hi,
Not sure if I completely understand, are you looking for something like this: Code:
#!/bin/bash Code:
Week01=Week09 Code:
printf "Week%d=" $j |
As suggested:
Code:
i=$1 |
I've tried a more straight forward approach. No loop.
Code:
#!/usr/bin/ksh |
Hey speedy ... please use [code][/code] tags when supplying code, for readability and preserve indentation.
|
Thanks Grail. This is my first post anywhere EVER.
|
No probs :) As you can see it is just a lot easier on the eyes.
|
thans druuna, grail and speedy 64...
i have my question solved... :) my requirement changed that i need 13 variables with each having 1 value in incremental fashion with the initial value being passed as a parameter. after seeing your posts and with little tweaking i was able to achieve what i wanted... please find teh code below: <code> i=$FSCL_WK_NBR for j in {1..13..1} do wk[$j]=$(printf "week%02d" $i) export Week[j]=${wk[j]} i=`expr $i + 1` #echo "Week$j=${wk[j]}" done </code> |
See post #10 for correct code tags.
Is there a purpose to exporting the variables? |
All times are GMT -5. The time now is 08:37 AM. |