0

Problem

I have code that first iterates through a file with a folder path per line ensuring they are correct (which it does pass, and I also know these to be valid paths), and then tries to pass it to the program borg but errors.

The problem seems to be all down to how I'm creating or using folderList (which you can see as the first echo) but I'm not sure how to fix it.

Input

/media/sf_D_DRIVE/VirtualMachines Backups/
/media/sf_C_DRIVE/Websites/47/sln/site/App_Data/

Error Output

"/media/sf_D_DRIVE/VirtualMachines Backups/" "/media/sf_C_DRIVE/Websites/47/sln/site/App_Data/"

"/media/sf_D_DRIVE/VirtualMachines: [Errno 2] No such file or directory: '"/media/sf_D_DRIVE/VirtualMachines'
Backups/": [Errno 2] No such file or directory: 'Backups/"'
"/media/sf_C_DRIVE/Websites/47/sln/site/App_Data/": [Errno 2] No such file or directory: '"/media/sf_C_DRIVE/Websites/47/sln/site/App_Data/"'

Code

#this snippet reads in folder paths from a file
while read line
do
    exists=false
    if [ -f "$line" ]; then
        exists=true
    fi
    if [ -d "$line" ]; then
        exists=true
    fi
    if [ $exists = false ]; then
        exit 1
    fi
    folderList+=" $line"
done < "$2"

echo $folderList #gets past here successfully

borg create -s --progress $1::${dateString} $folderList
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Geesh_SO
  • 2,156
  • 5
  • 31
  • 58
  • Use an array to build multiple arguments with whitespaces: [bash - surround all array elements or arguments with quotes](https://stackoverflow.com/q/35868710/3776858) – Cyrus Aug 19 '19 at 16:23

1 Answers1

1

Something like that should work better by using an array to pass arguments and simplifying your condition testing:

while read -r line
do
    if [ -f "$line" ] || [ -d "$line" ]; then
      folderList+=( "$line" )
    else
      exit 1
    fi
done < "$2"

echo "${folderList[@]}" #gets past here successfully

borg create -s --progress "$1::${dateString}" "${folderList[@]}"
Léa Gris
  • 17,497
  • 4
  • 32
  • 41