Convenient debugging with exit, unformatted data read

The following script is an example of two things. The first is how to preset a trap to do all of your cleanup, then you can exit anywhere in your script and it will still remove your temporary files. The other is just a silly idea using grep to handle unformatted data after the exit.

Script started on Fri 23 Sep 2011 02:46:04 PM CDT
 #  cat traptest 
#!/bin/bash
trap "rm $$.*" INT TERM EXIT #
grep -v "#" $0
ls > $$.ls #
exit # Everything after this will not be executed,
# but the trap above will always clean up your temp
# files, so you could use exit as a debugging tool

You can type all sorts of stuff here and it
will be displayed, but need not be echoed.
 #  ./traptest 

You can type all sorts of stuff here and it
will be displayed, but need not be echoed.
 #  exit 
exit
Script done on Fri 23 Sep 2011 02:46:14 PM CDT
  

Leave a Reply

Your email address will not be published. Required fields are marked *