I also need to be able to run commands with regular expressions, such as "rm *.c", but that fails to run within execve() (most cases), so I am attempting to work around that failure by passing the entire command set to "sh -c" instead. Any ideas?
If you want metacharacter expansion, you have to invoke the shell to do it (or write equivalent code to do it - which is possible and not too dreadfully hard to do with standard POSIX functions).
发布评论
评论(2)
您不应在
execve()
的参数内添加引号。应该这样调用:execve()
的第一个参数必须是可执行文件的路径(通常是完整路径)。You shouldn't put quotes inside arguments of
execve()
. It should be called like this:The first argument to
execve()
must be the path to the executable (usually a full path).shell 需要的符号是
sh -c "command plus argument"
。因此,您所做的“解决方法”实际上正是 shell 所要求的。另一种方法是使用
execvp()
运行ls
:如果您想要元字符扩展,则必须调用 shell 来执行此操作(或编写等效代码来执行此操作 - 这是可能的,并且对于标准 POSIX 函数来说并不难做到)。
要运行“
rm *.c
”,请使用:或者,如果您有一个需要专门传递的环境(我假设它是由
envp
指向的):Don'不要忘记
exec*
函数调用后的错误处理;它可能会失败。The notation the shell expects is
sh -c "command plus arguments"
. So, what you're doing as a 'workaround' is in fact what the shell demands.The alternative is to use
execvp()
to runls
:If you want metacharacter expansion, you have to invoke the shell to do it (or write equivalent code to do it - which is possible and not too dreadfully hard to do with standard POSIX functions).
To run '
rm *.c
', use:Or, if you have an environment you need to pass specifically (I assume it is pointed to by
envp
):Don't forget the error handling after the
exec*
function call; it can fail.