tags:

views:

63

answers:

5

I am trying to declare an array in bash, but when the code is run it says it cannot find the array. I have tried to write out the declaration of the array in several different ways, but it seems no matter how I try to declare it I cannot get it to work. I originally tried to declare it as such:

candidate[1]= 0
candidate[2]= 0
candidate[3]= 0

The error messages that are returned are:

votecalculation.sh: 13: candidate[1]=: not found
votecalculation.sh: 14: candidate[2]=: not found
votecalculation.sh: 15: candidate[3]=: not found

After this I tried another solution I found online:

ARRAY=( 'can1' 'can2' 'can3' )

When that is used it returns this error:

votecalculation.sh: 12: Syntax error: "(" unexpected

I am new to Bash and am getting really confused about arrays. Is there some specific way I need to declare an array or am I just going about it completely wrong?

A: 

Try removing the space:

candidate[1]=0
candidate[2]=0

and so on. I'm not an expert in this area myself but I think bash needs to recognize the whole assignment expression as one word, so you can't have spaces in it.

David Zaslavsky
votecalculation.sh: 13: candidate[1]=0: not found votecalculation.sh: 14: candidate[2]=0: not found votecalculation.sh: 15: candidate[3]=0: not foundI have tried it without spaces and above is the error I get when I try no spaces.
Waffle
A: 

It probably doesn't like the space after the equals sign.

Some other ideas:

  • Be sure that you're actually using bash to run your script, and not sh/dash.

  • You can explicitly declare a variable to be an array using declare -a varname

Chris AtLee
It says that declare is not found when I try to declare the array...it is just so odd that the rest of the program runs after pushing out the errors.
Waffle
Are you sure you're running bash?
Chris AtLee
At this point I am 100% sure it is declared as bash.
Waffle
A: 

In the first one there should be no spaces after the equal signs.

candidate[1]=0
candidate[2]=0
candidate[3]=0

The second one looks correct. Are you sure your shell is bash? Try adding a proper hash-bang line to the top of your script if you don't already have it:

#!/bin/bash
ARRAY=( 'can1' 'can2' 'can3' )
John Kugelman
I have that exact hash-bang and it still does not work. I have tried the code on my Mac as well as my Ubuntu machine.
Waffle
A: 
 #!/bin/bash

 myarray[0]=hello
 myarray[1]=world

 echo ${myarray[0]}
 echo ${myarray[1]}

save that to helloworld.bash

execute using . ./helloword.bash

Bryan
It did not find those either and for the echo it says it is a "bad substitution"
Waffle
do you have some strange implementation of bash?How are you calling the script?
Bryan
I don't think I do. I call the script from the terminal using "sh votecalculations.sh"
Waffle
try ./votecalculations.shor . ./votecalculations.sh
Bryan
but try that first with the code i pasted, in case you have other errors in your code
Bryan
If you're calling it explicitly with sh, that overrides whatever #! you have in the script itself.On Ubuntu, I believe sh is actually dash, not bash.What if you run via "bash votecalculations.sh"?
Chris AtLee
First one:sh: Can't open ./votecalculations.shSecond one just jumps to a new line and waits for another command.
Waffle
as i said, try my script first. yours might have other errors.
Bryan
It works when it is in the situation as you posted. It must be something else wrong with my code. Guess it is time to dig a little deeper. Thanks for all your help.
Waffle
A: 

If you have the correct shebang and you chmod +x scriptname, you don't need to start the script using bash scriptname - you can just use ./scriptname or if the directory it's in is in your PATH, then you can start it using simply scriptname.

If you have #!/bin/bash as your shebang and run sh scriptname then the shebang is overridden by the choice of shell on the command line.

There's no special meaning to having .sh or .bash at the end of the filename. It's just a matter of style or preference that some people like since it's intended to indicate the type of script (but only to the user - not to the system).

Dennis Williamson