- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: useradd in perl script
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
Discussions
Discussions
Forums
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
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
тАО11-09-2010 07:35 AM
тАО11-09-2010 07:35 AM
#!/usr/bin/perl
open(fh, "
@line=split(':', $line);
print "$line[0]\n";
print "$line[1]\n";
print "$line[2]\n";
$cmd1="/usr/sbin/useradd -u $line[1] -c $line[2] -s /bin/bash -g users -m $line[0]";
print "$cmd1\n";
if (system($cmd1)) {print "failed\n";}
}
close(fh);
here is the testfile:
#cat testfile
#login-name:uid:comment
testid:12345:test1-account
here is the output after run the script:
# ./test-perl.pl
testid
12345
test1-account
/usr/sbin/useradd -u 12345 -c test1
-s /bin/bash -g users -m nm12345
usage: useradd [-u uid [-o]] [-g group] [-G group,...]
[-d home] [-s shell] [-c comment] [-m [-k template]]
[-f inactive] [-e expire ] [-p passwd] [-M] [-n] [-r] name
useradd -D [-g group] [-b base] [-s shell]
[-f inactive] [-e expire ]
sh: line 2: -s: command not found
failed
Any suggestions? Thank you.
Solved! Go to Solution.
- Tags:
- useradd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2010 07:48 AM
тАО11-09-2010 07:48 AM
Re: useradd in perl script
You need to quote the gecos field in the system call that calls userad.
currently, useradd sees test-account, sees the '-' and assumes another argument.
Additionally, more often than not, gecos fields have spaces in them, so you'll want to quote the entry for that as well:
$cmd1="/usr/sbin/useradd -u $line[1] -c \"$line[2]\" -s /bin/bash -g users -m $line[0]";
HTH;
Doug
------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2010 07:54 AM
тАО11-09-2010 07:54 AM
SolutionMy first post is wrong... well, it's right in that you want to quote the gecos field, but wrong in the reason for your issue.
The issue is that you're reading the line from your text file, including the new line. So, basically, your testid's gecos field isn't:
test1-account
it is, in fact:
test1-account\n
that's getting added to your system command such that:
$cmd =
useradd -u 12345 -c test1-account\n -s /bin/bash ... [snip]
that's easy to get rid of:
foreach $line (
chomp $line # removes the newline
@line=split(':', $line);
HTH;
Doug
------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2010 08:18 AM
тАО11-09-2010 08:18 AM
Re: useradd in perl script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2010 11:18 AM
тАО11-09-2010 11:18 AM
Re: useradd in perl script
> the line should be ended with ":" in the testfile, then the useradd command is one line and runs with no issue.
That's a kludge that causes an additional (albeit empty) field to be created. Doug was correct, you needed to 'chomp' the newline from each input line read. Consider the '/etc/passwd' file --- while colon delimited, there is no trailing delimiter following the login program field.
Regards!
...JRF...