0

Hi guys I'm trying to ask for the number of folders to copy, store that into a variable numFolders and then push that through a for loop. I think that I need an escape character for the numFolders variable, but not sure. Thanks so much!

#!/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
  • Quote your parameter expansions! By definition, based on your comment to the answer, you don't know what folder and file names to expect; you are going to need to write your scripts very carefully. Commented Nov 9, 2017 at 19:28

1 Answer 1

1

This is invalid syntax:

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

It should be:

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

Which can be further simplified to:

for ((i = 1; i <= numFolders; i++))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this fixes my code exactly!! Simply swap out the lines and it works like a charm. I'm recovering old hard drives for people and learning to create scripts that will universally let me extract and sort data into easily organized folders for the client to sift through. This is just the beginning.

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.