1826496 Members
2766 Online
109692 Solutions
New Discussion

Re: PERL help needed

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

PERL help needed

HI,

I have a PERL script below which checks for the existence of filenames having the string "spool" as their prefixes and tokenizes the contents of those "spool" files. Purpose of this script is write the necessary information from the "spool" into a log file to indicate users which have been added/removed from a group by respective owner.

Contents of the "spool" as as follows, separated by colons:
remove:ken_lee:groupA:joe_wong:ken_lee
add:ken_lee:groupA:alice:ken_lee

field 1: action add/remove to indicate a user is to be added or remove from a NIS /var/yp/src/group file

field 2: group owner i.e ken_lee
field 3:name of group i.e groupA
field 4: name of user to be added/removed from NIS master group file i.e alice or joe_wong

field 5: group owner again

The script would need to have the group name, group owner and user to be added/remove and the action field from the "spool" file to be written into a log file. The log file would need to be in the format of:
groupname owner user date action
--------------------------------------
groupA ken_lee alice Jan10 add

Names of the log file should be have the following form:
year-month-groupname
e.g. 2003-Jan-groupA
The date field in the log file would be based on date of the "spool" file obtained by doing a "ls -l spool*".

However in my script, I tried creating a file of the form year-month-groupname to be able to write the groupname, owner, user, date, action into it, and the script produced the error attached in this message.

Could someone kindly tell me how I should fix the error in the attachment?

#!/usr/bin/perl
@spoolList = `ls -l /tmp/spool* |grep -v total`;

foreach $i (@spoolList){
@splitSpoolList = split(/\s+/, $i);
$spoolFile = $splitSpoolList[8];
$day = $splitSpoolList[6];
$time = $splitSpoolList[7];
$month = $splitSpoolList[5];
&processSpoolFileList($spoolFile);
}

sub processSpoolFile{
$fileName = shift(@_);
@spoolFileTokens = split(/:/,`cat $fileName`);
#foreach $k (@spoolFileTokens){
# print "$k \n";
#}
$action = $spoolFileTokens[0];
$owner = $spoolFileTokens[1];
$grpName = $spoolFileTokens[2];
$user = $spoolFileTokens[3];
#for($a=0; $a < @spoolFileTokens; $a++){
# print "\$spoolFileTokens[$a] = $spoolFileTokens[$a]
\n";
#}
&writeToLog($action, $owner, $grpName, $user, $day, $time,
$month);
}

sub writeToLog{
local($action, $owner, $grpName, $user, $day, $time,
$month) = @_;

$year = &getYear;
print "$year \n";
$logFile = $year"-"$month"-"$grpName;
print "\$logFile = $logFile \n";
open(file, ">>/home/$logFile") || die "open $logFile for append \n";
print file "group\t owner\t user\t date\t action \n";
print
file "$grpName\t$owner\t$user\t$date\t$action\n";
close file;

}

sub getYear{
$tmpDate = `date`;
print "$tmpDate \n";
@dateStuff = split(/\s+/,$tmpDate);
$year = $dateStuff[5];
print "\$year = $year \n";
return $year;
}

Also, how do I write the contents of the log file with the headers as specified below? :
groupname owner user date action
--------------------------------------


Could anyone kindly help me out?

Thanks

3 REPLIES 3
harry d brown jr
Honored Contributor
Solution

Re: PERL help needed


Line 41 and line 42 need to be concatenated:

print file "$grpName\t$owner\t$user\t$date\t$action\n";

Line 25, the \n"; needs to be wither appended to the above print statement or commented out

Line 37, change to this:
$logFile = $year."-".$month."-".$grpName;

Periods are string concat operators.

Line 13, the subroutine is called processSpoolFile, but you call it on line 10 as processSpoolFileList. So either change the subroutine name from processSpoolFile to processSpoolFileList, or change line 10 to reflect the name of processSpoolFile.

live free or die
harry

Live Free or Die
harry d brown jr
Honored Contributor

Re: PERL help needed


I attached the changes, but you need to REPLACE the first line from /opt/perl/bin/perl to your /usr/bin/perl.

have fun

live free or die
harry
Live Free or Die
David_246
Trusted Contributor

Re: PERL help needed

Hi,

If I seen it right you have the \n on a new line. Don't know if this is messed up by cut and past, but this might be your problem.
If you want answers on Perl questions, please look at the site : http://learn.perl.org/
You can subscribe yourself to a mailinglist, and I can garantuee your problem will be solved in minutes.

# print "\$spoolFileTokens[$a] = $spoolFileTokens[$a]
\n";

Best Regs David
@yourservice