Using Seq As A Variable With Padded Digits


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Seq As A Variable With Padded Digits
# 1  
Old 09-15-2009
Using Seq As A Variable With Padded Digits

Hi all. Im trying to use a sequence in a while loop like this below. I need it for navigating a year, month, day folder structure where a user can input the start date and have it go to the desired end date. The script will grab a certain file on each day then move onto the next. Ive got all that worked out using a static set of years in a for loop rather then a while loop with a seq. The problem is when I run into the month andn day folders as they are 01 02 03..10 11. Using the + 1 method it wont cd to the folders below 10 because the var is 1 2 3 instead of 01 02 03. Is there a way to make it add the zero to values below 10 but still keep count?



Code:
startdate=1
enddate=12
a=$startdate
if [ $a -lt 10 ]
   then a=0$a
   fi
 
while [ $a -lt $enddate ]
   DO OTHER THINGS HERE
   startdate=`expr $startdate + 1`
   a=$startdate
if [ $a -lt 10 ]
   then a=0$a
   fi
   done

# 2  
Old 09-15-2009
Seems like your code is working fine, albeit with some slight modifications. Try to incorporate the changes and let me know the result.

Your code, modified slightly:
Code:
 
$ cat file2
startdate=1
enddate=12
a=$startdate
if [[ $a -lt 10 ]]
   then a=0$a
   fi
while [ $a -le $enddate ] ## Changed -lt to le
do
   ##DO OTHER THINGS HERE
   printf "\na=${a}\n\n"
   startdate=`expr $startdate + 1`
   a=$startdate
   if [ $a -lt 10 ]
     then a=0$a
   fi
done
$

OUTPUT:

Code:
 
$ ./file2
a=01

a=02

a=03

a=04

a=05

a=06

a=07

a=08

a=09

a=10

a=11

a=12
 
$

HTH, Smilie


Regards,

praveen
# 3  
Old 09-15-2009
Thanks that works for me!
# 4  
Old 09-15-2009
Hi, maybe You should consider the seq command?

Code:
for x in $(seq -w 12); do
...
done

/Lakris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

How to define two digits variable in shell script?

Dear Shell script Experts, I am working on shell script which is defined here, qsub_seq.csh . The purpose of this script is to read few input files (with defined starting index and last index) and make processing faster over server. For some task, I had 1064 of input files, so I wrote another... (8 Replies)
Discussion started by: emily
8 Replies

3. Shell Programming and Scripting

Printf padded string

Is possible to print padded string in printf? Example echo 1 | awk '{printf("%03d\n", $1)}' 001I want S1 S11 S2 S21to be padded as: S01 S11 S02 S21Thanks! (26 Replies)
Discussion started by: yifangt
26 Replies

4. Shell Programming and Scripting

Comma padded.. Output

Hello, here is the outout of the command below.. Can someone please tell me how to get the output as below output needed: 18914,30716,17051,4139,14155... ( no comma for the last value) ps -e -o pcpu,pid,user,tty,args | sort -n -k 1 -r | head | awk '{print $2}' 18914 30716 17051 4139... (10 Replies)
Discussion started by: kamathg
10 Replies

5. Shell Programming and Scripting

extracting Number variable and the following digits.

HI all, I have output of something like this: crab: ExitCodes Summary >>>>>>>>> 12 Jobs with Wrapper Exit Code : 50117 List of jobs: 1-12 See https:///twiki/something/ for Exit Code meaning crab: ExitCodes Summary >>>>>>>>> 5 Jobs with Wrapper Exit Code : 8001 List of... (20 Replies)
Discussion started by: emily
20 Replies

6. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

7. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies

8. Shell Programming and Scripting

declaring variable with for $(seq)

Hi guys. i have the following script: 1 #!/bin/bash 2 linkcount=$(grep "/portal" tickets | wc -l) 3 grep "/portal" tickets > links 4 for i in $(seq 1 $linkcount); do 5 echo "BLYAT" 6 let link$i=$(sed -n "$i"p links) 7 echo $ 8 done the problem is, that "let" can`t... (1 Reply)
Discussion started by: neverhood
1 Replies

9. UNIX for Dummies Questions & Answers

ksh Checking if variable has 5 digits

How could I check if a numeric variable has 5 digits in KSH...I have a zipcode variable that I know will always be 5 digits, and I want to print out an error if it is less or more than 5 digits the problem is that I have it as: if ] but this won't work because the statement doesn't see 0001 as... (3 Replies)
Discussion started by: developncode
3 Replies

10. Shell Programming and Scripting

Move and rename files in seq. with padded digits

Greetings, I am new to scripting, but find if I can see the code working for a given problem, then I can eventually figure it out. (9 Replies)
Discussion started by: rocinante
9 Replies
Login or Register to Ask a Question