I would advise not to modify your script with every run; it just does not make sense. If you run your scripts in the same session, keep the run No. in a variable; if run across session, keep it in a file, or, e.g. a symbolic link.
say the script is running from terminal1 and and it is trying to be started from the another say terminal2, I would not allow that. I would use some thing like
run_count=$(ps -ef | grep myscript | grep -vc grep)
if [ $run_count -gt 1 ]; then
echo "This run is not allowed. Please try after sometime"
exit
else
echo "starting script"
fi
So - if it's one single session, initialize and export a (global) variable, say, RUNNO. from your script, and then, in every new script, add one to it, like (in bash) ((RUNNO++)) . The "locking" that you allude to in your above post can be done with a "lock" file in e.g. /var/run . Just create a file there ( touch /var/run/myscript ) for the duration of your runs, and delete when done. Don't allow any new script if that file exists.