Update a field in a file based on condition

Hi

i am new to scripting. i have a file file.dat with content as :

 
CONTENT_STORAGE PERCENTAGE FLAG:
/storage_01 64% 0
/storage_02 17% 1

I need to update the value of FLAG for a particular CONTENT_STORAGE value

I have written the following code

 
 #!/bin/sh
threshold=20
alert="threshold"
#Get default file store 
defaultStorage1=DEFAULT_STORE 
#DEFAULT_STORE is obtained as storage_01 from database table
 
echo "file reading"
{
 
while read CONTENT_STORAGE PERCENTAGE FLAG
do
 
    #Code for reading through lines and ignoring all until CONTENT_STORAGE is found
 
    # Ignore all lines until content storage namematches the name given 
    if [[ ${CONTENT_STORAGE} != $defaultStorage1 ]]; then
 echo "here"
        continue
    fi   
echo "CONTENT_STORAGE = ${CONTENT_STORAGE} / PERCENTAGE = ${PERCENTAGE} / FLAG = ${FLAG}" 
done < file.dat 
}
echo "Getting the percentage og $defaultStorage1 *********************"
#check the percentage stored in defaultStorage
df -H | grep $defaultStorage1 | awk '{ print $4 " " $5}' | while read output;
do 
  #Get the used percentage
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1)
 echo "used percentage .................. $usep"
# Check if the used percentage is > threshold_value. Do nothing till its > threshold
   if [ $(($usep)) -le $(($threshold)) ]; then
 echo "Used percentage of $defaultStorage1 ................. $(($usep))"
       continue
 
else 
 
echo "Used percentage is greater than threshold"
 
    # Ignore all lines until content storage namematches the name given 
    if [[ ${CONTENT_STORAGE} != $defaultStorage1 ]]; then
        continue
    else
       $FLAG = 1  
#i am trying to update the FLAG field in here. But not happening. Also while creating the tmp file file.mp and rewriting to file.dat, the headings are ignored. i want to just modify the FLAG value for /storage1 to 1
    fi 
    echo $CONTENT_STORAGE $PERCENTAGE $FLAG >> file.tmp
  done < file.dat  
mv file.tmp file.dat
   # check the percentage of each other filestore with flag =0
   #get the least used filestore
   #update the default store
 
   fi
 
done
 
 

Thanks for all the help in advance

In sh one says FLAG=1 ?

Updating a field in a file is pretty hard in sh. You can make a new file beside or in place of the original, or append.

The df command will do specific dirs without a grep.

I hope the first line leading space is a pasting typo.