Saad Saad - 1 month ago 9
Linux Question

Error in Declaring Arrays in Shell Script

I have a script in which I'm reading a file into an array line by line.

#!/bin/bash
echo "Enter audio file name. (File must be of .wav format)"

read fileName

echo "Enter path of the audio file"

read path

echo "Enter folder name"

read outputfolder

mkdir -p $outputfolder

echo "Processing $fileName"
./ilp_diarization2.sh $path/$fileName.wav 120 $outputfolder


#value="$(grep "$fileName.*S" $outputfolder/$fileName/$fileName.g.3.seg)"


#echo "${value}"

awk '{ print $3" "$4}' $outputfolder/$fileName/$fileName.g.3.seg > a

#var=$(awk '{ print $1 }' a) > 2

#echo "${var[0]}


getArray() {
array=() # Create array
while IFS= read -r line # Read a line
do
array+=("$line") # Append line to the array
done < "$1"
}

getArray "a" #file name


The error I'm having is in the array deceleration.

Syntax error: "(" unexpected (expecting "}")


I have tried using

array="()"


but none of them seems to work.

Here are the content of the file:

S0 [
42 4677
S10 [
4719 1266
6020 3618
9667 8463

Answer Source

Seems your version doesn't support arrays, otherwise readarray is a bash builtin and does same as function

help readarray

readarray -t my_array < filename