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
  

Transforming File Names

The following small script simply does a file name replacement. One place where it is useful is when I have a bunch of log files with fully qualified names but then change the server hostname to not be fully qualified and I want all stardard historical information.

#!/bin/ksh

## usage: transtr "Initial String" "Final String" [run]
##
## This command simply transforms a string with the
## default sed substitution.  You must include
## 'run' in the third field for it to execute, otherwise it
## simply returns the two strings.  You could actually
## not specify run and pipe the two fields into another
## command if necessary.
##

if [[ $1 == "" ]]
   then
      grep "^##" $0 | cut -c 3-
      exit
fi

while read STRING
   do
      NEW=`echo $STRING | sed "s/$1/$2/g"`
      echo $STRING $NEW
      if [[ $3 == "run" ]]
	then
       	   mv $STRING $NEW
      fi
   done

Script started on Tue 02 Aug 2011 10:33:28 AM CDT
 #  ls 
20110707_05_french-roast.deadlycoffee.com_aixbase.sbstats
20110708_05_french-roast.deadlycoffee.com_aixbase.sbstats
20110709_05_french-roast.deadlycoffee.com_aixbase.sbstats
20110710_05_french-roast.deadlycoffee.com_aixbase.sbstats
20110711_05_french-roast.deadlycoffee.com_aixbase.sbstats
20110712_05_french-roast.deadlycoffee.com_aixbase.sbstats
 #  ls | transtr ".deadlycoffee.com" "" 
20110707_05_french-roast.deadlycoffee.com_aixbase.sbstats 
	 20110707_05_french-roast_aixbase.sbstats
20110708_05_french-roast.deadlycoffee.com_aixbase.sbstats 
	 20110708_05_french-roast_aixbase.sbstats
20110709_05_french-roast.deadlycoffee.com_aixbase.sbstats 
	 20110709_05_french-roast_aixbase.sbstats
20110710_05_french-roast.deadlycoffee.com_aixbase.sbstats 
	 20110710_05_french-roast_aixbase.sbstats
20110711_05_french-roast.deadlycoffee.com_aixbase.sbstats 
	 20110711_05_french-roast_aixbase.sbstats
20110712_05_french-roast.deadlycoffee.com_aixbase.sbstats 
	 20110712_05_french-roast_aixbase.sbstats
 #  ls | transtr ".deadlycoffee.com" "" run 
20110707_05_french-roast.deadlycoffee.com_aixbase.sbstats  
    20110707_05_french-roast_aixbase.sbstats
20110708_05_french-roast.deadlycoffee.com_aixbase.sbstats  
    20110708_05_french-roast_aixbase.sbstats
20110709_05_french-roast.deadlycoffee.com_aixbase.sbstats  
    20110709_05_french-roast_aixbase.sbstats
20110710_05_french-roast.deadlycoffee.com_aixbase.sbstats  
    20110710_05_french-roast_aixbase.sbstats
20110711_05_french-roast.deadlycoffee.com_aixbase.sbstats  
    20110711_05_french-roast_aixbase.sbstats
20110712_05_french-roast.deadlycoffee.com_aixbase.sbstats  
    20110712_05_french-roast_aixbase.sbstats
 #  ls | transtr ".deadlycoffee.com" "" run 

 #  ls 
20110707_05_french-roast_aixbase.sbstats
20110708_05_french-roast_aixbase.sbstats
20110709_05_french-roast_aixbase.sbstats
20110710_05_french-roast_aixbase.sbstats
20110711_05_french-roast_aixbase.sbstats
20110712_05_french-roast_aixbase.sbstats
20110713_05_french-roast_aixbase.sbstats
20110714_05_french-roast_aixbase.sbstats
20110715_05_french-roast_aixbase.sbstats
 #  exit 
exit

Script done on Tue 02 Aug 2011 10:34:13 AM CDT
  

Wrapping text around a string with sed & functionality

Need to use sed to do a string replacement.

This:


# Comments until end of line


Should be:


<span class=comment>#Comments until end of line</span>


This can be accomplished with sed by using the ‘&’ operator which pastes in the match space.

 

Script started on Mon 01 Aug 2011 04:47:45 PM CDT

deadlycoffee:/tmp #  sed 's/^#.*.$/(&)' test | sed 's/^#$/(&)/' 
(#)
(#)
(#)
this is not a comment
this isn't either
(## This is)
(# So is this.)
deadlycoffee:/tmp #  exit 
exit

Script done on Mon 01 Aug 2011 04:48:52 PM CDT

 

In the above example, I do a much simpler replacement, but it is the same idea.

Reporting Packages

Is it possible for a reporting solution to both ‘look’ sufficiently effective to be purchased by the average IT department and ‘be’ sufficiently effective to accomplish anything of value? This is the real question and it seems sad that solutions may have to either pick one side or the other.

Don’t try this as root

Contexts of a tiny script that you should never run:

cp $0 $$ # Make a copy of yourself
sh $$ & # Run the copy in the background
sh $0 & # Rerun a new version of yourself in the background

This will probably take over your server or at least use up all of the filesystem pointers in its filesytem. In a minute or so you would have thousands of these running and doubling each generation.

This does demonstrate an interesting version of ‘the game of life’. If the copy (cp) could be a little less reliable and the whole thing could work in a few messed up ways, this could actually evolve into some other different tiny script.

Self-reference is always fun times.