Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Can anyone tell me why this bash script works if I cut and paste it to the terminal but throws "server_prep.sh: 7: Syntax error: "(" unexpected" when launched using $ sudo sh server_prep.sh ?

#!/bin/sh

#Packages
apt-get -y install ssh libsqlite3-dev ruby-full mercurial

#Gems
required_gems = ( rake rails sqlite3-ruby )

#Set up directories
[ ! -d /var/www ] && mkdir /var/www
[ ! -d /var/www/apps ] && mkdir /var/www/apps

#install gems manually
if ! which gem >/dev/null; then
	wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
	tar xvfz rubygems-1.3.5.tgz
	ruby rubygems-1.3.5/setup.rb
	ln -s /usr/bin/gem1.8 /usr/bin/gem
	gem update --system

	#Tidy Up
	rm -rf rubygems-1.3.5.tgz rubygems-1.3.5
fi

#Install required gems
for required_gem in "${required_gems[@]}"
do
	if ! gem list | grep $required_gem >/dev/null; then
		gem install $required_gems
	fi
done

Thanks in advance!

share|improve this question
add comment

3 Answers 3

up vote 5 down vote accepted

Your script is using sh, not bash, when it's executed. There could be a minor syntax difference between the two.

Try changing !#/bin/sh to !#/bin/bash at the top of your script

share|improve this answer
add comment

You can't have spaces when assigning variables in sh/bash. It has to be:

required_gems=( rake rails sqlite3-ruby )

In any case, it doesn't have to be an array, you could just do:

required_gems="fake rails sqlite3-ruby"

and then

for required_gem in ${required_gems}; do
  blah blah
done

Notice there are no "" around ${required_gems}

share|improve this answer
    
+1 You can usually use string lists instead of arrays. I don't think I've ever used an array in bash in the 10 years I've been programming in it. –  David Pashley Sep 13 '09 at 20:10
    
Indeed. The only time I use arrays in bash, is when I have several vars describing the same object. For example, when I have a service name/executable/description/sampling interval. –  katriel Sep 14 '09 at 22:05
add comment

The third to last line, should $required_gems read $required_gem?

share|improve this answer
    
doubleplus good. –  David Rickman Sep 14 '09 at 6:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.