1

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
21
  • How are you executing the script? sh or bash? Commented Aug 22, 2017 at 8:22
  • It's a .sh file. Commented Aug 22, 2017 at 8:23
  • Try ./yourfile to execute your file. Commented Aug 22, 2017 at 8:24
  • File name is go2.sh. I'm executing it by sudo ./go2.sh command. It still gives me the error. I think the error is in the code and not the command. Commented Aug 22, 2017 at 8:25
  • Try to add #!/bin/bash to the first line. Commented Aug 22, 2017 at 8:26

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

I have bash version 4.3.48(1)-release (x86_64-pc-linux-gnu). Which version should I have to use readarray command?
your script isn't executing with this shell : add ps -p $$ inside script, also echo "${BASH_VERSION}" should display 4.3.48
I have edited my post. Kindly go through it again and see if there's any error in the code.
I made a new file .sh file with and copy pasted the script with readarray command, it is now working correctly. I still don't understand what is the issue with the previous file. It has the same code on it as the new one.
the interpreter may not be what you expect. Have you added the commands ps -p $$ and echo "version: ${BASH_VERSION}", also can you show the command line to launch the script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.