grep is not a program name, i would like to grep a file called as core in unix and redirect o/p to a text file and this script should run continouly to catch core file and keep o/p in a text file:
-
$file = "core" ;
-
if($file = core)
-
{
-
-
print( "core file found in a system");
-
}
-
else
-
{
-
print("core file not found in a system");
-
}
-
or is any effient way to write same program
Well, first, code tags are required to surround any code you place into your posts in the forums. Please make sure that the next time you post code that you use them. You can read more about using code tags
here.
Now, looking at your code I do see a few things that I would like to point out:
1. By setting $file to have the value "core", it will always test as true. What you should be doing is obtaining a directory listing from where ever the core dumps are put and then scanning the listing for the file name "core" You can take the directory listing using back tics and store it into an array as such:
-
my @listing = `ls <directory_path>`;
-
Each element in the array will then be one line from the listing. You can then use a foreach loop to cycle through the array and test each element with a regular expression to see if it is equal to "core". If it is, do what you need to do. If not, do something else.
The back tics, by the way, are another way of executing system commands.
2. Instead of testing the variable above, you are setting it equal to the value. The equal sign, by itself, is an assignment operator. You would need to use either "==" (for numbers and integers) or "eq" (for strings and text).
For my suggestion above though, you will be testing each element in the array against a Regular Expression to see if it is equal to the word core.
I highly suggest that you really read
a book on perl so that you can code better. Also, when writing a perl script, you reallly need to include the pragmas "use strict" and "use warnings". They will help eliminate all of the silly nonsensical errors that us coders make and you will also find that most people here won't do much with your code if you don't use them.
That said, here is an untested bit of code to do what you want (and what I explained above):
-
-
use strict;
-
use warnings;
-
-
my $dumpdir = "/tmp";
-
-
my @listing = `ls $dumpdir`;
-
-
foreach my $line (@listing)
-
{
-
if($line =~ m/core/)
-
{
-
print("core file found in $dumpdir");
-
-
# pstack is tool in solaris to read core dump
-
system("pstack ${dumpdir}/core >test.txt");
-
}
-
}
-
-
Now, I could have put a print statement in an else to print "no core found", but it would have printed that for EVERY single line that it cycled through. Putting it in is completely up to you.
I hope this works for you or gets you on the right path. I really suggest reading the book at that link so you can get the basic concepts down.
Regards,
Jeff