Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running commands in strings fails #623

Closed
miversen33 opened this issue Nov 13, 2022 · 2 comments
Closed

Running commands in strings fails #623

miversen33 opened this issue Nov 13, 2022 · 2 comments

Comments

@miversen33
Copy link

Hello! I have found an interesting issue when running a command with uv.spawn

Below is an example lua script that will fail, though if you run the command in a linux shell, the command is valid.

-- test.lua
local uv = require("luv")

local handle = nil
local on_exit = function(code, signal)
    print(string.format("Exit Code: %s", code))
    print(string.format("Exit Signal: %s", signal))
    handle:close()
end

local stdout = uv.new_pipe(false)
local stderr = uv.new_pipe(false)
-- This command `/bin/sh -c 'touch /tmp/testing.txt'` is a valid linux command, but this script fails out
local command = "/bin/sh"
local args = {"-c", "'touch /tmp/testing.txt'"}
local opts = {
    args = args,
    stdio = {nil, stdout, stderr}
}

handle, _ = uv.spawn(command, opts, on_exit)

stdout:read_start(function(err, data)
    assert(not err, err)
    print(string.format("%s", data))
end)

stderr:read_start(function(err, data)
    assert(not err, err)
    print(string.format("Error: %s", data))
end)

uv.run()

The above will drop the following output

$ lua test.lua 
Error: /bin/sh: line 1: touch /tmp/testing.txt: No such file or directory

Error: nil
nil
Exit Code: 127
Exit Signal: 0

However, this is not the case when you run the command via a shell

$ /bin/sh -c 'touch /tmp/testing.txt'
$ echo $?
0

I assume I am doing something wrong but I dont really know. I have tried breaking the string arg up by space but I get the same error. Any ideas?

@squeek502
Copy link
Member

Have you tried

local args = {"-c", "touch /tmp/testing.txt"}

(i.e. without the single quote chars)

AFAIK quote parsing is a feature of the shell and so quotes should not be included in the args table as you're able to provide each arg separately without the need for quotes.

@miversen33
Copy link
Author

Im so very annoyed that that worked lol. I appreciate you! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants