Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: Re: Re: Using doublequotes in a system call

by kjherron (Pilgrim)
on May 19, 2002 at 05:39 UTC ( #167616=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Re: Re: Re: Using doublequotes in a system call
Select or Download Code
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*

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://167616]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-07-29 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (214 votes), past polls