I'm quite confused reading the doc of Ruby's system
method here. I'm not sure what are commands and what are options. What do I do if I want to execute the following?
wget -pk -nd -P /public/google www.google.com
For security reasons, I'd like to use one of the versions that uses no shell (the second and third forms in the URL I gave, rather than the first)
Consider the examples:
system("echo *")
system("echo", "*")
The first one passes the string 'echo *'
to the shell to be parsed and executed; that's why system('echo *')
produces the same output as saying echo *
from the shell prompt: you get a list of files in the current directory. The corresponding argument form is:
commandline : command line string which is passed to the standard shell
The second one bypasses the shell entirely. It will look for echo
in the PATH
and then execute it with the string '*'
as its argument. Since the shell expands wildcards (at least on unixy systems), the *
will stay as a simple *
and you'll see *
as the output. The corresponding argument form here is:
cmdname, arg1, ... : command name and one or more arguments (no shell)
The third form:
[cmdname, argv0], arg1, ... : command name, argv[0] and zero or more arguments (no shell)
is used when you want to execute cmdname
but have it show up with a different name in ps
listings and such. You can see this in action by opening two terminals. Open up irb
in one of them and say:
system('sleep', '10')
then quickly switch to the other and look at the ps
listing. You should see sleep 10
in there. But, if you give this to irb
:
system(['sleep', 'pancakes'], '10')
and check the ps
listing, you'll see pancakes 10
. Similar two-terminal tricks will show you a shell -c sleep 10
if you say system('sleep 10')
.
If you supply a Hash as the first argument, then that Hash is used as the environment variables for the spawned process. If you supply a Hash as the final argument, then that Hash is used as options; further documentation on the arguments is, as noted in the system
documentation, available under Kernel#spawn
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With