{"id":215,"date":"2015-05-11T19:41:35","date_gmt":"2015-05-11T11:41:35","guid":{"rendered":"http:\/\/owemeawe.com\/?p=215"},"modified":"2015-05-11T19:41:50","modified_gmt":"2015-05-11T11:41:50","slug":"%ef%bc%88%e8%bd%ac%ef%bc%89powershell%e4%b8%8eshell%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%af%b9%e6%af%94","status":"publish","type":"post","link":"https:\/\/peloo.net\/?p=215","title":{"rendered":"\uff08\u8f6c\uff09powershell\u4e0eshell\u5904\u7406\u5b57\u7b26\u4e32\u5bf9\u6bd4"},"content":{"rendered":"<div>\n<div>\u8f6c\u81ea\uff1ahttp:\/\/blog.chinaunix.net\/uid-9781829-id-1997702.html<\/div>\n<div><\/div>\n<div><span style=\"color: #006699\">1. \u6211\u4eec\u6765\u770bShell\u4e2d\u6c42\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u4f8b\u5b50:<\/span><\/div>\n<div><\/div>\n<\/div>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code>%x=\"abcd\"<br \/>\n#\u65b9\u6cd5\u4e00<br \/>\n%expr length $x<br \/>\n4<br \/>\n# \u65b9\u6cd5\u4e8c<br \/>\n%echo ${#x}<br \/>\n4<br \/>\n# \u65b9\u6cd5\u4e09<br \/>\n%expr \"$x\" : \".*\"<br \/>\n4<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6211\u4eec\u9700\u8981\u8fd9\u4e48\u591a\u79cd\u529e\u6cd5\u561b??\u6211\u89c9\u5f97\u4e00\u79cd\u5c31\u591f\u4e86. PowerShell\u4e2d, \u8c03\u7528\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u5c5e\u6027\u5c31\u53ef\u4ee5\u8fd4\u56de\u5b57\u7b26\u4e32\u957f\u5ea6\u4e86, \u5982\u4e0b:<\/p>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code>PS C:\\&gt; \"abcd\"<br \/>\nabcd<br \/>\nPS C:\\&gt; \"abcd\".length<br \/>\n4<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>PowerShell\u770b\u8d77\u6765\u662f\u4e0d\u662f\u66f4\u52a0\u7684\u6e05\u695a\u5462?<\/p>\n<p><span style=\"color: #006699\">2. \u6211\u4eec\u63a5\u4e0b\u6765\u770b\u770bshell\u4e2d \u67e5\u627e\u5b50\u4e32:<\/span><\/p>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code>%expr index $x \"a\"<br \/>\n1<br \/>\n%expr index $x \"bc\"<br \/>\n2<br \/>\n%expr index $x \"cd\"<br \/>\n3<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>expr\u8fd4\u56de\u7684\u6570\u7ec4\u4e0b\u6807\u662f\u4ece1\u5f00\u59cb\u8ba1\u6570\u7684. \u770b\u770bPowerShell\u600e\u4e48\u5e94\u4ed8\u5462?<\/p>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code>PS C:\\&gt; $x=\"abcd\"<br \/>\nPS C:\\&gt; $x.IndexOf('a')<br \/>\n0<br \/>\nPS C:\\&gt; $x.IndexOf('b')<br \/>\n1<br \/>\nPS C:\\&gt; $x.IndexOf('bc')<br \/>\n1<br \/>\nPS C:\\&gt; $x.IndexOf('cd')<br \/>\n2<br \/>\nPS C:\\&gt; $x.IndexOf('ef')<br \/>\n-1<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>OK, PowerShell\u7684\u6570\u7ec4\u4e0b\u6807\u548c\u5927\u90e8\u5206\u7f16\u7a0b\u8bed\u8a00\u4e00\u81f4, \u6570\u7ec4\u4e0b\u6807\u662f\u4ece0\u5f00\u59cb\u8ba1\u7b97\u7684.<\/p>\n<p><span style=\"color: #006699\">3. \u5728Shell\u4e2d\u5f97\u5230\u5b50\u5b57\u7b26\u4e32:<\/span><\/p>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code># \u65b9\u6cd5\u4e00<br \/>\n# expr &lt;string&gt; startpos length<br \/>\n%expr substr \"$x\" 1 3<br \/>\nabc<br \/>\n%expr substr \"$x\" 1 5<br \/>\nabcd<br \/>\n%expr substr \"$x\" 2 5<br \/>\nbcd<br \/>\n# \u65b9\u6cd5\u4e8c<br \/>\n# ${x:pos:lenght}<br \/>\n%echo ${x:1}<br \/>\nbcd<br \/>\n%echo ${x:2}<br \/>\ncd<br \/>\n%echo ${x:0}<br \/>\nabcd<br \/>\n%echo ${x:0:2}<br \/>\nab<br \/>\n%pos=1<br \/>\n%len=2<br \/>\n%echo ${x:$pos:$len}<br \/>\nbc<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6765\u770b\u770bPowerShell\u7684<span style=\"color: #000000\">Substring\u65b9\u6cd5\u5427<\/span><\/p>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code>PS C:\\&gt; $x<br \/>\nabcd<br \/>\nPS C:\\&gt; $x.Substring(0, 2)<br \/>\nab<br \/>\nPS C:\\&gt; $x.Substring(0, 4)<br \/>\nabcd<br \/>\nPS C:\\&gt; $x.Substring(0, 6)<br \/>\n<span style=\"color: #ff0000\">Exception calling <span style=\"color: #ff0000\">\"Substring\"<\/span> with <span style=\"color: #ff0000\">\"2\"<\/span> argument<span style=\"color: #ff0000\"><span style=\"color: #ff0000\">(<\/span>s): <\/span><\/span><span style=\"color: #ff0000\"><span style=\"color: #ff0000\">\"Index and length must refer to a location within the string.<br \/>\n<\/span><span style=\"color: #ff0000\">Parameter name: length\"<\/span><br \/>\n<\/span><span style=\"color: #ff0000\">At line:1 char:13<\/span><\/code><code><span style=\"color: #ff0000\">+ $x.<\/span><\/code><code><span style=\"color: #ff0000\">Substring(\u00a0&lt;&lt;&lt;&lt; 0, 6)<\/span><br \/>\nPS C:\\&gt; trap { $x.Substring(0, 6) }<br \/>\nPS C:\\&gt;<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>PowerShell\u4e0d\u5141\u8bb8\u5728\u5bf9\u5b57\u7b26\u4e32\u64cd\u4f5c\u8fc7\u7a0b\u4e2d, \u51fa\u73b0\u8d8a\u754c\u7684\u60c5\u51b5. \u56e0\u6b64\u5176\u4e2d\u4e00\u4e2a\u4f8b\u5b50\u7ed9\u51fa\u4e86\u9519\u8bef\u4fe1\u606f. \u4f46\u662f\u63a5\u4e0b\u6765\u6211\u4f7f\u7528trap\u5173\u952e\u5b57, \u5c06\u6267\u884c\u7684\u5f02\u5e38\u6355\u83b7. \u56e0\u6b64, \u8fd9\u6b21\u64cd\u4f5c\u5c31\u4ec0\u4e48\u90fd\u6ca1\u6709\u8f93\u51fa.<\/p>\n<p><span style=\"color: #006699\">4. shell\u4e2d\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f:<\/span><\/p>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code># \u6253\u5370\u5339\u914d\u957f\u5ea6<br \/>\n%expr match $x \".\"<br \/>\n1<br \/>\n%expr match $x \"abc\"<br \/>\n3<br \/>\n%expr match $x \"bc\"<br \/>\n0<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230, expr\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f, \u5339\u914d\u540e\u7ed9\u51fa\u4e86\u5339\u914d\u7684\u957f\u5ea6. \u6211\u4eec\u770b\u770bPowerShell\u5185\u7f6e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd0\u7b97\u7b26:<\/p>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code>PS C:\\&gt; $x -match '.'; $matches[0]; ($matches[0]).length<br \/>\nTrue<br \/>\na<br \/>\n1<br \/>\nPS C:\\&gt; $x -match 'abc'; $matches[0]; ($matches[0]).length<br \/>\nTrue<br \/>\nabc<br \/>\n3<br \/>\nPS C:\\&gt; $x -match 'dc'; $matches[0]; ($matches[0]).length<br \/>\nFalse<br \/>\nabc<br \/>\n3<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u4e09\u4e2a\u8bed\u53e5\u90fd\u53d1\u51fa\u4e86\u81ea\u5df1\u7684\u6267\u884c\u7ed3\u679c, -match\u64cd\u4f5c\u7b26\u8fd4\u56debool\u503c, \u63d0\u793a\u662f\u5426\u5339\u914d\u6210\u529f. \u5982\u679c\u5339\u914d\u6210\u529f\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee$matches\u8fd9\u4e2ahashtable\u83b7\u5f97\u8fd4\u56de\u60c5\u51b5. \u9ed8\u8ba4\u6574\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u4fe1\u606f\u88ab\u653e\u5728key = 0\u7684\u4f4d\u7f6e. \u5982\u679c\u5339\u914d\u4e0d\u6210\u529f, $matches\u7684\u4fe1\u606f\u662f\u4e0d\u4f1a\u88ab\u66f4\u65b0\u7684. \u8fd9\u4e9b\u884c\u4e3a\u548cPerl\u51e0\u4e4e\u4e00\u6837. \u719f\u6089Perl\u7684\u670b\u53cb\u662f\u4e0d\u662f\u89c9\u5f97\u8fd9\u4e2a\u5730\u65b9\u5f88\u81ea\u7136??\u76f8\u6bd4\u4f7f\u7528expr, \u867d\u7136\u8fd9\u91cc\u590d\u6742\u4e86\u4e00\u4e9b, \u4f46\u662f\u529f\u80fd\u786e\u5f3a\u5927\u4e86\u5f88\u591a.<\/p>\n<p><span style=\"color: #006699\">5. \u5728Shell\u4e2d\u5bf9\u5b57\u7b26\u4e32\u7684\u6390\u5934\u53bb\u5c3e<\/span><\/p>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code>%x=aabbaarealwwvvww<br \/>\n%echo \"${x%w*w}\"<br \/>\naabbaarealwwvv<br \/>\n%echo \"${x%%w*w}\"<br \/>\naabbaareal<br \/>\n%echo \"${x##a*a}\"<br \/>\nlwwvvww<br \/>\n%echo \"${x#a*a}\"<br \/>\nbbaarealwwvvww<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u54ce\u5440, \u8fd9\u4e2a\u5947\u602a\u7684\u8868\u8fbe\u5f0f, \u770b\u8d77\u6765\u8ba9\u4eba\u8d39\u89e3\u5462...&gt;_&lt;\u770b\u770b-replace\u8fd0\u7b97\u7b26\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u5427^^<\/p>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code>PS C:\\&gt; $x = 'aabbaarealwwvvww'<br \/>\nPS C:\\&gt; $x -replace 'w*$'<br \/>\naabbaarealwwvv<br \/>\nPS C:\\&gt; $x -replace 'w.*w$'<br \/>\naabbaareal<br \/>\nPS C:\\&gt; $x -replace '^a.*a'<br \/>\nlwwvvww<br \/>\nPS C:\\&gt; $x -replace '^a*'<br \/>\nbbaarealwwvvww<br \/>\nPS C:\\&gt;<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f60\u4f1a\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u561b??\u5982\u679c\u4f60\u4f1a, \u90a3\u4e48\u4e0a\u9762\u7684\u64cd\u4f5c, \u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8868\u793a\u4e0d\u662f\u5f88\u7b80\u5355\u561b?<\/p>\n<p><span style=\"color: #006699\">6. \u5728Shell\u4e2d\u5b57\u7b26\u4e32\u7684\u66ff\u6362<\/span><\/p>\n<p><span style=\"color: #000000\">\u8fd9\u91cc\u4e0d\u5141\u8bb8\u4f7f\u7528regex\u5462..\u4e0d\u8fc7\u53ef\u4ee5\u7528*,?...<\/span><\/p>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code>%x=abcdabcd<br \/>\n%echo ${x\/a\/b} # \u53ea\u66ff\u6362\u4e00\u4e2a<br \/>\nbbcdabcd<br \/>\n%echo ${x\/\/a\/b} # \u66ff\u6362\u6240\u6709<br \/>\nbbcdbbcd<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u7528PowerShell\u597d\u50cf\u4e0d\u5bb9\u6613\u5462...\u56e0\u4e3a-replace\u662f\u6574\u884c\u5339\u914d\u7684. \u8fd9\u70b9\u4e0a\u4e0d\u5982Perl\u7684s:::\u529f\u80fd\u5f3a\u5927. \u6211\u5148\u7ed9\u51fa\u7b2c\u4e8c\u4e2a\u7684\u529e\u6cd5...^^\u4e5f\u8bb8\u4ee5\u540e\u4f1a\u60f3\u5230\u597d\u529e\u6cd5\u5462<\/p>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code>PS C:\\&gt; $x='abcdabcd'<br \/>\nPS C:\\&gt; $x -replace 'a','b'<br \/>\nbbcdbbcd<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5199\u5230\u8fd9\u91cc, \u6211\u60f3\u5927\u5bb6\u4e5f\u80fd\u611f\u53d7\u5230PowerShell\u7684\u4e00\u4e9b\u7279\u6027, \u6b63\u5982\u6211\u7684\u7406\u89e3, \u6240\u6709.Net\u7a0b\u5e8f\u5458, \u5929\u751f\u5bf9PowerShell\u4e2d\u5927\u90e8\u5206\u529f\u80fd\u662f\u975e\u5e38\u719f\u6089\u7684. \u53ef\u4ee5\u76f4\u63a5\u8c03\u7528.Net\u4e0a\u7684\u65b9\u6cd5, \u4e3aPowerShell\u6269\u5c55\u4e86\u5f3a\u5927\u7684\u529f\u80fd.<\/p>\n<p>\u6211\u518d\u7ed9\u51e0\u4e2a\u6bd4\u8f83\u6709\u610f\u601d\u7684\u529f\u80fd:<\/p>\n<table border=\"1\" width=\"95%\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#f1f1f1\">\n<tbody>\n<tr>\n<td><code>PS C:\\&gt; $x<br \/>\nabcdabcd<br \/>\nPS C:\\&gt; $x.ToUpper()<br \/>\nABCDABCD<br \/>\nPS C:\\&gt; $x.ToUpper().ToLower()<br \/>\nabcdabcd<br \/>\nPS C:\\&gt; $x='abcdabcdab'<br \/>\nPS C:\\&gt; $x.Trim('ab')<br \/>\ncdabcd<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Trim\u662f\u4e2a\u5e38\u89c1\u7684\u529f\u80fd, \u8fd8\u6709TrimStart, TrimEnd. ToUpper, ToLower\u66f4\u662f\u5f88\u65b9\u4fbf\u7684\u529f\u80fd.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u81ea\uff1ahttp:\/\/blog.chinaunix.net\/uid-9781829-id-1997702.html 1. \u6211\u4eec\u6765\u770bShell\u4e2d\u6c42\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u4f8b\u5b50: %x=&#8221;abcd&#8221; #\u65b9\u6cd5\u4e00 %expr length $x 4 # \u65b9\u6cd5\u4e8c %echo ${#x} 4 # \u65b9\u6cd5\u4e09 %expr &#8220;$x&#8221; : &#8220;.*&#8221; 4 \u6211\u4eec\u9700\u8981\u8fd9\u4e48\u591a\u79cd\u529e\u6cd5\u561b??\u6211\u89c9\u5f97\u4e00\u79cd\u5c31\u591f\u4e86. PowerShell\u4e2d, \u8c03\u7528\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u5c5e\u6027\u5c31\u53ef\u4ee5\u8fd4\u56de\u5b57\u7b26\u4e32\u957f\u5ea6\u4e86, \u5982\u4e0b: PS C:\\&gt; &#8220;abcd&#8221; abcd PS C:\\&gt; &#8220;abcd&#8221;.length 4 PowerShell\u770b\u8d77\u6765\u662f\u4e0d\u662f\u66f4\u52a0\u7684\u6e05\u695a\u5462? 2. \u6211\u4eec\u63a5\u4e0b\u6765\u770b\u770bshell\u4e2d \u67e5\u627e\u5b50\u4e32: %expr index $x &#8220;a&#8221; 1 %expr index $x &#8220;bc&#8221; 2 %expr index $x &#8220;cd&#8221; 3 expr\u8fd4\u56de\u7684\u6570\u7ec4\u4e0b\u6807\u662f\u4ece1\u5f00\u59cb\u8ba1\u6570\u7684. \u770b\u770bPowerShell\u600e\u4e48\u5e94\u4ed8\u5462? PS&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-powershell","category-test"],"aioseo_notices":[],"brizy_media":[],"_links":{"self":[{"href":"https:\/\/peloo.net\/index.php?rest_route=\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peloo.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peloo.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peloo.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/peloo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=215"}],"version-history":[{"count":2,"href":"https:\/\/peloo.net\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/peloo.net\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions\/217"}],"wp:attachment":[{"href":"https:\/\/peloo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peloo.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peloo.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}