-1

This question already has an answer here:

Hi am trying to write a script that updates a row read from file , Every thing is working fine so far except the following mentioned line at the end

here is my script :

#!/bin/bash     

#INITIALIZE PATHS
DB_PATH=${1}      
DATA_PATH="$DB_PATH/data/"
METADATA_PATH="$DB_PATH/metadata"   

#LOAD THE META DB FILE AND READ DATABASE NAME IN DB_NAME 
#READ ALSO COLUMNS IN AN ARRAY CALLED TABLE_NAMES                      
METADATA_LOAD=$(<"$METADATA_PATH/DB")
IFS=$'+'
read -a NAMES <<< "$METADATA_LOAD"



#PROMPET FOR TABLE TO UPDATE
echo ""
echo ""
echo ""
echo "You Currently Working with Database ${NAMES[0]}" 
echo ""
echo "You Have The Following Tables to Choose From :"
IFS=' '
for token in ${NAMES[@]:1}
do
echo "  $token"
done
echo ""
echo ""
echo ""
echo "Please Write The Name Of The Table You Want To Update :"
read CURRENT_TABLE


#Form META DATA TABLE FILE PATH
TABLE_DATA_PATH="$DB_PATH/data/$CURRENT_TABLE"
TABLE_METADATA_PATH="$DB_PATH/metadata/$CURRENT_TABLE"


#PROMPET WHICH COLUMN IS NEEDED TO CHECK FOR EQUALITY
echo ""
echo ""
echo ""
echo "The Table You Selected Have The Following Columns"
i=0 
TABLE_METADATA=$(<"$TABLE_METADATA_PATH")
IFS='+'
read -a CURRENT_TABLE_META <<< "$TABLE_METADATA"
IFS=' '
for token in ${CURRENT_TABLE_META[@]:1}
do
i=$(($i+1))
echo $i $token
done
echo "Please Enter The Number Column to Check For Equality"
read TEST_COLUMN_NUM
echo ""
echo ""
echo "Please Enter The Column Value to Check For Equality"
read TEST_COLUMN_VAL

echo "Please Enter The Column Number to Modify"
read MODIFY_COLUMN_NUM

echo "Please Enter The New Column Value"
read MODIFY_COLUMN_VAL

#Form DATA TABLE FILE PATH
TABLE_DATA=$(<"$TABLE_DATA_PATH") 


IFS=' '
read -a ROWS_ARRAY <<< "$TABLE_DATA"  




for CURRENT_ROW_INDEX in ${!ROWS_ARRAY[@]}
do

  IFS=':'
  read -a COLUMNS_ARRAY <<< "${ROWS_ARRAY[CURRENT_ROW_INDEX]}"

   for CURRENT_COL_INDEX in ${!COLUMNS_ARRAY[@]} 
   do

       if [[ ${COLUMNS_ARRAY[$CURRENT_COL_INDEX]} == $TEST_COLUMN_VAL  &&  $CURRENT_COL_INDEX -eq $(($TEST_COLUMN_NUM-1)) ]]
       then

           IFS=' '
           COLUMNS_ARRAY[$MODIFY_COLUMN_NUM] = "ABC"



       fi   

   done
done

I did not finish the script yet but I have an error at the line :

 COLUMNS_ARRAY[$MODIFY_COLUMN_NUM] = "ABC"

it gives me Error

./Update: line 91: COLUMNS_ARRAY[3]: command not found

I want to assign to the array at the given index replacing the old value, please help me to know the reason of error and how to solve it

marked as duplicate by ilkkachu, Rui F Ribeiro, jayhendren, G-Man, mdpc Jan 24 '18 at 1:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I would advise in the future not saying "a code", it does not sounds well. A shell script usually is best saved in a file with an ".sh" extension. – Rui F Ribeiro Jan 23 '18 at 20:56
  • you mean the word "code" I mentioned above ? .. if you mean so .. that's un accurate use of term may be because i am a beginner ..thanks for your advice :D – AbdAllah Talaat Jan 23 '18 at 21:03
  • @ikkachu thanks that was the reason ... you are correct ..it worked perfectly fine thanks – AbdAllah Talaat Jan 23 '18 at 21:08
1

Same thing as with regular variable assignment in the shell, don't put whitespace around the =.

Given i=123, this is an assignment to index 123 in array:

array[$i]="abc"

This tries to run a command called array[123] (or whatever the result if a filename matching that as a glob pattern is found):

array[$i] = "abc"

Also, every time you use ${array[@]}, with or without ! or :n:m, you are likely to want to put quotes around it. Without quotes, the array values are subject to word splitting, with them, they aren't.

You have, e.g. for token in ${NAMES[@]:1} and for .. in ${!ROWS_ARRAY[@]}

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