elmonitoboy
elmonitoboy

Reputation: 25

Creating variable based off counter in for loop using bash

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.

Upvotes: 2

Views: 190

Answers (1)

anubhava
anubhava

Reputation: 785108

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[@]}"

Upvotes: 1

Related Questions