deleting the part of the file(overwrite) using start and end point

no in actual code am setting flag in some "if" conditon which is inside this grep-while loop... when this while loop ends i am utilizing the set flag to do some operations but flag is reset to 0 when this grep-while loop ends... what to do ??

What is the if condition? may be we can work that out, that might be easy...

--ahamed

okay wait.. let me paste the partial code here...

flag=0
while read linexT
do
                                                 len=${#linexT}
                                                 charlast=${linexT:$len-1:$len}
                                                charfirst=${linexT:0:1}
                                                 if [ "$charlast" = "," ]
                                                 then
                                                      line11=${linexT:0:$len-1}
                                                 else
                                                      line11=$linexT
                                                 fi
                                                 if [ "$charfirst" = ">" ]
                                                 then
                                                           
#                                               here somewhere inbetween i am checking whether 
if flag=0
then
                do someting here
fi
fi
 
                   if [[ "$charfirst" = "<" ]]
                                                 then
                                                         line11=${line11:2:$len}
                                                        echo "missing in second: $line11"
                                                        colname1=$( echo $line11 | awk -F\` '{print $2}' )
                                                       line333=`cat tablextract2.sql | grep -w -i $colname1`
                                                        grep -w -i $colname1 tablextract2.sql | while read line22 ; do
 if [[ `expr match "$line11" ".*PRIMARY KEY.*"` = "0" ]]  &&  [[ `expr match "$line22" ".*PRIMARY KEY.*"` = "0" ]] && [[ "$key1" != "KEY" ]] &&  [[ "$key2" != "KEY" ]] && [[ `expr match "$line11" ".*UNIQUE KEY.*"` = "0" ]]  &&  [[ `expr match "$line22" ".*UNIQUE KEY.*"` = "0" ]]
         then
                                if grep -i -q "auto_increment" <<<$line22
                                then
                                         insert_column "$table_name1" "PRIMARY KEY (\`$colname1\`)"
                                         modify "$table_name1" "$line22"
                                        flag=1
                                        echo "flag: $flag"
                                else
                                         modify "$table_name1" "$line22"
                                fi
fi
done
 
done < tmp.sql
 
                                                             

---------- Post updated at 02:18 PM ---------- Previous update was at 02:14 PM ----------

and also how to close a duplicate thread...?

One way is to make use of tmp files...

grep -w -i $colname1 tablextract2.sql > /tmp/tmp.$$
while read line; do
  echo "$line"
  flag=1
  echo "flag: $flag"
done < /tmp/tmp.$$
 
echo "flag: $flag"
 
rm -f /tmp/tmp.$$

--ahamed

1 Like

oh this will work i guess.. thanks :-)... but what is tmp.$$...?

$$ is the process ID of the shell - often used to make (somewhat) unique filenames.

1 Like

oh okay got it :slight_smile: thanks

This is not an issue with bash. Once you understand that the pipe launches a subshell, it comes as obvious that the parent shell does not know what the subshell did to its variable.

1 Like

oh does it mean like a sub thread in main thread.... or the scope of the variable dude to different shells..?

The behaviour is not same in ksh.

#!/bin/ksh
var=123;
echo $var
echo $var | while read line
do
  var=456
  echo $var
done
echo $var
 
[root@bt]./run
123
456
456
#!/bin/bash
var=123;
echo $var
echo $var | while read line
do
  var=456
  echo $var
done
echo $var
 
[root@bt]./run
123
456
123

--ahamed

2 Likes

Thanks everyone.. i finally completed the code.. :slight_smile: woudlnt have done without the help from all you guys.. thanks again :slight_smile: