{"id":2519,"date":"2015-11-07T16:26:49","date_gmt":"2015-11-07T08:26:49","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2519"},"modified":"2015-11-07T16:26:49","modified_gmt":"2015-11-07T08:26:49","slug":"shell%e4%b8%ad%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8c%85%e5%90%ab","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2519.html","title":{"rendered":"Shell\u4e2d\u7684\u5b57\u7b26\u4e32\u5305\u542b"},"content":{"rendered":"<p>=Start=<\/p>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>linux shell string contain<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/229551\/string-contains-in-bash\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/229551\/string-contains-in-bash<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/4277665\/how-do-i-compare-two-string-variables-in-an-if-statement-in-bash\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/4277665\/how-do-i-compare-two-string-variables-in-an-if-statement-in-bash<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/2829613\/how-do-you-tell-if-a-string-contains-another-string-in-unix-shell-scripting\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/2829613\/how-do-you-tell-if-a-string-contains-another-string-in-unix-shell-scripting<\/a><\/li>\n<li><a href=\"http:\/\/codingstandards.iteye.com\/blog\/1181490\" target=\"_blank\">http:\/\/codingstandards.iteye.com\/blog\/1181490<\/a><\/li>\n<li><a href=\"http:\/\/www.cnblogs.com\/chengmo\/archive\/2010\/10\/02\/1841355.html\" target=\"_blank\">http:\/\/www.cnblogs.com\/chengmo\/archive\/2010\/10\/02\/1841355.html<\/a><\/li>\n<\/ul>\n<h5>\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6>1.\u901a\u914d\u7b26<\/h6>\n<pre class=\"lang:default decode:true\">string='My long string'\nif [[ $string == *\"My long\"* ]]; then\n  echo \"It's there!\"\nfi<\/pre>\n<h6>2.\u6b63\u5219\u5339\u914d<\/h6>\n<pre class=\"lang:default decode:true\">string='My long string'\nif [[ $string =~ .*My.* ]]; then\n   echo \"It's there!\"\nfi<\/pre>\n<h6>3.switch&#8230;case\u7248\u672c\u7684\u901a\u914d\u7b26(\u901f\u5ea6\u6700\u5feb\u2026\u2026)<\/h6>\n<pre class=\"lang:default decode:true\">string='My long string'\ncase \"$string\" in\n  *foo*)\n    # Do stuff\n    ;;\nesac<\/pre>\n<h6>4.\u7528grep\u6765\u5b9e\u73b0<\/h6>\n<pre class=\"lang:default decode:true\">string='My long string'\nif grep -q foo &lt;&lt;&lt;$string; then\n    echo \"It's there\"\nfi<\/pre>\n<h6>5.\u7528\u5b57\u7b26\u4e32\u66ff\u6362\/\u5220\u9664\u6765\u5b9e\u73b0<\/h6>\n<pre class=\"lang:default decode:true\">string='My long string'\nif [ \"$string\" != \"${string\/foo\/}\" ]; then\n    echo \"It's there!\"\nfi<\/pre>\n<h6>\u6027\u80fd\u6bd4\u8f83\uff1a<\/h6>\n<p><a href=\"http:\/\/stackoverflow.com\/a\/25535717\" target=\"_blank\">http:\/\/stackoverflow.com\/a\/25535717<\/a><\/p>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash\n# Author:\tixyzero.com\n# Date:\t\t2015-11-7\n# set -x\n# export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '\n\n#echo \"$@\"\n#echo \"$*\"\n#LOGGER=\"N\"\n#options=\"$@\"\n#if [ \"$options\" != \"${options\/--log\/}\" ]; then\n#    LOGGER=\"Y\"\n#fi\n\nfunction stringContain() { [ -z \"${2##*$1*}\" ]; }\n\n# [[ $b =~ $a ]]\n# [ \"${b\/$a\/\/}\" = \"$b\" ]\n# [[ $b == *$a* ]]\n# case $b in *$a) : ;; esac\n# stringContain $a $b\n\n\/usr\/bin\/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do [[ $b =~ $a ]] ; x=$(($x-1)); done'\nsleep 2\n\/usr\/bin\/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do [ \"${b\/$a\/\/}\" = \"$b\" ] ; x=$(($x-1)); done'\nsleep 2\n\/usr\/bin\/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do [[ $b == *$a* ]] ; x=$(($x-1)); done'\nsleep 2\n\/usr\/bin\/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do case $b in *$a) : ;; esac ; x=$(($x-1)); done'\nsleep 2\n\/usr\/bin\/time bash -c 'function stringContain() { [ -z \"${2##*$1*}\" ]; }; a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do stringContain $a $b ; x=$(($x-1)); done'\nsleep 2\n\/usr\/bin\/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do [ -z \"${b##*$a*}\" ] ; x=$(($x-1)); done'\nsleep 2\n\/usr\/bin\/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do [ \"$b\" != \"${b\/$a\/}\" ] ; x=$(($x-1)); done'<\/pre>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u641c\u7d22\u5173\u952e\u5b57\uff1a linux shell string contain \u53c2\u8003\u94fe\u63a5\uff1a http:\/\/ [&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,12],"tags":[83,65,558,19],"class_list":["post-2519","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","category-tools","tag-bash","tag-shell","tag-string","tag-tips"],"views":2981,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2519","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=2519"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2519\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}