1

I have the following code:

PROJECT_TYPES="iPad iPhone"
ANT_TARGET_NAMES="ipadf ipaf"

INDEX=0

for PROJECT_TYPE in $PROJECT_TYPES; do

echo "${PROJECT_TYPE} => ${ANT_TARGET_NAMES[$INDEX]}"

let "INDEX++"
done

This displays the following lines:

iPad => ipadf ipaf
iPhone =>

How can I change the code so it displays:

iPad => ipadf
iPhone =>  ipaf

???

Thanks in advance

Mike

1
  • 1
    foo="bar baz" creates a string with the contents bar baz. In certain contexts, strings can be split into sequences because of the way they're interpolated (whitespace splitting), but they are not lists. Commented Sep 7, 2011 at 16:28

2 Answers 2

2

The correct way to do this is:

INDEX=0
PROJECT_TYPES=(iPad iPhone)
ANT_TARGET_NAMES=(ipadf ipaf)

for PROJECT_TYPE in ${PROJECT_TYPES[*]} 
do 
echo "${PROJECT_TYPE} => ${ANT_TARGET_NAMES[$INDEX]}"  
let "INDEX++" 
done
Sign up to request clarification or add additional context in comments.

Comments

1

bash 4 has associative arrays so you could write:

declare -A targets=([iPad]=ipadf [iPhone]=ipaf)
for project_type in "${!targets[@]}"; do
  printf "%s => %s\n" "$project_type" "${targets[$project_type]}"
done

Otherwise, declare two arrays as in ennuikiller's answer, but I would iterate over the indices directly

projects=(iPad iPhone)
targets=(ipadf ipaf)
for (( i=0; i < ${#projects[@]}; i++ )); do
  printf "%s => %s\n" "${projects[$i]}" "${targets[$i]}"
done

1 Comment

Thanks, but I have bash 3 here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.