I'm trying to assign a value if the array at a certain index is empty, but I'm getting a command not found error.

while IFS=$';' read -r -a array
do

# if empty set Not Available
if [[ -z ${array[6]} ]] ; then
        $array[6]="Not Available"
        echo barcode is ${array[6]}
fi

  echo '  <Product>' >> $file_out
  echo '    <Reference>'${array[0]}'</Reference>' >> $file_out
  echo '    <Name>'${array[1]}'</Name>' >> $file_out
  echo '    <Category>'${array[2]}'</Category>' >> $file_out
  echo '    <Price>'${array[3]}'</Price>' >> $file_out
  echo '    <Scale>'${array[4]}'</Scale>' >> $file_out
  echo '    <Manufacture>'${array[5]}'</Manufacture>' >> $file_out
  echo '    <Barcode>'${array[6]}'</Barcode>' >> $file_out
  echo '    <DatePub>'${array[7]}'</DatePub>' >> $file_out
  echo '    <Image>'${array[8]}'</Image>' >> $file_out
  echo '    <Availability>'$availability'</Availability>' >> $file_out
  echo '    <Supplier>'$Supplier'</Supplier>' >> $file_out

echo '  </Product>' >> $file_out
done < $file_in

this code is working except for the "value assign part":

# if empty set Not Available
if [[ -z ${array[6]} ]] ; then
        $array[6]="Not Available"
        echo barcode is ${array[6]}
fi

Error:

./convert-csv-to-xml: row 20: LM113A[6]=Not Available: command not found
barcode is

row 20 is:

$array[6]="Not Available"
up vote 0 down vote accepted

We don't need to use $ for assigning a string to an array.

Just try with

array[6]="Not Available"
  • whoooops!!! thank you – Kintaro Aug 10 at 10:22

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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