Jon49
Jon49

Reputation: 4606

Bash Loop 2 Arrays

I'm trying to create a loop for a couple of arrays but I get this error:

./bash.sh: 3: ./bash.sh: source[0]=/media/jon/my\ directory/: not found

This is what my code looks like:

sourceFiles[1]=/media/jon/ACER/Documents\ and\ Settings/Laura/Documents/Shared
destinationFiles[1]=/media/jon/My\ Book/Shared

for index in ${!sourceFiles[@]}
do
  sudo rsync -a --delete ${sourceFiles[$index]} ${destinationFiles[$index]}
done

I'm some what green to bash files and this is terribly frustrating that doing a simple loop is so difficult.

Update

I needed a #!/bin/bash at the top per the correct answer.

Upvotes: 1

Views: 123

Answers (2)

morgano
morgano

Reputation: 17422

Try enclosing in double quote your variables in your sudo line:

sudo rsync -a --delete "${sourceFiles[$index]}" "${destinationFiles[$index]}"

Upvotes: 2

pdw
pdw

Reputation: 8866

Your code looks ok. I think you're not using bash though ("not found" is not a bash error message). Are you perhaps using /bin/sh? On many systems that's a minimal POSIX shell, not bash.

A POSIX shell would not recognize sourceFiles[1]=... as an assignment and would consequently run it as a command. Hence the "not found" error.

Upvotes: 2

Related Questions