0

I have a couple of files with certain keywors (one is MAT1). For this key word i like to read the ID corresponding to it, put this together with the filename into an array. I tried the following (I am not very familiar with bash programming):

#!/bin/bash
Num=0
arr=( $(find . -name '*.mat' | sort) )

for i in "${arr[@]}"
do

  file=$(basename "${i}")

  while read -r line
  do

  name="$line"
  IFS=' ' read -r -a array <<< "$line"
    for index in "${!array[@]}"
    do

      if [ ${array[index]} == "MAT1" ]
      then
        out[$num] = "${array[index+1]} $file "
        let num++
        #printf "%-32s %8i\n" "$file" "${array[index+1]}"
      fi

    done

  done < "$i"

done

With this get the message

make_mat_list.bsh: line 21: out[0]: command not found

make_mat_list.bsh: line 21: out[1]: command not found

What is wrong here?

1 Answer 1

1

bash is white-space sensitive, your line below cannot have spaces.

out[$num] = "${array[index+1]} $file "

As for the reason for the error, the shell treats that particular line as the first word being a command out[$num] i.e. out[1]..etc and rest of it as arguments to it = and "${array[index+1]} $file ", which does not make any sense. Remove the spaces and do jsut

out[$num]="${array[index+1]} $file"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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