1

I have a small script below; however, I don't quite understand the for loop and if statement. First, let's take a look at script:

listFieldNumbers ()
 { for ii in ${!field_number[@]}; 
do 
if [ $ii -lt $(( ${#field_number[*]}-1 )) ];
 then echo -n "$(( ${field_number[$ii]} + 1 )),"; 
else echo "$(( ${field_number[$ii]} + 1))"; 
fi; 
done; }

I don't understand what do following statements mean:

  1. field_number[@]
  2. {#field_number[*]}
2

field_number is an array
${!field_number[@]} gives you the list of indices of the array
${#field_number[*]} is the number of elements in the array.

I'd recommend you look for a bash tutorial about arrays. They are a bit tricky to work with.

1
  1. reference all the element in the field_number array
  2. it's ${#field_number[*]}, the number of field_number elements

Just read this tutorial: http://www.thegeekstuff.com/2010/06/bash-array-tutorial/

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.