我正试图启动mplayer。我的文件名包含空格,这些空格应该被转义。
这是我正在使用的代码:
@player_pid = fork do
exec "/usr/bin/mplayer #{song.file}"
end
如果#{song.file}
包含类似"/home/example/music/01 - a song.mp3"
这样的路径,我该如何适当地转义这个变量(以及标题可能包含的其他奇怪字符),以便终端接受我的命令?
我正试图启动mplayer。我的文件名包含空格,这些空格应该被转义。
这是我正在使用的代码:
@player_pid = fork do
exec "/usr/bin/mplayer #{song.file}"
end
如果#{song.file}
包含类似"/home/example/music/01 - a song.mp3"
这样的路径,我该如何适当地转义这个变量(以及标题可能包含的其他奇怪字符),以便终端接受我的命令?
Shellwords 应该适合您 :)
exec "/usr/bin/mplayer %s" % Shellwords.escape(song.file)
在 Ruby 1.9.x 中,看起来你必须先require
它。require "shellwords"
但在 Ruby 2.0.x 中,我不必显式地要求它。
请永远不要使用 "单行命令" 形式的 exec
,那会让你面临所有通常的引号和注入问题,并且毫无意义地启动一个 shell。从fine manual中可以看到:
exec(cmdname, arg1, ...)
命令名称和一个或多个参数(没有 shell)
因此,与其折腾引号和转义等问题,不如直接使用无 shell 版本:
exec '/usr/bin/mplayer', song.file
并且完全绕过shell。同样适用于system
。
/usr/bin/mplayer
路径,因此您可能需要引用一种机制来定位可执行文件。当然,如果这个程序只是供您自己使用,那就没问题了。 - vgoff$PATH
中运行二进制文件的标准方式。 - chutz