shellwords.rb ã« shellescape() ç‰ã‚’è¿½åŠ ã—よã†ã¨æ€ã„ã¾ã™ã€‚
 ç†ç”±ã¨ã—ã¦ã¯ã€ system(), open(“|…”), IO.popen() ç‰ã§ã‚³ãƒžãƒ³ãƒ‰
ラインを記述ã™ã‚‹éš›ã€ãƒ¡ã‚¿æ–‡å—ç‰ã‚’エスケープã™ã‚‹é©å½“ãªé–¢æ•°ãŒæ¨™æº–ã§
æä¾›ã•ã‚Œã¦ã„ãªã„ãŸã‚ã§ã™ã€‚
 仕様ã¨ã—ã¦ã¯ã€Bourneシェルã«æ¸¡ã™ã“ã¨ã‚’想定ã—ã€å®‰å…¨ã§ãªã„æ–‡å—ã‚’
ãƒãƒƒã‚¯ã‚¹ãƒ©ãƒƒã‚·ãƒ¥()ã§ã‚¨ã‚¹ã‚±ãƒ¼ãƒ—ã—ã¾ã™ã€‚æ–‡å—列ã¯ã‚·ãƒ³ã‚°ãƒ«ãƒã‚¤ãƒˆåˆ—
ã¨ã—ã¦æ‰±ã„ã¾ã™ã€‚ç†ç”±ã¯ä»¥ä¸‹ã®é€šã‚Šã§ã™ã€‚
-
shellwords ã¯ãã‚‚ãã‚‚Bourneシェル(POSIX)ã®æ–‡æ³•ã«å‰‡ã£ãŸå‡¦ç†ã‚’è¡Œã†
関数をæä¾›ã™ã‚‹ãƒ©ã‚¤ãƒ–ラリã§ã‚る。 -
(Plan9ã®) rc ç‰ã‚’å«ã‚多ãã®ã‚·ã‚§ãƒ«ã§ä½¿ãˆã‚‹ã®ãŒæœ›ã¾ã—ã„ã¨ã„ã†å£°ã‚‚
[ruby-list:30049] ç‰ã§å‡ºãŸã€‚ã—ã‹ã—ã€Bourneシェル㨠rc ã§ã¯å¼•ç”¨ç¬¦
(')ã®ã‚¨ã‚¹ã‚±ãƒ¼ãƒ—ã®ä»•æ–¹ã«äº’æ›æ€§ãŒãªã〠Windows ã® cmd.exe も変数
展開や引用符ã®æ‰±ã„ã®å·®ç•°ãŒå¤§ããã€ã²ã¨ã¤ã§å…¨å¯¾å¿œã™ã‚‹ã‚¨ã‚¹ã‚±ãƒ¼ãƒ—
方法ãŒãªã„。ãŸã ã—〠(t)csh ã¯æ”¹è¡Œ(LF)ã®ã‚¨ã‚¹ã‚±ãƒ¼ãƒ—ãŒåŠ¹ã‹ãªã„(ãã‚‚ãã‚‚ã§ãã‚‹
ã®ã‹ä¸æ˜Ž)点を除ã‘ã°äº’æ›æ€§ãŒã‚る。 -
引用符(‘)ã§æ‹¬ã‚‹æ–¹å¼ã ã¨ã€æ„Ÿå˜†ç¬¦(!)ã®æ‰±ã„ã®å·®ç•°ã®ãŸã‚ (t)csh ã§
使ãˆãªããªã£ã¦ã—ã¾ã†ã€‚ã¾ãŸã€ãƒžãƒ«ãƒãƒã‚¤ãƒˆæ–‡å—ã®ä¸ã«å¼•ç”¨ç¬¦(’)ãŒ
ã‚ã£ãŸå ´åˆã€ãƒžãƒ«ãƒãƒã‚¤ãƒˆå¯¾å¿œã®ã‚·ã‚§ãƒ«ã§ ‘…’'‘…’ ãŒèª¤è§£é‡ˆ
ã•ã‚Œã‚‹å¯èƒ½æ€§ãŒé«˜ã„。 -
二é‡å¼•ç”¨ç¬¦(")ã§æ‹¬ã‚‹æ–¹å¼ã ã¨ã€ãƒãƒƒã‚¯ã‚¹ãƒ©ãƒƒã‚·ãƒ¥()ã®æ‰±ã„ã®å·®ç•°ã®
ãŸã‚ (t)csh ã§ä½¿ãˆãªããªã£ã¦ã—ã¾ã†ã€‚
 パッãƒã‚’添付ã—ã¾ã—ãŸã€‚以下ãŒã‚µãƒžãƒªãƒ¼ã§ã™ã€‚
-
Shellwords.shellescape ã‚’è¿½åŠ :
Bourneシェルã«è§£é‡ˆã•ã‚Œã‚‹å¯èƒ½æ€§ã®ã‚ã‚‹æ–‡å—をエスケープã—ã¦è¿”ã™
(仕様ã¯å®Ÿè£…ãŠã‚ˆã³ã‚³ãƒ¡ãƒ³ãƒˆã‚’å‚ç…§ã®ã“ã¨) -
Shellwords.shelljoin ã‚’è¿½åŠ :
与ãˆã‚‰ã‚ŒãŸå¼•æ•°åˆ—ã‹ã‚‰ã€Bourneシェルコマンドライン文å—列を生æˆ
ã—ã¦è¿”ã™ (shellescape を利用) -
Shellwords.shellwords ã®åˆ¥å shellsplit を定義
-
利用ã®ä¾¿ã®ãŸã‚ String ã‚„ Array ã«ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ãƒ¡ã‚½ãƒƒãƒ‰ã‚’追åŠ
 cmd.exe ã‚„ rc ã®å¯¾å¿œã«ã¤ã„ã¦ã¯ã€å°†æ¥çš„ã«ã‚ªãƒ—ã‚·ãƒ§ãƒ³å¼•æ•°ã‚’è¿½åŠ ã—ã€
プラットフォームã”ã¨ã«å¦¥å½“ãªãƒ‡ãƒ•ã‚©ãƒ«ãƒˆå€¤ã‚’è¨å®šã™ã‚‹ã“ã¨ã§å¯¾å¿œã§ã
ã‚Œã°ã¨æ€ã„ã¾ã™ã€‚
–
/
/__ __ Akinori.org / MUSHA.org
/ ) ) ) ) / FreeBSD.org / Ruby-lang.org
Akinori MUSHA aka / (_ / ( (__( @ iDaemons.org / and.or.jp
“Different eyes see different things,
Different hearts beat on different strings –
But there are times for you and me when all such things agree”
Index: shellwords.rb
— shellwords.rb (revision 13420)
+++ shellwords.rb (working copy)
@@ -1,29 +1,31 @@
-# shellwords.rb: Split text into an array of tokens a la UNIX shell
+# shellwords.rb: Manipulates strings a la UNIX Bourne shell
-# This module is originally a port of shellwords.pl, but modified to
-# conform to POSIX / SUSv3 (IEEE Std 1003.1-2001).
+# This module manipulates strings according to the word parsing rules
+# of the UNIX Bourne shell.
-# Examples:
+# The shellwords() function was originally a port of shellwords.pl,
+# but modified to conform to POSIX / SUSv3 (IEEE Std 1003.1-2001).
-# require ‘shellwords’
-# words = Shellwords.shellwords(line)
-#
-# or
-#
-# require ‘shellwords’
-# include Shellwords
-# words = shellwords(line)
+# Authors:
+# - Wakou Aoyama
+# - Akinori MUSHA [email protected]
module Shellwords
-
Splits a string into an array of tokens in the same way the UNIX
-
Bourne shell does.
-
-
argv = Shellwords.shellwords(‘here are “two words”’)
-
argv #=> [“here”, “are”, “two words”]
-
+String#shellwords+ is a shorthand for this function.
-
-
argv = ‘here are “two words”’.shellwords
-
argv #=> [“here”, “are”, “two words”]
def shellwords(line)
words = []
@@ -39,6 +41,98 @@
end
words
end - alias shellsplit shellwords
-
-
Escapes a string so that it can be safely used in a Bourne shell
-
command line.
-
-
Note that a resulted string should be used unquoted and is not
-
intended for use in double quotes nor in single quotes.
-
-
open(“| grep #{Shellwords.shellescape(pattern)} file”) { |pipe|
-
# …
-
}
-
-
+String#shellescape+ is a shorthand for this function.
-
-
open(“| grep #{pattern.shellescape} file”) { |pipe|
-
# …
-
}
-
- def shellescape(str)
-
An empty argument will be skipped, so return empty quotes.
- return “‘’” if str.empty?
- str = str.dup
-
Process as a single byte sequence because not all shell
-
implementations are multibyte aware.
- str.gsub!(/([^A-Za-z0-9_-.,:/@\n])/n, “\\\1”)
-
A LF cannot be escaped with a backslash because a backslash + LF
-
combo is regarded as line continuation and simply ignored.
- str.gsub!(/\n/, “‘\n’”)
- return str
- end
-
-
Builds a command line string from an argument list +array+ joining
-
all elements escaped for Bourne shell and separated by a space.
-
-
open(‘|’ + Shellwords.shelljoin([‘grep’, pattern, *files])) {
|pipe|
-
# …
-
}
-
-
+Array#shelljoin+ is a shorthand for this function.
-
-
open(‘|’ + [‘grep’, pattern, *files].shelljoin) { |pipe|
-
# …
-
}
-
- def shelljoin(array)
- array.map { |arg| shellescape(arg) }.join(’ ')
- end
- module_function :shellwords, :shellsplit, :shellescape, :shelljoin
+end
+class String
-
-
call-seq:
-
str.shellwords => array
-
-
Splits +str+ into an array of tokens in the same way the UNIX
-
Bourne shell does. See +Shellwords::shellwords+ for details.
-
- def shellwords
- Shellwords.shellwords(self)
- end
- alias shellsplit shellwords
- module_function :shellwords
-
-
call-seq:
-
str.shellescape => string
-
-
Escapes +str+ so that it can be safely used in a Bourne shell
-
command line. See +Shellwords::shellescape+ for details.
-
- def shellescape
- Shellwords.shellescape(self)
- end
+end
+class Array