{"id":2063,"date":"2013-02-15T09:16:59","date_gmt":"2013-02-15T00:16:59","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=2063"},"modified":"2018-11-14T16:33:56","modified_gmt":"2018-11-14T07:33:56","slug":"bash%e3%81%a7cgi-2-cgi%e3%81%ae%e3%81%8a%e3%81%be%e3%81%98%e3%81%aa%e3%81%84","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/02\/15\/bash%e3%81%a7cgi-2-cgi%e3%81%ae%e3%81%8a%e3%81%be%e3%81%98%e3%81%aa%e3%81%84\/","title":{"rendered":"bash\u3067CGI (2) CGI\u306e\u304a\u307e\u3058\u306a\u3044"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002bash\u3067CGI\u306e\uff12\u56de\u76ee\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u300c\u304a\u307e\u3058\u306a\u3044\u300d\u306e\u90e8\u5206\u306b\u3064\u3044\u3066\u3067\u3059\u3002<\/p>\n<h2>\u304a\u307e\u3058\u306a\u3044\u3068\u306f<\/h2>\n<p>\u50d5\u304cCGI\u3092\u59cb\u3081\u305f\u306e\u306f2000\u5e74\u9593\u8fd1\u306e\u3053\u308d\u3067\u3001Y2K\u554f\u984c\u3068\u304b\u8a00\u308f\u308c\u3066\u3044\u305f\u3053\u308d\u3067\u3059\u3002\u5f53\u6642\u306fCGI\u306e\u6700\u521d\u306e\u6570\u884c\u3092\u300c\u304a\u307e\u3058\u306a\u3044\u300d\u3068\u3057\u3066\u6559\u308f\u3063\u3066\u3044\u307e\u3057\u305f\u3002\u300c\u7406\u7531\u306f\u3068\u3082\u304b\u304f\u3053\u3046\u66f8\u304b\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3093\u3060\u3088\u300d\u3068\u3044\u3046\u611f\u3058\u3067\u3057\u305f\u3002<\/p>\n<pre class=\"lang:sh decode:true\" title=\"helloworld.cgi\">#!\/bin\/bash\r\n# helloworld.cgi\r\n\r\n# \u304a\u307e\u3058\u306a\u3044\r\necho \"Content-type:text\/plain\"\r\necho\r\n\r\n# \u5185\u5bb9\r\necho \"Hello World\"<\/pre>\n<p>\u524d\u56de\u306fecho\u306e\u90e8\u5206\u304b\u3089\u3092\u300c\u304a\u307e\u3058\u306a\u3044\u300d\u3068\u8868\u8a18\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u306e\u8fba\u308a\u306f\u8af8\u8aac\u3042\u308b\u3088\u3046\u3067\u3059\u306e\u3067\u3001\u30d5\u30a1\u30a4\u30eb\u306e1\u884c\u76ee\u304b\u3089\u89e3\u8aac\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>1\u884c\u76ee\u306b\u3064\u3044\u3066<\/h2>\n<p><code>#!\/bin\/bash<\/code><\/p>\n<p>\u306e\u90e8\u5206\u3067\u3059\u304c\u3001\u3053\u308c\u306f<strong>shebang<\/strong>\u3067\u3059\u306d\u3002Unix\/Linux\u4e0a\u3067\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3055\u305b\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u8a18\u8ff0\u3067\u3059\u3002<\/p>\n<p>\u7c21\u5358\u306b\u8a00\u3048\u3070\u3001\u5148\u982d2\u30d0\u30a4\u30c8\u304c#!\u3060\u3063\u305f\u5834\u5408\u306b\u3001\u6307\u5b9a\u3055\u308c\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u30d5\u30a1\u30a4\u30eb\u540d\u304c\u5f15\u6570\u3068\u3057\u3066\u6e21\u3055\u308c\u3001\u5b9f\u884c\u3055\u308c\u308b\u3068\u3044\u3046\u30eb\u30fc\u30eb\u3067\u3059\u3002<\/p>\n<p>\u3067\u3059\u306e\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306asample.cgi\u304c\u3042\u3063\u305f\u5834\u5408\u3001<\/p>\n<pre class=\"lang:sh decode:true\" title=\"sample.cgi\">#!\/bin\/bash\r\n# sample.cgi<\/pre>\n<p>\u4ee5\u4e0b\u306e2\u3064\u306e\u5b9f\u884c\u65b9\u6cd5\u306f\u540c\u3058\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \" >$ .\/sample.cgi\r\n$ \/bin\/bash sample.cgi<\/pre>\n<p>\u6307\u5b9a\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001bash\u3084perl\u306a\u3069\u3001\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3055\u305b\u308b\u305f\u3081\u306e\u30b3\u30de\u30f3\u30c9\u3067\u306a\u304f\u3066\u3082\u304b\u307e\u3044\u307e\u305b\u3093\u3002<br \/>\n\u8a66\u3057\u306b\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u4f5c\u3063\u3066\u5b9f\u884c\u3057\u3066\u307f\u308b\u3068\u3001<\/p>\n<p>sample.tail<\/p>\n<pre class=\"lang:sh decode:true \" >#!\/bin\/tail -3\r\ntail\u30673\u884c\u8868\u793a\u3059\u308b\r\n1\r\n2\r\n3\r\n4\r\n5\r\n<\/pre>\n<pre class=\"lang:sh decode:true \" >$ .\/sample.tail\r\n3\r\n4\r\n5<\/pre>\n<p>\u3068\u306a\u308a\u307e\u3059\u3002\u6700\u5f8c\u306e3\u884c\u304c\u8868\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u306d\u3002<\/p>\n<p>shebang\u306b\u3064\u3044\u3066\u306f\u3001\u4ee5\u4e0b\u306e\u30da\u30fc\u30b8\u306b\u8a73\u3057\u304f\u8a18\u8f09\u3055\u308c\u3066\u3044\u307e\u3059\u3002<br \/>\n<a href=\"http:\/\/x68000.q-e-d.net\/~68user\/unix\/pickup?keyword=shebang&target=command\">UNIX\u306e\u90e8\u5c4b \u30b3\u30de\u30f3\u30c9\u691c\u7d22:shebang\u3000(*BSD\/Linux)<\/a><\/p>\n<h2>echo\u306e\u51fa\u529b\u5185\u5bb9\u306b\u3064\u3044\u3066<\/h2>\n<pre class=\"lang:sh decode:true\" title=\"helloworld.cgi\"># \u304a\u307e\u3058\u306a\u3044\r\necho \"Content-type:text\/plain\"\r\necho<\/pre>\n<p>\u3053\u306eecho\u306e\u90e8\u5206\u3067\u3059\u304c\u3001\u3053\u308c\u304c\u7121\u304b\u3063\u305f\u3089\u3069\u3046\u306a\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p>\u8a66\u3057\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001Internal Server Error\u304c\u8d77\u3053\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \" >#!\/bin\/bash\r\n# omajinai-nai.cgi\r\n\r\necho \"No omajinai!!!\"<\/pre>\n<pre class=\"lang:sh decode:true \" >$ curl -s http:\/\/localhost\/bash-cgi\/omajinai-nai.cgi\r\n&lt;!DOCTYPE HTML PUBLIC \"-\/\/IETF\/\/DTD HTML 2.0\/\/EN\"&gt;\r\n&lt;html&gt;&lt;head&gt;\r\n&lt;title&gt;500 Internal Server Error&lt;\/title&gt;\r\n&lt;\/head&gt;&lt;body&gt;\r\n&lt;h1&gt;Internal Server Error&lt;\/h1&gt;\r\n&lt;p&gt;The server encountered an internal error or\r\nmisconfiguration and was unable to complete\r\nyour request.&lt;\/p&gt;\r\n&lt;p&gt;Please contact the server administrator,\r\n root@localhost and inform them of the time the error occurred,\r\nand anything you might have done that may have\r\ncaused the error.&lt;\/p&gt;\r\n&lt;p&gt;More information about this error may be available\r\nin the server error log.&lt;\/p&gt;\r\n&lt;hr&gt;\r\n&lt;address&gt;Apache\/2.2.15 (CentOS) Server at localhost Port 80&lt;\/address&gt;\r\n&lt;\/body&gt;&lt;\/html&gt;<\/pre>\n<p>Apache\u306e\u30a8\u30e9\u30fc\u30ed\u30b0\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \" >[Fri Feb 08 12:58:10 2013] [error] [client 127.0.0.1] malformed header from script. Bad header=No omajinai!!!: omajinai-nai.cgi<\/pre>\n<p>\u4e0d\u6b63\u306a\u30d8\u30c3\u30c0\u30fc\u3060\u3088\uff01\u3068\u6012\u3089\u308c\u3066\u3044\u307e\u3059\u3002echo\u3057\u3066\u3044\u308b\u300cNo omajinai!!!\u300d\u306b\u5bfe\u3057\u3066\u3001Bad header\u3060\u3068\u3082\u8a00\u308f\u308c\u3066\u3044\u307e\u3059\u306d\u3002<\/p>\n<p>\u30d7\u30ed\u30b0\u30e9\u30e0\u3068\u3057\u3066\u5b9f\u884c\u306f\u3067\u304d\u3066\u3044\u308b\u3082\u306e\u306e\u3001\u305d\u306e\u51fa\u529b\u5185\u5bb9\u306b\u554f\u984c\u304c\u3042\u308b\u3068\u3044\u3046\u72b6\u614b\u3067\u3059\u3002<\/p>\n<h2>CGI\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30eb\u30fc\u30eb<\/h2>\n<p>\u3053\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u306b\u3064\u3044\u3066\u3082\u3001RFC\u3067\u5b9a\u3081\u3089\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre>6.2.  Response Types\r\n\r\n   The response comprises a message-header and a message-body, separated\r\n   by a blank line.  The message-header contains one or more header\r\n   fields.  The body may be NULL.<\/pre>\n<p>\u30ec\u30b9\u30dd\u30f3\u30b9\u306f\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u30d8\u30c3\u30c0\u30fc\u3068\u30e1\u30c3\u30bb\u30fc\u30b8\u30dc\u30c7\u30a3\u3067\u69cb\u6210\u3055\u308c\u3066\u3001\u7a7a\u884c(blank line)\u3067\u533a\u5207\u308b\u3002<br \/>\n\u30e1\u30c3\u30bb\u30fc\u30b8\u30d8\u30c3\u30c0\u30fc\u306f1\u3064\u4ee5\u4e0a\u306e\u30d8\u30c3\u30c0\u30fc\u30d5\u30a3\u30fc\u30eb\u30c9\u304b\u3089\u306a\u308a\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u30dc\u30c7\u30a3\u306fNULL\u3067\u3082\u826f\u3044\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001echo\u6587\u306e2\u884c\u306f\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u30d8\u30c3\u30c0\u30fc\u3068\u7a7a\u884c\u3092\u51fa\u529b\u3057\u3066\u3044\u308b\u90e8\u5206\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u306d\u3002<br \/>\n\u5f53\u7136\u3001\u30d8\u30c3\u30c0\u30fc\u306e\u5185\u5bb9\u306b\u3082\u610f\u5473\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u5f8c\u306e\u56de\u3067\u8aac\u660e\u3057\u3066\u3044\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\" title=\"helloworld.cgi\">#!\/bin\/bash\r\n# helloworld.cgi\r\n\r\n# CGI\u306e\u30d8\u30c3\u30c0\u30fc\u90e8\u5206\r\necho \"Content-type:text\/plain\" \r\n# \u30d8\u30c3\u30c0\u30fc\u3068\u30dc\u30c7\u30a3\u3092\u5206\u3051\u308b\u7a7a\u884c\r\necho \r\n\r\n# \u30dc\u30c7\u30a3\r\necho \"Hello World\"<\/pre>\n<p>\u300c\u304a\u307e\u3058\u306a\u3044\u300d\u306e\u610f\u5473\u304c\u3088\u3046\u3084\u304f\u7406\u89e3\u3067\u304d\u307e\u3057\u305f\uff01<\/p>\n<h2>\u6539\u884c\u30b3\u30fc\u30c9\u306b\u3064\u3044\u3066<\/h2>\n<p>\u307e\u3060\u307e\u3060\u6c17\u306b\u306a\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u30d8\u30c3\u30c0\u30fc\u3068\u30dc\u30c7\u30a3\u3092\u533a\u5207\u308b\u305f\u3081\u306b\u7a7a\u884c\u304c\u5fc5\u8981\u3068\u8a00\u3063\u3066\u3082\u3001\u6539\u884c\u30b3\u30fc\u30c9\u306f\u3069\u3046\u3057\u305f\u3089\u826f\u3044\u3093\u3067\u3057\u3087\u3046\u3002OS\u306b\u4f9d\u5b58\u3059\u308b\u3088\u3046\u306a\u6c17\u304c\u3057\u307e\u3059\u304c\u3001\u3060\u3068\u3057\u305f\u3089Linux\u3060\u3068LF\u3067\u3001Windows\u3060\u3068CRLF\u3067\u826f\u3044\u306e\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p>RFC\u3092\u53c2\u7167\u3057\u307e\u3059\u3068\u3001<\/p>\n<pre>6.3.4.  Protocol-Specific Header Fields\r\n\r\n   The script MAY return any other header fields that relate to the\r\n   response message defined by the specification for the SERVER_PROTOCOL\r\n   (HTTP\/1.0 [1] or HTTP\/1.1 [4]).  The server MUST translate the header\r\n   data from the CGI header syntax to the HTTP header syntax if these\r\n   differ.  For example, the character sequence for newline (such as\r\n   UNIX's US-ASCII LF) used by CGI scripts may not be the same as that\r\n   used by HTTP (US-ASCII CR followed by LF).<\/pre>\n<p>\u7a7a\u884c\u306f\u3001LF\u304b\u3082\u3057\u308c\u306a\u3044\u3057\u3001CRLF\u304b\u3082\u3057\u3093\u306a\u3044\u3088\uff01\u3068\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u306d\u3002\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>LF<\/p>\n<pre class=\"lang:sh decode:true \" >#!\/bin\/bash\r\n# lf.cgi\r\necho -ne \"Content-Type:text\/plain\\n\\n\"\r\n\r\necho \"LF\"<\/pre>\n<pre class=\"lang:sh decode:true \" >$ curl -s http:\/\/localhost\/bash-cgi\/lf.cgi\r\nLF<\/pre>\n<p>CRLF<\/p>\n<pre class=\"lang:sh decode:true \" >#!\/bin\/bash\r\n# crlf.cgi\r\necho -ne \"Content-Type:text\/plain\\r\\n\\r\\n\"\r\n\r\necho \"CRLF\"<\/pre>\n<pre class=\"lang:sh decode:true \" >curl -s http:\/\/localhost\/bash-cgi\/crlf.cgi\r\nCRLF<\/pre>\n<p>\u3061\u3083\u3093\u3068\u4e21\u65b9\u5b9f\u884c\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u30d8\u30c3\u30c0\u30fc\u3068\u30dc\u30c7\u30a3\u306e\u533a\u5207\u308a\u306f\u3001LF\u3067\u3082CRLF\u3067\u3082\u5927\u4e08\u592b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u306d\u3002\u6539\u884c\u30b3\u30fc\u30c9\u306fLF\u3058\u3083\u306a\u3044\u3068\u30c0\u30e1\u3060\u3068\u601d\u3063\u3066\u3044\u307e\u3057\u305f\u2026\u3002<\/p>\n<p>\u3061\u306a\u307f\u306bCR\u306e\u307f\u3060\u3068\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \" >#!\/bin\/bash\r\n# cr.cgi\r\necho -ne \"Content-Type:text\/plain\\r\\r\"\r\n\r\necho \"CR\"<\/pre>\n<pre class=\"lang:sh decode:true \" >curl -s http:\/\/localhost\/bash-cgi\/cr.cgi\r\n<!DOCTYPE HTML PUBLIC \"-\/\/IETF\/\/DTD HTML 2.0\/\/EN\">\r\n<html><head>\r\n<title>500 Internal Server Error<\/title>\r\n(\u7701\u7565\u3057\u307e\u3059)<\/pre>\n<h3>\u6ce8\u610f\u70b9<\/h3>\n<p>\u6ce8\u610f\u3057\u3066\u3082\u3089\u3044\u305f\u3044\u306e\u306f\u3001\u3042\u304f\u307e\u3067\u3082\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u6539\u884c\u30b3\u30fc\u30c9\u3067\u3059\u3002CGI\u306e\u30d5\u30a1\u30a4\u30eb\u305d\u306e\u3082\u306e\u306b\u4f7f\u7528\u3057\u3066\u3044\u308b\u6539\u884c\u30b3\u30fc\u30c9\u306fLF\u3067\u306a\u3044\u3068\u3001bash\u3067\u306e\u5b9f\u884c\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u9023\u8f09\u8a18\u4e8b\u306b\u304a\u3044\u3066\u306f\u3001\u4ee5\u964d\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30d5\u30a1\u30a4\u30eb\u306e\u6539\u884c\u30b3\u30fc\u30c9\u306fLF<\/li>\n<li>\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30d8\u30c3\u30c0\u30fc\u3068\u30dc\u30c7\u30a3\u306e\u533a\u5207\u308a\u306b\u306fCRLF<\/li>\n<\/ul>\n<p>\u6b21\u56de\u306f\u30d8\u30c3\u30c0\u30fc\u306b\u3064\u3044\u3066\u6398\u308a\u4e0b\u3052\u3066\u3044\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002bash\u3067CGI\u306e\uff12\u56de\u76ee\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u300c\u304a\u307e\u3058\u306a\u3044\u300d\u306e\u90e8\u5206\u306b\u3064\u3044\u3066\u3067\u3059\u3002\u3061\u3087\u3063\u3068\u9577\u304f\u306a\u308a\u307e\u3057\u305f\u2026\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2013\/02\/15\/bash%e3%81%a7cgi-2-cgi%e3%81%ae%e3%81%8a%e3%81%be%e3%81%98%e3%81%aa%e3%81%84\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[86,18],"tags":[87,88],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2063"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=2063"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2063\/revisions"}],"predecessor-version":[{"id":17954,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2063\/revisions\/17954"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=2063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=2063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=2063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}