{"id":195466,"date":"2020-07-16T10:05:36","date_gmt":"2020-07-16T02:05:36","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=195466"},"modified":"2020-07-03T09:10:55","modified_gmt":"2020-07-03T01:10:55","slug":"five-methods-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/five-methods-linux.html","title":{"rendered":"5\u79cd\u5728Linux\u7ec8\u7aef\u4e2d\u8fdb\u884c\u7b97\u672f\u8fd0\u7b97\u7684\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5411\u60a8\u5c55\u793a\u5728Linux\u7ec8\u7aef\u4e2d\u8fdb\u884c\u7b97\u672f\u8fd0\u7b97\u7684\u5404\u79cd\u6709\u7528\u65b9\u6cd5\u3002 \u5728\u672c\u6587\u7ed3\u675f\u65f6\uff0c\u60a8\u5c06\u5b66\u4e60\u5728\u547d\u4ee4\u884c\u4e2d\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\u7684\u57fa\u672c\u4e0d\u540c\u5b9e\u7528\u65b9\u6cd5\u3002\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\uff01<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1.\u4f7f\u7528Bash Shell<\/strong><\/div>\n

\u5728Linux CLI\u4e0a\u8fdb\u884c\u57fa\u672c\u6570\u5b66\u8fd0\u7b97\u7684\u7b2c\u4e00\u4e2a\u4e5f\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u53cc\u62ec\u53f7\u3002 \u4ee5\u4e0b\u662f\u6211\u4eec\u4f7f\u7528\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\u7684\u503c\u7684\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n

$ ADD=$(( 1 + 2 ))\r\n$ echo $ADD\r\n$ MUL=$(( $ADD * 5 ))\r\n$ echo $MUL\r\n$ SUB=$(( $MUL - 5 ))\r\n$ echo $SUB\r\n$ DIV=$(( $SUB \/ 2 ))\r\n$ echo $DIV\r\n$ MOD=$(( $DIV % 2 ))\r\n$ echo $MOD<\/pre>\n

\"\"<\/p>\n

Linux Bash Shell\u4e2d\u7684\u7b97\u6cd5<\/p>\n

2.\u4f7f\u7528expr\u547d\u4ee4<\/strong><\/div>\n

expr\u547d\u4ee4\u8ba1\u7b97\u8868\u8fbe\u5f0f\u5e76\u5c06\u63d0\u4f9b\u7684\u8868\u8fbe\u5f0f\u7684\u503c\u6253\u5370\u5230\u6807\u51c6\u8f93\u51fa\u3002 \u6211\u4eec\u5c06\u7814\u7a76\u4f7f\u7528expr\u8fdb\u884c\u7b80\u5355\u6570\u5b66\uff0c\u8fdb\u884c\u6bd4\u8f83\uff0c\u589e\u52a0\u53d8\u91cf\u503c\u548c\u67e5\u627e\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u4e0d\u540c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528expr\u547d\u4ee4\u8fdb\u884c\u7b80\u5355\u8ba1\u7b97\u7684\u4e00\u4e9b\u793a\u4f8b\u3002 \u8bf7\u6ce8\u610f\uff0c\u8bb8\u591a\u8fd0\u7b97\u7b26\u9700\u8981\u5bf9shell\u8fdb\u884c\u8f6c\u4e49\u6216\u5f15\u7528\uff0c\u4f8b\u5982*\u8fd0\u7b97\u7b26\uff08\u6211\u4eec\u5c06\u5728\u8868\u8fbe\u5f0f\u7684\u6bd4\u8f83\u4e0b\u67e5\u770b\u66f4\u591a\uff09\u3002<\/p>\n

$ expr 3 + 5\r\n$ expr 15 % 3\r\n$ expr 5 \\* 3\r\n$ expr 5 \u2013 3\r\n$ expr 20 \/ 4<\/pre>\n

\"\"<\/p>\n

\u5728Linux\u4e2d\u4f7f\u7528expr\u547d\u4ee4\u7684\u57fa\u672c\u7b97\u6cd5<\/p>\n

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u8fdb\u884c\u6bd4\u8f83\u3002 \u5f53\u8868\u8fbe\u5f0f\u6c42\u503c\u4e3afalse\u65f6\uff0cexpr\u5c06\u6253\u5370\u503c0\uff0c\u5426\u5219\u6253\u53701\u3002<\/p>\n

\u6211\u4eec\u6765\u770b\u4e00\u4e9b\u4f8b\u5b50\uff1a<\/p>\n

$ expr 5 = 3\r\n$ expr 5 = 5\r\n$ expr 8 != 5\r\n$ expr 8 \\> 5\r\n$ expr 8 \\< 5\r\n$ expr 8 \\<= 5<\/pre>\n

\"\"<\/p>\n

\u6bd4\u8f83Linux\u4e2d\u7684\u7b97\u672f\u8868\u8fbe\u5f0f<\/p>\n

\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528expr\u547d\u4ee4\u589e\u52a0\u53d8\u91cf\u7684\u503c\u3002 \u770b\u4e00\u4e0b\u4e0b\u9762\u7684\u4f8b\u5b50\uff08\u4ee5\u540c\u6837\u7684\u65b9\u5f0f\uff0c\u4f60\u4e5f\u53ef\u4ee5\u51cf\u5c11\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\uff09\u3002<\/p>\n

$ NUM=$(( 1 + 2))\r\n$ echo $NUM\r\n$ NUM=$(expr $NUM + 2)\r\n$ echo $NUM<\/pre>\n

\"\"<\/p>\n

\u53d8\u91cf\u7684\u589e\u91cf\u503c<\/p>\n

\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u67e5\u627e\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff1a<\/p>\n

$ expr length \"\u8fd9\u91cc\u662fLinux\u516c\u793e www.linuxidc.com\"<\/pre>\n

\"\"<\/p>\n

\u6709\u5173\u4e0a\u8ff0\u8fd0\u7b97\u7b26\u542b\u4e49\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u89c1expr\u624b\u518c\u9875\uff1a<\/p>\n

$ man expr<\/pre>\n
3.\u4f7f\u7528bc\u547d\u4ee4<\/strong><\/div>\n

bc\uff08\u57fa\u672c\u8ba1\u7b97\u5668\uff09\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5b9e\u7528\u7a0b\u5e8f\uff0c\u5b83\u63d0\u4f9b\u4e86\u60a8\u5e0c\u671b\u4ece\u7b80\u5355\u7684\u79d1\u5b66\u6216\u91d1\u878d\u8ba1\u7b97\u5668\u4e2d\u83b7\u5f97\u7684\u6240\u6709\u529f\u80fd\u3002 \u5b83\u5bf9\u4e8e\u8fdb\u884c\u6d6e\u70b9\u6570\u5b66\u8fd0\u7b97\u7279\u522b\u6709\u7528\u3002\u5982\u679c\u672a\u5b89\u88c5bc\u547d\u4ee4\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n

$ sudo apt install bc  #Debian\/Ubuntu\r\n$ sudo yum install bc  #RHEL\/CentOS\r\n$ sudo dnf install bc  #Fedora 22+<\/pre>\n

\u5b89\u88c5\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5411\u5176\u4f20\u9012\u53c2\u6570\u4ee5\u4ea4\u4e92\u6a21\u5f0f\u6216\u975e\u4ea4\u4e92\u65b9\u5f0f\u8fd0\u884c\u5b83 - \u6211\u4eec\u5c06\u67e5\u770b\u8fd9\u4e24\u79cd\u60c5\u51b5\u3002 \u8981\u4ee5\u4ea4\u4e92\u65b9\u5f0f\u8fd0\u884c\u5b83\uff0c\u8bf7\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e0b\u952e\u5165\u547d\u4ee4bc\u5e76\u5f00\u59cb\u8fdb\u884c\u4e00\u4e9b\u6570\u5b66\u8fd0\u7b97\uff0c\u5982\u56fe\u6240\u793a\u3002<\/p>\n

$ bc<\/pre>\n

\"\"<\/p>\n

\u4ee5\u975e\u4ea4\u4e92\u6a21\u5f0f\u542f\u52a8bc<\/p>\n

\u4e0b\u9762\u7684\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728\u547d\u4ee4\u884c\u4e0a\u975e\u4ea4\u4e92\u5f0f\u5730\u4f7f\u7528bc\u3002<\/p>\n

$ echo '3+5' | bc\r\n$ echo '15 % 2' | bc\r\n$ echo '15 \/ 2' | bc\r\n$ echo '(6 * 2) - 5' | bc<\/pre>\n

\"\"<\/p>\n

-l\u6807\u5fd7\u7528\u4e8e\u9ed8\u8ba4\u6bd4\u4f8b\uff08\u5c0f\u6570\u70b9\u540e\u7684\u6570\u5b57\uff09\u523020\uff0c\u4f8b\u5982\uff1a<\/p>\n

linuxidc@linuxidc:~\/www.linuxidc.com$ echo '13\/5' | bc\r\n2\r\nlinuxidc@linuxidc:~\/www.linuxidc.com$ echo '13\/5' | bc -l\r\n2.60000000000000000000<\/pre>\n

\"\"<\/p>\n

\u7528\u6d6e\u52a8\u6570\u5b57\u505a\u6570\u5b66<\/p>\n

4.\u4f7f\u7528Awk\u547d\u4ee4<\/strong><\/div>\n

Awk\u662fGNU\/Linux\u4e2d\u6700\u7740\u540d\u7684\u6587\u672c\u5904\u7406\u7a0b\u5e8f\u4e4b\u4e00\u3002 \u5b83\u652f\u6301\u52a0\u6cd5\uff0c\u51cf\u6cd5\uff0c\u4e58\u6cd5\uff0c\u9664\u6cd5\u548c\u6a21\u6570\u8fd0\u7b97\u7b26\u3002 \u5b83\u5bf9\u4e8e\u8fdb\u884c\u6d6e\u70b9\u6570\u5b66\u8fd0\u7b97\u4e5f\u5f88\u6709\u7528\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u6267\u884c\u57fa\u672c\u6570\u5b66\u8fd0\u7b97\uff0c\u5982\u56fe\u6240\u793a\u3002<\/p>\n

$ awk 'BEGIN { a = 6; b = 3; print \"(a + b) = \", (a + b) }'\r\n$ awk 'BEGIN { a = 6; b = 3; print \"(a - b) = \", (a - b) }'\r\n$ awk 'BEGIN { a = 6; b = 3; print \"(a *  b) = \", (a * b) }'\r\n$ awk 'BEGIN { a = 6; b = 3; print \"(a \/ b) = \", (a \/ b) }'\r\n$ awk 'BEGIN { a = 6; b = 3; print \"(a % b) = \", (a % b) }'<\/pre>\n

\"\"<\/p>\n

5.\u4f7f\u7528factor\u547d\u4ee4<\/strong><\/div>\n

factor\u547d\u4ee4\u7528\u4e8e\u5c06\u6574\u6570\u5206\u89e3\u4e3a\u7d20\u6570\u56e0\u5b50\u3002 \u4f8b\u5982\uff1a<\/p>\n

linuxidc@linuxidc:~\/www.linuxidc.com$ factor 18\r\n18: 2 3 3\r\nlinuxidc@linuxidc:~\/www.linuxidc.com$ factor 133\r\n133: 7 19\r\nlinuxidc@linuxidc:~\/www.linuxidc.com$ factor 23\r\n23: 23\r\nlinuxidc@linuxidc:~\/www.linuxidc.com$ factor 200\r\n200: 2 2 2 5 5<\/pre>\n

\"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728Linux CLI\u4e0a\u8fdb\u884c\u57fa\u672c\u6570\u5b66\u8fd0\u7b97\u7684\u7b2c\u4e00\u4e2a\u4e5f\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u53cc\u62ec\u53f7\u3002 \u4ee5\u4e0b\u662f\u6211\u4eec\u4f7f\u7528\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\u7684\u503c\u7684 […]<\/p>\n","protected":false},"author":310,"featured_media":195492,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-195466","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/310"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=195466"}],"version-history":[{"count":13,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195466\/revisions"}],"predecessor-version":[{"id":195501,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195466\/revisions\/195501"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/195492"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=195466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=195466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=195466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}