netguy has asked for the wisdom of the Perl Monks concerning the following question:
I have written a script that will only ever run in a linux environment. The purpose of the script is to be piped to, look for certain strings and modify or not and send to STDOUT or not similar to grep/awk/sed but with a more specific purpose that those don't address easily.
I have looked though other posts, but didn't find anything. Not sure if that's because it's impossible, everyone else already knows but me or I just don't know how to search.
I'm trying to determine within the script the context in which it is being executed, and/or better yet, the process/command I'm receiving STDIN from. With my current script, if it runs as in the first example below, it just sits waiting for STDIN and won't exit without CTRL-C or some other non-graceful intervention. When it's receiving STDIN as in the second example, everything works as expected.
For Example:This is an example of the script logic I'm trying to accomplish:
#!/usr/bin/perl use strict; my @output; my $stuff; if ( <not piped to> and @ARGV == 0 ) { print "This is how you use this perl script...\n"; exit; } while (<STDIN>) { if ($_ =~ m/start/ ) { push @output, chomp; $stuff = 1; } elsif ( $_ =~ m/end/ ) { push @output, chomp; changout(@output); undef $stuff; } elsif ($stuff) { push @output, chomp; } else { print STDOUT $_; } } sub changout { foreach my $line (@_) { if ( $line =~ m/something/ ) { print STDOUT $line; } else { print STDOUT $line."\n"; } } }
|
|---|