{"id":167,"date":"2020-04-22T21:47:15","date_gmt":"2020-04-22T13:47:15","guid":{"rendered":"https:\/\/www.funnymuddy.com\/?p=167"},"modified":"2020-09-02T17:12:53","modified_gmt":"2020-09-02T09:12:53","slug":"shell-tips","status":"publish","type":"post","link":"https:\/\/www.funnymuddy.com\/?p=167","title":{"rendered":"Shell Tips"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u83dc\u5355\u9009\u62e9<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>#!\/bin\/bash\necho \"which one do you want?\"\nselect option in option1 option2 option3 option4;\ndo\n break\ndone\necho \"You have selected $option\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7528\u6237\u8f93\u5165<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>read -p \"input a word :\" word\nif [ ! -n \"$word\" ] ;then\n echo \"you have not input a word!\"\nelse\n echo \"the word you input is $word\"\n read -p \"input another word :\" anotherword\nif [ ! -n \"anotherword\" ] ;then\n echo \"you have not input another word!\"\n else\n echo \"another word you input is $anotherword\"\n fi\nfi<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u5165\u53c2\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>if [ -z \"$1\" ]; then\n  echo \"no param input\"\nelse\n  echo \"$1\"\nfi<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5224\u65ad\u6587\u4ef6\u548c\u6587\u4ef6\u5939<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>#!\/bin\/bash\nif [ ! -e \"\/home\/vagrant\/abc\" ];then\n echo \"file or dir not exists.\"\nelse\n rm -rf \/home\/vagrant\/abc\necho \"file\/dir removed.\"\nfi\n\nif [ ! -d \"\/home\/vagrant\/test\" ];then\n  mkdir \/home\/vagrant\/test\nelse\n  echo \"dir exists.\"\nfi\n\nif [ ! -f \"\/home\/vagrant\/abc\" ];then\n echo \"file not exists.\"\nelse\n rm -f \/home\/vagrant\/abc\necho \"file removed.\"\nfi<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>#!\/bin\/bash\nif [ $# != 2 ] ; then\necho \"USAGE: $0 file_path_1 file_path_2\"\necho \" e.g.: $0 \/home\/vagrant\/abc \/home\/vagrant\/efg\"\nexit 1;\nfi<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u811a\u672c\u8def\u5f84<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SCRIPT_DIR=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" &amp;&amp; pwd )\"\necho $SCRIPT_DIR<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">sed<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\u66ff\u6362: \n<code>sed -i \"s\/string_a\/string_b\/g\" file_path\n<\/code>\u66ff\u6362\u7684\u5982\u679c\u662f\u8def\u5f84\uff08\u542b\u6709\/)\uff0c\u5219\u7528|\u4ee3\u66ff\u3002\u6ce8\u610f\u8981\u7528\u53cc\u5f15\u53f7\n<code>str=\"\/data\/path\"\nsed -i \"s|string_a|$str|g\" file_path<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">eval<\/h3>\n\n\n\n<p>mycmd=&#8217;ls -all&#8217;<br>eval $mycmd #execute command<\/p>\n\n\n\n<p><\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_167\" class=\"pvc_stats all  \" data-element-id=\"167\" style=\"\"><i class=\"pvc-stats-icon small\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img decoding=\"async\" src=\"https:\/\/www.funnymuddy.com\/wp-content\/plugins\/page-views-count\/ajax-loader.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u83dc\u5355\u9009\u62e9 #!\/bin\/bash echo &#8220;which one do you want?&#8221; select o&hellip;&nbsp;<a href=\"https:\/\/www.funnymuddy.com\/?p=167\" class=\"\" rel=\"bookmark\">\u9605\u8bfb\u66f4\u591a &raquo;<span class=\"screen-reader-text\">Shell Tips<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_167\" class=\"pvc_stats all  \" data-element-id=\"167\" style=\"\"><i class=\"pvc-stats-icon small\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img decoding=\"async\" src=\"https:\/\/www.funnymuddy.com\/wp-content\/plugins\/page-views-count\/ajax-loader.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,1],"tags":[],"class_list":["post-167","post","type-post","status-publish","format-standard","hentry","category-os","category-1"],"_links":{"self":[{"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=167"}],"version-history":[{"count":7,"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":605,"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=\/wp\/v2\/posts\/167\/revisions\/605"}],"wp:attachment":[{"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.funnymuddy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}