- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Redirecting a system command output to a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2005 02:02 AM
07-14-2005 02:02 AM
Redirecting a system command output to a file
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2005 02:05 AM
07-14-2005 02:05 AM
Re: Redirecting a system command output to a file
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2005 02:09 AM
07-14-2005 02:09 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2005 02:09 AM
07-14-2005 02:09 AM
Re: Redirecting a system command output to a file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2005 02:11 AM
07-14-2005 02:11 AM
Re: Redirecting a system command output to a file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2005 02:20 AM
07-14-2005 02:20 AM
Re: Redirecting a system command output to a 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2005 02:37 AM
07-14-2005 02:37 AM
Re: Redirecting a system command output to a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2005 03:00 AM
07-14-2005 03:00 AM
Re: Redirecting a system command output to a file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2005 03:44 AM
07-14-2005 03:44 AM
Re: Redirecting a system command output to a file
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");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2005 04:44 AM
07-14-2005 04:44 AM
Re: Redirecting a system command output to a file
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