what are you trying to do? ... btw, your test as i understand it will never hold true as you're checking for an environment variable that should already be set BEFORE the script gets to the test ... if not already set, who's going to set it?
I wouldnt necessarily do it this way but you could set a flag within the shell (preferably at the top of the script) using sed. Basically, create a pattern that is unique so that you can use a simple sed statement to set a shell variable before the shell exits. If the shell is restarted it could perform work based upon the setting of this variable. To make this work you would need to create a function that is a single point of exit within the script. Anything goes wrong you call the function, runs the sed, and exits gracefully.