Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error near unexpected token "(" when declaring arrays in bash

I have to do several maven tests on some EARs I have.

Instead of doing them manually, I want to write a shell script which automates the process.

This is what I have:

#!/bin/bash
projects = ("MAIN_EAR", "EJB_EAR", "SIT_EAR", "ENC_EAR", "ENVIRONMENT_EAR", "PRESS_EAR")

myenvs  = ("dev", "cart")

for prj in "${projects[@]}"
do
    :
    for myenv in "${myenvs[@]}"
    do
       :
       mvn –am –pl "../$prj" clean package –Denvironment=$myenv
    done
done

And this is the output:

back@slash-PC:~/workspace/WSP$ bash maven_tests.sh
maven_tests.sh: line 2: Syntax error near unexpected token "("
maven_tests.sh: line 2: `projects = ("MAIN_EAR", "EJB_EAR", "SIT_EAR", "ENC_EAR", "ENVIRONMENT_EAR", "PRESS_EAR")

It seems that bash doesn't like how I declared the array.

What am I missing?


If it helps: I'm on Xubuntu 14.04 x64

like image 892
BackSlash Avatar asked Jan 09 '23 16:01

BackSlash


2 Answers

You can't have spaces around the = when defining variables in bash. Also, array elements are separated with a space, not with a ,. You'll have to use e.g.

myenvs=("dev" "cart")
like image 75
nos Avatar answered Jan 20 '23 17:01

nos


There are some syntax errors esp in declaring BASH arrays. (Spaces around = in array declaration and using commas between array elements).

Try this code:

#!/bin/bash
projects=("MAIN_EAR" "EJB_EAR" "SIT_EAR" "ENC_EAR" "ENVIRONMENT_EAR" "PRESS_EAR")

myenvs=("dev" "cart")

for prj in "${projects[@]}"; do
    for myenv in "${myenvs[@]}"; do
       mvn –am –pl "../$prj" clean package –Denvironment="$myenv"
    done
done
like image 27
anubhava Avatar answered Jan 20 '23 15:01

anubhava