Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我必须在用户提供输入的地方运行一些 shell 命令。我找到了一种看起来很安全的方法:system *%W(ls #{file}) [here]。
system *%W(ls #{file})
我需要获取该命令的输出,所以我不能只使用system. 有没有办法清理反引号``或 for的命令%x[]?
system
``
%x[]
你想要IO::popen而不是system. 您仍然可以传递一个字符串数组来调用命令,而无需使用 shell,并且可以read从生成的 IO 对象中调用。
IO::popen
read
如果您也想阅读 stderr,请使用open3模块而不是 IO。
open3
您正在运行哪些 Ruby 不支持的 shell 命令?如果要列出文件,请使用Dir