1

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.

Jon49
  • 4,444
  • 4
  • 36
  • 73
  • Run your script through [SpellCheck](http://www.shellcheck.net/). And personally, I think [this syntax](http://stackoverflow.com/a/49114/3076724) would be simpler if you want to use indexes. – Reinstate Monica Please Jul 14 '14 at 00:36
  • @BroSlow Thanks, I'll change it over, it would even be nicer if I could just use a `map2` syntax! Or even just `range(0..4).map`. – Jon49 Jul 14 '14 at 02:02

2 Answers2

2

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.

pdw
  • 8,359
  • 2
  • 29
  • 41
2

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

sudo rsync -a --delete "${sourceFiles[$index]}" "${destinationFiles[$index]}"
morgano
  • 17,210
  • 10
  • 45
  • 56
  • You were both right, but I had tried what you suggested before, but the underlying problem was pdw's answer. Thanks for your help. – Jon49 Jul 14 '14 at 02:00