Bash脚本如何通过for循环中的预定义变量递增?

时间:2017-11-09 18:45:01

标签: bash shell scripting

大家好我试图要求复制的文件夹数量,将其存储到变量numFolders中然后通过for循环推送。我认为我需要一个numFolders变量的转义字符,但不确定。非常感谢!

#!/bin/bash

echo "This script will copy an entire drive to a dropbox folder with the same drive name"

read -p "Please enter drive name (case-sensitive):" driveName


mkdir ~/Dropbox/$driveName

# original above

# create all folders
# documents and settings
# rsync --progress --recursive --ignore-existing
echo "Now copying $driveName > Documents and Settings to Dropbox > $driveName"
rsync --progress --recursive --ignore-existing "/Volumes/$driveName/Documents and Settings" ~/Dropbox/$driveName

# Ask for how many folders
read -p "How many folders do you want?" numFolders

# Asks for name of folder and copies that until copied all the # of specified folders
for (i=1; i<= $numFolders; i+=1)
do
        echo $i
        read -p "Please enter name of folder you'd like to copy as well" folderName
        echo "Now copying $folderName to Dropbox $driveName"
        rsync --progress --recursive --ignore-existing "/Volumes/$driveName/$folderName" ~/Dropbox/$driveName
done

1 个答案:

答案 0 :(得分:1)

这是无效的语法:

for (i=1; i<= $numFolders; i+=1)

应该是:

for ((i=1; i<= $numFolders; i+=1))

可以进一步简化为:

for ((i = 1; i <= numFolders; i++))