How to escape strings for terminal in Ruby?

*爱你&永不变心* 提交于 2019-12-18 12:09:33

问题


I am attempting to start mplayer. My filename contains spaces and these should be escaped. This is the code I am using:

@player_pid = fork do
   exec "/usr/bin/mplayer #{song.file}"
end

where #{song.file} contains a path like "/home/example/music/01 - a song.mp3". How can I escape this variable properly (and possible other weird characters that the title may contain) so the terminal will accept my command?


回答1:


Shellwords should work for you :)

exec "/usr/bin/mplayer %s" % Shellwords.escape(song.file)

In ruby 1.9.x, it looks like you have to require it first

require "shellwords"

But in ruby 2.0.x, I didn't have to explicitly require it.




回答2:


Please never use the "single command line" form of exec, that leaves you open to all the usual quoting and injection issues and pointlessly launches a shell. From the fine manual:

exec(cmdname, arg1, ...)

command name and one or more arguments (no shell)

So instead of mucking around with quoting and escaping and what not, just use the shell-less version:

exec '/usr/bin/mplayer', song.file

and bypass the shell completely. Similarly for system.



来源:https://stackoverflow.com/questions/17476389/how-to-escape-strings-for-terminal-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!