For Loop queries

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For Loop queries
# 1  
Old 01-22-2018
For Loop queries

I have Existing FOR loop in script like below. But the zip should happen for only those years in another file generated dynamically.

Existing FOR LOOP -
for i in XYZ_*ABC${YEAR_2014}.csv; do
    printf "%s\n" "$i"
    done | zip -@  >> $XYZ_2014.log 2>&1 || ERROR_COUNT=$((ERROR_COUNT + 1)) 
for i in XYZ_*ABC${YEAR_2015}.csv; do
    printf "%s\n" "$i"
    done | zip -@  >> $XYZ_2015.log 2>&1 || ERROR_COUNT=$((ERROR_COUNT + 1)) 
for i in XYZ_*ABC${YEAR_2016}.csv; do
    printf "%s\n" "$i"
    done | zip -@  >> $XYZ_2016.log 2>&1 || ERROR_COUNT=$((ERROR_COUNT + 1))

I have file which has the years for which zip should happen.


Updated version should look like below
for i in XYZ_*ABC${YEAR_2014}.csv; do
    printf "%s\n" "$i"
    done | zip -@  >> $XYZ_2014.log 2>&1 || ERROR_COUNT=$((ERROR_COUNT + 1)) 
for i in XYZ_*ABC${YEAR_2015}.csv; do
    printf "%s\n" "$i"
    done | zip -@  >> $XYZ_2015.log 2>&1 || ERROR_COUNT=$((ERROR_COUNT + 1))

It would be helpful if you can let me know how to incorporate the dynamic year change in the FOR loop.


Last edited by Scott; 01-22-2018 at 04:48 PM.. Reason: Please use code tags
# 2  
Old 01-22-2018
Have you considered reading "dynamic_years.txt" first, in a while-loop, and applying that to the inner for-loop?

while read YEAR; do
  for i in XYZ_*ABC$YEAR.csv; do
    printf "%s\n" "$i"
  done | zip -@ XYZ_$  >> XYZ_$YEAR.log 2>&1 || ERROR_COUNT=$((ERROR_COUNT + 1))
done < dynamic_years.txt

This User Gave Thanks to Scott For This Post:
# 3  
Old 01-23-2018
Is that for loop really necessary? Wouldn't
ls -1 XYZ_*ABC$YEAR.csv

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX Desktop Questions & Answers

/etc/motd - queries

If I have /etc/motd, he is file or directory? I saw that some call them folders and files others... Which option is better? I knew that being a director, but many told me not. Thank you! (1 Reply)
Discussion started by: mescu
1 Replies

2. IP Networking

RDNS Queries

Hey everyone, I have a question, I've been playing around with tcpdump, and noticed my machine making numerous rdns look ups. They are displayed like: > 9950+ PTR? (44) My question is, if dns works based on numerical... (0 Replies)
Discussion started by: Lost in Cyberia
0 Replies

3. UNIX for Advanced & Expert Users

How many DNS queries

Is there any way to see how many queries come into our external DNS server? In looking at DNS providers, most of them base pricing on number of queries per month so I just wanted to see if you had any idea/way of gathering that data? A rough ballpark figure would even work. Our DNS server is... (1 Reply)
Discussion started by: raggmopp
1 Replies

4. Programming

Combine 3 queries

can these 3 be combined into 1 query? createtablea1as selecta.tps_Res_nb, b.tkt_prod_cd, b.tkt_prod_typ_nm, b.prod_intrnl_ds, b.tkt_prod_typ_nm AS TKT_ENTL_NM, casewhen b.tkt_prod_nm isnotnullthen b.tkt_prod_nm when b.tkt_prod_nm isnulland b.prod_intrnl_ds isnotnullthen... (1 Reply)
Discussion started by: dwr80
1 Replies

5. Shell Programming and Scripting

Few queries regarding awk...

One of the command output is as below. -rw-r--r--+ 1 root root 75G Nov 21 16:43 /var/ovs/mount/86BXXX/running_pool/Machine1/System-sda.img -rw-r--r--+ 1 root root 75G Nov 21 16:36 /var/ovs/mount/86BXXX/running_pool/Machine2/System.img -rw-r--r--+ 1 root root 150G Sep 23 19:13... (2 Replies)
Discussion started by: pinga123
2 Replies

6. Homework & Coursework Questions


Any help on like where to get started on this? I'm just confused. 1. The problem statement, all variables and given/known data: Enter text here.Queries to satisfy these two report requests (use your CCI database): Retrieve all rows of active inventory where current on hands is less than... (0 Replies)
Discussion started by: lakers34kb
0 Replies

7. Shell Programming and Scripting

my queries

hi guys Well, i need to have a report generation script or any script which will show me all the content/information of a file when i run that script. Please help me on this isssue at the earliest.As i am little bit aware of scripting.Thanks in advance! regards ash (4 Replies)
Discussion started by: whizkidash
4 Replies

8. UNIX for Advanced & Expert Users

Two small queries

Query 1 : How to check if a directory already exists? If doesn't exist then create a new one. Query 2 : I want to put following text using a single echo statement into a log file and also want to retain the formatting of the text. How it can be... (3 Replies)
Discussion started by: skyineyes
3 Replies

9. UNIX for Advanced & Expert Users

Some queries...

Guys need some advice on how to check some of the questions below? i'm running on an open VMS platform... which i am an idiot to... appreciate if anyone can give some hints or source on how to check on.. a script that is running on cron job... but doesn't run as the login user name.. 1. why... (6 Replies)
Discussion started by: 12yearold
6 Replies
Login or Register to Ask a Question

Featured Tech Videos