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.

2
  • Run your script through SpellCheck. And personally, I think this syntax would be simpler if you want to use indexes. Commented Jul 14, 2014 at 0: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. Commented Jul 14, 2014 at 2:02

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

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

1 Comment

You were both right, but I had tried what you suggested before, but the underlying problem was pdw's answer. Thanks for your help.

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.