{"id":2446,"date":"2015-11-07T15:10:34","date_gmt":"2015-11-07T07:10:34","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2446"},"modified":"2015-11-07T15:10:34","modified_gmt":"2015-11-07T07:10:34","slug":"bash%e4%b8%adsourceforkexec%e7%9a%84%e7%9b%b8%e5%85%b3%e7%9f%a5%e8%af%86","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2446.html","title":{"rendered":"Bash\u4e2dsource\/fork\/exec\u7684\u76f8\u5173\u77e5\u8bc6"},"content":{"rendered":"<p>\u6211\u8bb0\u5f97\u5728\u6211\u7b2c\u4e00\u6b21\u7ffb\u770b\u300aUnix\u73af\u5883\u9ad8\u7ea7\u7f16\u7a0b\u300b\u7684\u65f6\u5019\uff0c\u91cc\u9762\u6709\u8bb2\u8fc7fork\u548cexec\u7684\u77e5\u8bc6\uff08APUE\u8fd9\u672c\u8eab\u4e66\u5f88\u591a\u77e5\u8bc6\u5e94\u8be5\u90fd\u8bb2\u4e86\uff0c\u4f46\u662f\u4ee5\u6211\u73b0\u5728\u7684\u6c34\u5e73\u8fd8\u7406\u89e3\/\u4f53\u4f1a\u4e0d\u5230\u7f62\u4e86\uff09\uff0c\u4f46\u90a3\u65f6\u5019\u5565\u4e5f\u4e0d\u61c2\uff0c\u770b\u4e86\u4e5f\u5c31\u770b\u4e86\uff0c\u53ea\u6709\u4e2a\u5927\u6982\u7684\u5370\u8c61\uff0c\u73b0\u5728\u5728\u5b9e\u9645\u5de5\u4f5c\u7f16\u7801\u8fc7\u7a0b\u4e2d\u78b0\u5230\u95ee\u9898\u4e86\u624d\u540e\u6094\u5f53\u65f6\u4e3a\u4ec0\u4e48\u4e0d\u5b66\u624e\u5b9e\u4e00\u70b9\uff1f<\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u6807\u9898\u8d77\u7684\u6bd4\u8f83\u542b\u7cca\uff0c\u56e0\u4e3a\u6211\u73b0\u5728\u7684\u77e5\u8bc6\u6c34\u5e73\u65e0\u6cd5\u5b8c\u5168\u89e3\u91ca\u3001\u8bf4\u660e source\/fork\/exec \u4e2d\u5305\u542b\u7684\u77e5\u8bc6\u70b9\uff0c\u53ea\u80fd\u4e00\u70b9\u4e00\u70b9\u6765\u5427\uff0c\u9010\u6b65\u52a0\u6df1\u7406\u89e3\u3002<\/p>\n<p>=Start=<\/p>\n<h5>=source\/fork\/exec\u4e09\u8005\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b=<\/h5>\n<p># source<br \/>\n<span style=\"color: #ff0000;\"><strong>\u6240\u8c13source\uff0c\u5c31\u662f\u8ba9 script \u5728\u5f53\u524d shell \u5185\u6267\u884c<\/strong><\/span>\u3001 \u800c\u4e0d\u662f\u4ea7\u751f\u4e00\u4e2a sub-shell \u6765\u6267\u884c\u3002 \u7531\u4e8e\u6240\u6709\u6267\u884c\u7ed3\u679c\u5747\u5728\u5f53\u524d shell \u5185\u6267\u884c\uff0c\u6240\u4ee5\u5728\u5f53\u524d script \u4e2d\u7684\u5176\u5b83\u5730\u65b9\u53ef\u4ee5\u7528\u5230\u6267\u884c\u7ed3\u679c\u4e2d\u7684\u53d8\u91cf\u548c\u503c\u3002\u4e3e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">[root@sec-tools script_s]# pwd\n\/root\/script_s\n[root@sec-tools script_s]# cat cd_2_tmp.sh\n#!\/bin\/bash\ncd \/tmp\n[root@sec-tools script_s]# source cd_2_tmp.sh\n[root@sec-tools tmp]# pwd\n\/tmp<\/pre>\n<p># fork<br \/>\n\u6b63\u5e38\u6765\u8bf4\uff0c\u5f53\u6211\u4eec\u6267\u884c\u4e00\u4e2a shell script \u65f6(\u6bd4\u5982\u5728shell\u547d\u4ee4\u884c\u4e2d\u6267\u884c&#8221;.\/a.sh&#8221;)\uff0c\u5176\u5b9e\u662f\u5148\u4ea7\u751f\u4e00\u4e2a sub-shell \u7684\u5b50\u8fdb\u7a0b\uff0c \u7136\u540e\u7531 sub-shell \u518d\u53bb\u6267\u884c\u547d\u4ee4\u884c\u7684\u5b50\u8fdb\u7a0b\u3002\u56e0\u4e3a<span style=\"color: #ff0000;\"><strong>\u73af\u5883\u53d8\u91cf\u53ea\u80fd\u4ece\u7236\u8fdb\u7a0b\u5230\u5b50\u8fdb\u7a0b\u5355\u5411\u4f20\u9012(\u6362\u53e5\u8bdd\u8bf4\uff1a\u5728\u5b50\u8fdb\u7a0b\u4e2d\u73af\u5883\u5982\u4f55\u53d8\u66f4\uff0c\u5747\u4e0d\u4f1a\u5f71\u54cd\u7236\u8fdb\u7a0b\u7684\u73af\u5883)<\/strong><\/span>\uff0c\u6240\u4ee5\uff0c\u4f1a\u6709\u4e0b\u9762\u7684\u73b0\u8c61\uff1a<\/p>\n<pre class=\"lang:default decode:true\">[root@sec-tools script_s]# pwd\n\/root\/script_s\n[root@sec-tools script_s]# cat cd_2_tmp.sh\n#!\/bin\/bash\ncd \/tmp\n[root@sec-tools script_s]# .\/cd_2_tmp.sh\n[root@sec-tools script_s]# pwd\n\/root\/script_s<\/pre>\n<p># exec<br \/>\nexec \u548c source \u4e00\u6837\uff0c\u4e5f\u662f\u8ba9 script \u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\u4e0a\u6267\u884c\uff0c\u4f46\u662f<strong><span style=\"color: #ff0000;\">\u539f\u6709\u8fdb\u7a0b\u5728 script \u6267\u884c\u7ed3\u675f\u4e4b\u540e\u81ea\u5df1\u4e5f\u88ab\u7ed3\u675f\u4e86<\/span><\/strong>\u3002\u4e3e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">[root@sec-tools script_s]# pwd\n\/root\/script_s\n[root@sec-tools script_s]# cat cd_2_tmp.sh\n#!\/bin\/bash\ncd \/tmp\n[root@sec-tools script_s]# exec cd_2_tmp.sh\n-bash: exec: cd_2_tmp.sh: not found\n[root@sec-tools script_s]#\n[root@sec-tools script_s]# exec .\/cd_2_tmp.sh\nConnection to x.y.z.x closed.<\/pre>\n<p>\u7efc\u4e0a\uff0c\u539f\u6709\u8fdb\u7a0b\u662f\u5426\u4f1a\u88ab\u7ec8\u6b62\uff0c\u662fexec\u4e0esource\/fork\u7684\u6700\u5927\u5dee\u5f02\u3002<\/p>\n<h6>\u53c2\u8003\u5185\u5bb9\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/wiki.jikexueyuan.com\/project\/13-questions-of-shell\/exec-source.html\" target=\"_blank\">Shell\u5341\u4e09\u95ee-exec\u4e0esource\u5dee\u5728\u54ea\uff1f<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/wzb56\/13_questions_of_shell\" target=\"_blank\">https:\/\/github.com\/wzb56\/13_questions_of_shell<\/a><\/li>\n<\/ul>\n<p>=subshell\u7684\u4e00\u4e9b\u77e5\u8bc6\u70b9=<\/p>\n<ul>\n<li><a href=\"http:\/\/mywiki.wooledge.org\/BashFAQ\/024\" target=\"_blank\">http:\/\/mywiki.wooledge.org\/BashFAQ\/024<\/a><\/li>\n<li><a href=\"http:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/Command-Execution-Environment.html\" target=\"_blank\">http:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/Command-Execution-Environment.html<\/a><\/li>\n<li><a href=\"http:\/\/shouce.jb51.net\/shell\/subshells.html\" target=\"_blank\">http:\/\/shouce.jb51.net\/shell\/subshells.html<\/a><\/li>\n<li><a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/subshells.html\" target=\"_blank\">http:\/\/tldp.org\/LDP\/abs\/html\/subshells.html<\/a><\/li>\n<li><a href=\"http:\/\/fvue.nl\/wiki\/Bash:_Piped_%60while-read%27_loop_starts_subshell\" target=\"_blank\">http:\/\/fvue.nl\/wiki\/Bash:_Piped_%60while-read%27_loop_starts_subshell<\/a><\/li>\n<li><a href=\"http:\/\/www.ahlinux.com\/shell\/20040.html\" target=\"_blank\">http:\/\/www.ahlinux.com\/shell\/20040.html<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/caesarzou\/article\/details\/7310201\" target=\"_blank\">http:\/\/blog.csdn.net\/caesarzou\/article\/details\/7310201<\/a><\/li>\n<li><a href=\"http:\/\/www.blogbus.com\/wzgyantai-logs\/39539595.html\" target=\"_blank\">http:\/\/www.blogbus.com\/wzgyantai-logs\/39539595.html<\/a><\/li>\n<li><a href=\"http:\/\/www.centoscn.com\/shell\/2013\/0817\/1356.html\" target=\"_blank\">http:\/\/www.centoscn.com\/shell\/2013\/0817\/1356.html<\/a><\/li>\n<li><a href=\"http:\/\/cnbin.github.io\/blog\/2015\/06\/23\/fu-shell-yu-zi-shell\/\" target=\"_blank\">http:\/\/cnbin.github.io\/blog\/2015\/06\/23\/fu-shell-yu-zi-shell\/<\/a><\/li>\n<li><a href=\"https:\/\/en.wikibooks.org\/wiki\/A_Quick_Introduction_to_Unix\/Shells_and_subshells\" target=\"_blank\">https:\/\/en.wikibooks.org\/wiki\/A_Quick_Introduction_to_Unix\/Shells_and_subshells<\/a><\/li>\n<li><a href=\"http:\/\/bash.cyberciti.biz\/guide\/What_is_a_Subshell%3F\" target=\"_blank\">http:\/\/bash.cyberciti.biz\/guide\/What_is_a_Subshell%3F<\/a><\/li>\n<li><a href=\"http:\/\/op.baidu.com\/2014\/07\/bash%E7%9A%84%E5%B8%B8%E7%94%A8%E5%8A%9F%E8%83%BD%E5%92%8C%E6%8A%80%E5%B7%A7\/\" target=\"_blank\">bash \u7684\u5e38\u7528\u529f\u80fd\u548c\u6280\u5de7<\/a><\/li>\n<\/ul>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/16667794\/why-bash-subshell-doesnt-increase-in-pipe\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/16667794\/why-bash-subshell-doesnt-increase-in-pipe<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/4625309\/local-variables-in-bash-local-vs-subshell\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/4625309\/local-variables-in-bash-local-vs-subshell<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/tagged\/subshell\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/tagged\/subshell<\/a><\/li>\n<\/ul>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u8bb0\u5f97\u5728\u6211\u7b2c\u4e00\u6b21\u7ffb\u770b\u300aUnix\u73af\u5883\u9ad8\u7ea7\u7f16\u7a0b\u300b\u7684\u65f6\u5019\uff0c\u91cc\u9762\u6709\u8bb2\u8fc7fork\u548cexec\u7684\u77e5\u8bc6\uff08APUE\u8fd9\u672c\u8eab\u4e66\u5f88\u591a\u77e5 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7],"tags":[83,552],"class_list":["post-2446","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-bash","tag-subshell"],"views":3403,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2446"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2446\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}