hi, i am trying to write a script to generate a big number of users in perl.. it is actually my second time writting perl .. i used to write shell scripts before. my little script seems not working because "useradd" isn't found by the compiler. if anyone may point out why it would be great. thanks.

#!/usr/bin/perl

$uName=$ARGV[0];
$uNumStart=$ARGV[1];
$uNumEnd=$ARGV[2];

for($i=$uNumStart; $i<=uNumEnd; $i++)
{
   useradd "$uName.$i" -g 999;
}

if useradd is an external application you probably want to use sytemt() to run it:

#!/usr/bin/perl

$uName=$ARGV[0];
$uNumStart=$ARGV[1];
$uNumEnd=$ARGV[2];

for($i=$uNumStart; $i<=uNumEnd; $i++)
{
   system(qq{useradd "$uName.$i" -g 999});
}

if useradd is an external application you probably want to use sytemt() to run it:

#!/usr/bin/perl

$uName=$ARGV[0];
$uNumStart=$ARGV[1];
$uNumEnd=$ARGV[2];

for($i=$uNumStart; $i<=uNumEnd; $i++)
{
   system(qq{useradd "$uName.$i" -g 999});
}

useradd is a command in fedora to add a user, so is it considered as an external command?

anyway i tried your suggestion it doesn't give me error but users weren't generated. it is the same as i tried

`useradd "$uName.$i" -g 999`;

I am not familiar with the useradd application, maybe you need to provide a path to the application instead of just using the application name.

Since the code-as-presented doesn't catch and test the return-value of the system() command, you won't know if system() fails to find useradd, which is probably what happened. It's in /usr/sbin on my old RedHat box ("whereis useradd").

In general, the more the app you're calling is like an admin tool, the more important it is to use an explicit path in calling it. It's not just so you can run it, it's so nobody can put malicious code by that name earlier in the search path and so get it run by mistake by your code.

Hi,
1. If you are not able find what is going on when running command from perl, it is better to run the SAME command, used in the code, in your shell by yourself and check. Eg: from above. useradd <value_of_$uName.$i> -g 999.
2. Knowing the return value of the external application after execution. It helps you know how actually execution of the command went. For eg:

`$command_string`;
if($?){print "command ends with error: ",$?;} #if command fails, then $? will have non-zero value, returned by the command.
This article has been dead for over six months. Start a new discussion instead.