in reply to Re^3: Using doublequotes in a system call
in thread Using doublequotes in a system call
This is the right idea, but your system() call isn't quite correct. When you run something like:
then system() runs the command by way of the a shell. The shell splits the command string up into words--removing the quotes in the process--and ends up passing the '-c' and the $fullname to useradd as two separate arguments.system("$useradd -c \"$fullname\" ...");
However, when you call:
then the shell doesn't get involved, and useradd receives the exact argument list you passed to system(). In this case you've constructed a single stringsystem( $useradd, qq|-c "fullname"|, ...
which useradd will percieve as a single argument, quotes and all. This probably isn't what useradd is expecting.-c "value-of-$fullname"
If you're going to use the list form of system, you really have to pass each argument as a separate list element, eg:
This way, the shell isn't involved, because you're using the list form of system(). But useradd receives each command-line argument as a separate element (with no extraneous quotes) just like it expects.system ($useradd, '-c', $fullname, '-d', "/home/sites/site$site_count/users/$username", '-g', "site$site_count", '-G', "site-adm$site_count", '-p', $password, '-s', '/bin/false', '-u', $uid, $username);
If you have trouble understanding the difference, then try running each of the following:
Use the q{} or qq{} quote form if you like; it shouldn't matter. The first and third lines should work; the second should give you an error of some sort.system('cat -n /etc/group'); system('cat', '-n /etc/group'); system('cat', '-n', '/etc/group');
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: Using doublequotes in a system call
by particle (Vicar) on May 19, 2002 at 12:37 UTC |
In Section
Seekers of Perl Wisdom