1

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!

5

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

8

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}

  • +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
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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