http://www.perlmonks.org?node_id=167616


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:
system("$useradd -c \"$fullname\" ...");
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.

However, when you call:

system( $useradd, qq|-c "fullname"|, ...
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 string
-c "value-of-$fullname"
which useradd will percieve as a single argument, quotes and all. This probably isn't what useradd is expecting.

If you're going to use the list form of system, you really have to pass each argument as a separate list element, eg:

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

If you have trouble understanding the difference, then try running each of the following:

system('cat -n /etc/group'); system('cat', '-n /etc/group'); system('cat', '-n', '/etc/group');
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.

Replies are listed 'Best First'.
Re^5: Using doublequotes in a system call
by particle (Vicar) on May 19, 2002 at 12:37 UTC
    you are, of course, correct. my 'in-head-interpreter' still doesn't parse perfectly, so my on-the-fly example was flawed.

    your clear and detailed response will now be the place i point anyone who wants to understand using system.

    ~Particle *accelerates*