newbie01 perl wrote:
Hi,
Hello,
Do the following set of codes does the same thing?
FYI, am more interested on whether I can use CODE 03 to pipe/re-direct
output from the OS commands, i.e. ps, df etc.
Probably, yes.
Will using | works on Windows as well?
Sorry, I can't help you with Windows, I never use it.
perldoc perlport
perldoc perlwin32
Am very new to Perl and has mostly done UNIX shell scripting. Am wanting to
write Perl scripts where a UNIX script/command has to run and am having to
grep for things that I need to check, for example, ps -ef | grep
ora_pmon_TEST1 to check for the pmon process of the Oracle TEST1 database.
For example:
open(PS,"ps -e -o pid,stime,args |") || die "ps command failed $! \n";
It is usually better to use the list form of pipe open so that you don't
invoke the shell as well. And it is usually better to use lexically
scoped filehandles instead of global ones.
open my $PS, '-|', 'ps', '-e', '-o', 'pid,stime,args' or die "ps command
failed $!\n";
while (<PS> )
{
print $_;
If you are going to use $_ then you may as well use it everywhere:
while ( $_ = <PS> ) {
print $_;
Or better yet, use a lexically scoped variable instead:
while ( my $line = <PS> ) {
print $line;
--- Check what $_ is and do some work, perhaps I should be doing regex's
and if $_ =~ /searchstring/ ??? ---
Yes, something like that.
}
close PS;
When you open a pipe to an external command you should also verify that
the pipe filehandle closed correctly.
perldoc -f close
Response/feedback will be much appreciated. Thanks in advance.
--------
CODE 01:
--------
open(FILE, "x.txt") or die("Unable to open file");
You should include the $! variable in the error message so you know why
open failed and maybe the file name as well. You should probably use a
lexical filehandle and the three argument form of open.
while (<FILE> )
{
print $_;
}
close FILE;
That demonstrates standard file processing.
--------
CODE 02:
--------
system('ls x.txt 1>/dev/null 2>/dev/null');
'ls x.txt' will basicly print 'x.txt' to STDOUT if the file exists or an
error message if it doesn't but you have redirected both STDOUT and
STDERR to /dev/null.
$result=$?;
Because of the redirection system() has to run a shell which in turn
will run 'ls' so $? will be the error code the shell returns and not the
error code that 'ls' returns.
if ( $result eq 0 ) {
Why not just:
if ( -e 'x.txt' ) {
print "File exist ... open the file and put into an array \n";
open(FILE, "x.txt");
You don't test the return status of open so the file could have been
deleted by the time you get here. Just use open() and forget the other
tests because if you can open the file successfully then you know that
the file exists and has been successfully opened.
open my $FILE, '<', 'x.txt' or do {
warn "Cannot open 'x.txt' $!";
exit 9;
};
while ( <$FILE> ) {
print;
}
close $FILE;
while (<FILE> )
{
print $_;
}
close FILE;
} else {
print "File does not exist ... exiting \n";
exit 9;
}
--------
CODE 03:
--------
open(CAT,"cat x.txt |") || die "Unable to open file $! \n";
while (<CAT> )
{
print $_;
}
close CAT;
exit 0;
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/