RubyにはPHPのescapeshellcmd相当のメソッドはないんですか?
PHP がすぐ近くにあったので調べてみた。php-4.4.1/ext/standard/exec.c の php_escape_shell_cmd() で処理している。特殊文字に'\'をつければ良いみたい。
Ruby で作るとこうか?:
#!ruby require 'test/unit' def escape_shell_cmd cmd cmd.gsub(/(["'#&;`|*?~<>^()\[\]{}$\\\x0A\xFF])/) { "\\" + $1 } end class TestEscapeShellCmd < Test::Unit::TestCase def test_escape_semicolon cmd = 'date;date' assert_equal('date\;date', escape_shell_cmd(cmd), ';(semi-colon) must be escaped') end end
実は「'」については、こんな処理があったのだがよくわからないので飛ばした。
case '\'': #ifndef PHP_WIN32 if (!p && (p = memchr(str + x + 1, str[x], l - x - 1))) { /* noop */ } else if (p && *p == str[x]) { p = NULL; } else { cmd[y++] = '\\'; } cmd[y++] = str[x]; break; #endif
このコードの LICENSE は The PHP License, version 2.02、Author は Rasmus Lerdorf です。(これでいいのか?)