使用bash在for循环中创建基于计数器的变量

时间:2013-11-20 16:44:43

标签: bash variables for-loop counter

所以我试图让我的脚本询问执行命令需要多少次,立即获取所有输入,然后运行它。所以它应该做的任务就是接受一堆文件路径。

    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

但回声只打印出$ i。如何创建变量svn_repo1,svn_repo2,... svn_repo $ repo_number。

1 个答案:

答案 0 :(得分:1)

您可以使用BASH变量间接,但使用BASH数组会更容易。考虑一下这个脚本:

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