Operating System - HP-UX
1753741 Members
4097 Online
108799 Solutions
New Discussion юеВ

Re: useradd in perl script

 
SOLVED
Go to solution
John Guster
Trusted Contributor

useradd in perl script

why the command is broke into second line starting at -s option? here is the script:
#!/usr/bin/perl
open(fh, "foreach $line () {
@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.
4 REPLIES 4
Doug O'Leary
Honored Contributor

Re: useradd in perl script

Hey;

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
Doug O'Leary
Honored Contributor
Solution

Re: useradd in perl script

ah, there it is.

My 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
John Guster
Trusted Contributor

Re: useradd in perl script

thanks. the line should be ended with ":" in the testfile, then the useradd command is one line and runs with no issue.
James R. Ferguson
Acclaimed Contributor

Re: useradd in perl script

Hi John:

> 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...