3

How do I iterate for nth file in a for loop in unix? below some code I have tried but not succeeded

#!/bin/bash
#
n=2
array=( "CTL_MLPOSDTLP1_1.ctl" "CTL_MLPOSDTLP1_2.ctl" "CTL_MLPOSDTLP1_3.ctl" )
for x in "${array[@]}"
for ((x=${array[@]}; x<=n; x++));
do
echo "array[x]"
done
  • What do you mean by "some file nth position"?   What is the expected/desired output? – G-Man Jan 4 at 3:13
  • @roaima: I respect John1024, and I believe that he has written many answers much better than this one.   It is an adequate answer to the questions “How do I iterate through the elements of an array?” and “How do I access elements of an array by their index numbers?” — questions that have probably been asked at least a handful of times, and have probably received answers that are clearer and more coherent, elsewhere.   … (Cont’d) – G-Man Jan 4 at 23:21
  • (Cont’d) …  I’m concerned that (1) anybody who wants to know how to iterate through the elements of an array won’t find this question, or won’t recognize it as being equivalent to their question, because of how poorly this one is worded, and (2) anybody who wants to know what this question appears to be asking (how do I look at every other (i.e., every 2nd) element of an array?) won’t be satisfied by John’s answer. – G-Man Jan 4 at 23:21
  • @roaima: But my point is that it’s a duplicate answer.  If the question is incomprehensible, how are we to choose one that means the same thing? – G-Man Jan 4 at 23:33
  • @G-Man I've no strong feelings either way. I'll pull out. – roaima Jan 5 at 9:30
2

Here are two ways to loop over an array:

#!/bin/bash
array=( "CTL_MLPOSDTLP1_1.ctl" "CTL_MLPOSDTLP1_2.ctl" "CTL_MLPOSDTLP1_3.ctl" )

echo Loop 1
for x in "${array[@]}"
do
    echo "$x"
done

echo Loop 2
for ((x=0; x<${#array[@]}; x++));
do
    echo "${array[x]}"
done

Looping over selected items

This scripts allows you to specify on the command line which elements from the array ate to be processed:

#!/bin/bash
array=( "CTL_MLPOSDTLP1_1.ctl" "CTL_MLPOSDTLP1_2.ctl" "CTL_MLPOSDTLP1_3.ctl" )

for x in "$@"
do
    echo "${array[x]}"
done

Suppose we want to loop over the first two and skip the third. (Since bash uses zero-based indexing, the first two are number 0 and number 1.) Use:

$ bash script.sh 0 1
CTL_MLPOSDTLP1_1.ctl
CTL_MLPOSDTLP1_2.ctl

To run just the third:

$ bash s.sh 2
CTL_MLPOSDTLP1_3.ctl

To iterate from 0 to n-1

for ((x=0; x<n; x++));
do
    echo "${array[x]}"
done
  • but how abt if i want to iterate only 2 time ? means only for 2 files from the array i want to do some operation and skip the 3rd file as i dont want it for this time .. may next time i want to iterate all files in the array list? – sabyasachi kar Nov 11 '14 at 6:37
  • @sabyasachikar OK. I added a script which lets you select from the command line which array elements to process. See updated answer. – John1024 Nov 11 '14 at 6:49
  • Thanks @John, nice information on zero based indexing :) ... but i want it inside the shell,not from the command line as i need to take the nth value from a config file. Regards sabya – sabyasachi kar Nov 11 '14 at 6:54
  • What is your config file format? Is there any other information that I need that you haven't told me yet? – John1024 Nov 11 '14 at 6:57
  • 1
    +1. Good answers in this time window do not get enough love – iruvar Nov 11 '14 at 7:34
0
#!bin/bash
n=2
array=( "CTL_MLPOSDTLP1_1.ctl" "CTL_MLPOSDTLP1_2.ctl" "CTL_MLPOSDTLP1_3.ctl" )
for ((x=0; x<=n-1; x++));
do
    echo "${array[x]}"
done

Thanks @john for the help as I edited and got the correct output; it's working now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.