Operating System - HP-UX
1833053 Members
2269 Online
110049 Solutions
New Discussion

Redirecting a system command output to a file

 
Mike Keys
Regular Advisor

Redirecting a system command output to a file

As you know, I have been asking for help regarding a user administration program that I am writing. All of your responses have been very helpful, since I an a novice to Perl. I will get the programming books as (A. Clay suggess), but this is something that I thought would not be that hard and if I got stuck, I could probably find an answer somewhere on these forums.

I have limited programming experience, but know the basic structure of most programs.

I am at the point where I am trying to build the command to add a user and I think I have it. However, I want the output redirected to the command line rather than performing the operation on the /etc/passwd file itself at this time. Here is the subroutine and how it is called in the program:


SUB:

sub adduser {

my $cmd = sprintf("/usr/sbin/useradd -u %u -g %g -c %c -b %b -s %s %l",$nuid,$ngid,$ncomment,$nbdir,$nshell,$nuser);
my $status = "system($cmd) > /tmp/passwd.fil 2>&1";
return ($status);
}



Here is how it is called:

my $status = adduser($highestuid,$gid,$newgecos,$dir,$shell,$new);
sleep 2;
$status == 0 ? print "User addedd successfully" : "Error";

Any suggestions?
9 REPLIES 9
Rodney Hills
Honored Contributor

Re: Redirecting a system command output to a file

I have in these instances replaced the command with "echo". So change /usr/sbin/useradd to echo, then when it runs command line is displayed.

HTH

-- Rod Hills
There be dragons...
David Child_1
Honored Contributor

Re: Redirecting a system command output to a file



sub adduser {

my $cmd = sprintf("/usr/sbin/useradd -u %u -g %g -c %c -b %b -s %s %l",$nuid,$ngid,$ncomment,$nbdir,$nshell,$nuser);

print "$cmd > /tmp/passwd.fil 2>&1\n";

}

If just printing to stdout then I don't see much purpose in checking the status so I dropped that line out.
Mike Keys
Regular Advisor

Re: Redirecting a system command output to a file

still no luck
Mike Keys
Regular Advisor

Re: Redirecting a system command output to a file

david, i am trying your suggestion.
Mike Keys
Regular Advisor

Re: Redirecting a system command output to a file

still doesn't write to file.

It also appears that the variables are not passing in their data. The result was that I received the following line written to STDOUT:

/usr/sbin/useradd -u 0 -g 0 -c -b 0 -s %l > '/tmp/passwd.fil' 2>&1
Rick Garland
Honored Contributor

Re: Redirecting a system command output to a file

I am able to run a perl script and using the 'tee -a $LOGFILE' I get the display on the screen like I should plus I get all activity placed into the $LOGFILE.

The $LOGFILE can be an absolute file name as well.

Example;
./useradd.pl | tee -a /tmp/mylogfile

I get the output on screen so I can see what is happening plus I get the info in the /tmp/mylogfile as a result of the 'tee -a' command

Mike Keys
Regular Advisor

Re: Redirecting a system command output to a file

Thanks for the suggestions, but any reason why my variables are not being passed?
Mike Keys
Regular Advisor

Re: Redirecting a system command output to a file

o.k. almost there. i was able to get the file generated. However, I am having trouble get the "" needed around the comment for the command to run. Here is the sub now:

sub useradd {
my $tmpfile = "/tmp/passwd.fil";
my $cmd = sprintf("/usr/sbin/useradd -u $highestuid -g $dgid -c '$desc' -d $ndir -s $dshell $new");
system("echo $cmd | tee -a $tmpfile 2>&1");
}
David Child_1
Honored Contributor

Re: Redirecting a system command output to a file

Sorry Mike, I misunderstood your question. For some reason I thought you just wanted to print to stdout.

In perl the best way to handle writing to files is within perl itself. Try this;

sub useradd {
my $tmpfile = "/tmp/passwd.fil";
my $cmd = sprintf("/usr/sbin/useradd -u $highestuid -g $dgid -c '$desc' -d $ndir -s $dshell $new");

open (TMPFILE,">${tmpfile}");
print TMPFILE "$cmd\n";
close (TMPFILE) || die "\nCould not write to file: $tmpfile:$!";

}

The '|| die' part should give you an error if you can't write to the file, but test it to be sure it works correctly.

David