2

So I am trying to have my script ask how many times it needs to do a command, take all the inputs at once, and then run it. So the task it's supposed to do is just take in a bunch of file paths.

    echo "How many SVN repositories do you need to convert and commit?"
    read repo_numb
    echo

    for ((i=1; i<=$repo_numb; i++));
    do
      echo "What is the full path to SVN repository $i?"
      read svn_repo_$i
      echo $svn_repo_$i
    done

But the echo only prints out the $i. How do I get it to create variables svn_repo1, svn_repo2, ... svn_repo$repo_number.

1
  • Note, when i is 5, your read really does set the value of svn_repo_5 (the argument undergoes parameter expansion before read is called). To access it, you would need name="$svn_repo_$i"; echo ${!name}. The array suggested by anubhava is the correct answer, though. Commented Nov 20, 2013 at 17:08

1 Answer 1

1

You can use BASH variable indirection but it will be even easier to use BASH array. Consider this script:

read -p "How many SVN repositories do you need to convert and commit?" repo_numb
echo

svn_repos=()
for ((i=1; i<=repo_numb; i++));
do
  read -p "What is the full path to SVN repository $i?" _tmp
  svn_repos+=( $_tmp )
done

echo "svn_repos=${svn_repos[@]}"
Sign up to request clarification or add additional context in comments.

Comments

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.