GolfScript, 30 chars
{`".~"+"#{$<.read}".@=!{~}*}.~ This program reads the contents of a file named on the command line and, if it does not exactly equal the code above, interprets it as GolfScript. If the input is exactly equal to the code above, it will simply be printed unchanged (except for a newline appended to the end).
This is a fairly straightforward adaptation of this self-identifying programthis self-identifying program. Specifically:
{ }is a code block literal in GolfScript..~, applied to a code block, duplicates the block and executes the copy.
Inside the code block:
`stringifies the copy of the code block.".~"+appends the characters.~to it, yielding a string containing the source code of the program."#{$<.read}"is a documented hack that allow the execution of Ruby code within GolfScript. In this case, it executes the Ruby statement$<.read(shamelessly stolen from Lowjacker's Ruby solutionLowjacker's Ruby solution), which reads and returns the contents of any files specified on the command line. This hack is needed because GolfScript itself provides no explicit file I/O capabilities..@duplicates and shuffles the elements on top of the stack so that the stack contains two copies of the file contents followed by the source code of this program.=!compares the top two items on the stack (i.e. the file contents and the source), returning 1 if they are different and 0 if they are the same.{~}*evaluates the remaining copy of the file contents as GolfScript code, but only if the result of the comparison is 1. (Technically, it executes the code block{~}as many times as given by the number on the stack, i.e. 0 or 1 times. Inside the block,~is the GolfScript eval operator.)
Ps. If reading the code to execute from stdin is allowed, this challenge can be solved in 21 characters without having to shell out to Ruby:
{`".~"+1$=!{""\~}*}.~ This program will read an input string from stdin and, if it does not match its own source, executes it (with an empty input). Like the program above, input that does match the source is simply echoed back.