{"id":92,"date":"2023-03-17T00:44:57","date_gmt":"2023-03-16T16:44:57","guid":{"rendered":"http:\/\/idc.birk.cn\/?p=92"},"modified":"2023-03-17T01:01:33","modified_gmt":"2023-03-16T17:01:33","slug":"linux-%e4%b8%8b-shell-%e5%91%bd%e4%bb%a4%e7%9a%84%e5%88%86%e7%b1%bb%e5%8f%8a%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/idc.birk.cn\/?p=92","title":{"rendered":"Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5"},"content":{"rendered":"<p>\u5728 Linux \u4e0b\uff0c\u4e3b\u8981\u6709\u53ef\u6267\u884c\u6587\u4ef6\u3001shell\u547d\u4ee4\u7b49\u4e0d\u540c\u7684\u5206\u7c7b\uff0c\u4e0b\u9762\u5217\u51fa\u4e86 Linux \u4e0b\u547d\u4ee4\u7684\u4e0d\u540c\u79cd\u7c7b\uff1a<\/p>\n<div><strong>1. \u7a0b\u5e8f\u53ef\u6267\u884c\u6587\u4ef6\uff08\u6587\u4ef6\u7cfb\u7edf \u4e2d\u7684\u547d\u4ee4\uff09<\/strong><\/div>\n<p>\u5f53\u4f60\u6267\u884c\u4e00\u6761\u547d\u4ee4\u7684\u65f6\u5019\uff0cLinux \u901a\u8fc7\u4ece\u5de6\u5230\u53f3\u641c\u7d22\u5b58\u50a8\u5728 $PATH \u73af\u5883\u53d8\u91cf\u4e2d\u7684\u76ee\u5f55\u6765\u627e\u5230\u8fd9\u6761\u547d\u4ee4\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u67e5\u770b\u5b58\u50a8\u5728 $PATH \u4e2d\u7684\u76ee\u5f55\uff1a<\/p>\n<pre>$ echo $PATH\r\n\/home\/aaronkilik\/bin:\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin:\/usr\/games:\/usr\/local\/games\r\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u547d\u4ee4\u4e2d\uff0c\u76ee\u5f55<\/p>\n<pre>\/home\/aaronkilik\/bin<\/pre>\n<p>\u5c06\u4f1a\u88ab\u9996\u5148\u641c\u7d22\uff0c\u7d27\u8ddf\u7740\u662f<\/p>\n<pre>\/usr\/local\/sbin<\/pre>\n<p>\u7136\u540e\u4e00\u76f4\u63a5\u7740\u4e0b\u53bb\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u641c\u7d22\u987a\u5e8f\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u6bd4\u5982\u5728 \/usr\/bin \u76ee\u5f55\u91cc\u7684\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u547d\u4ee4 $ ll \/bin\/\u3002<br \/>\n\u793a\u4f8b\u8f93\u51fa\uff1a<\/p>\n<pre>total 16284\r\ndrwxr-xr-x  2 root root    4096 Jul 31 16:30 .\/\r\ndrwxr-xr-x 23 root root    4096 Jul 31 16:29 ..\/\r\n-rwxr-xr-x  1 root root    6456 Apr 14 18:53 archdetect*\r\n-rwxr-xr-x  1 root root 1037440 May 17 16:15 bash*\r\n-rwxr-xr-x  1 root root  520992 Jan 20  2016 btrfs*\r\n-rwxr-xr-x  1 root root  249464 Jan 20  2016 btrfs-calc-size*\r\nlrwxrwxrwx  1 root root       5 Jul 31 16:19 btrfsck -&gt; btrfs*\r\n-rwxr-xr-x  1 root root  278376 Jan 20  2016 btrfs-convert*\r\n-rwxr-xr-x  1 root root  249464 Jan 20  2016 btrfs-debug-tree*\r\n-rwxr-xr-x  1 root root  245368 Jan 20  2016 btrfs-find-root*\r\n-rwxr-xr-x  1 root root  270136 Jan 20  2016 btrfs-image*\r\n-rwxr-xr-x  1 root root  249464 Jan 20  2016 btrfs-map-logical*\r\n-rwxr-xr-x  1 root root  245368 Jan 20  2016 btrfs-select-super*\r\n-rwxr-xr-x  1 root root  253816 Jan 20  2016 btrfs-show-super*\r\n-rwxr-xr-x  1 root root  249464 Jan 20  2016 btrfstune*\r\n-rwxr-xr-x  1 root root  245368 Jan 20  2016 btrfs-zero-log*\r\n-rwxr-xr-x  1 root root   31288 May 20  2015 bunzip2*\r\n-rwxr-xr-x  1 root root 1964536 Aug 19  2015 busybox*\r\n-rwxr-xr-x  1 root root   31288 May 20  2015 bzcat*\r\nlrwxrwxrwx  1 root root       6 Jul 31 16:19 bzcmp -&gt; bzdiff*\r\n-rwxr-xr-x  1 root root    2140 May 20  2015 bzdiff*\r\nlrwxrwxrwx  1 root root       6 Jul 31 16:19 bzegrep -&gt; bzgrep*\r\n-rwxr-xr-x  1 root root    4877 May 20  2015 bzexe*\r\nlrwxrwxrwx  1 root root       6 Jul 31 16:19 bzfgrep -&gt; bzgrep*\r\n-rwxr-xr-x  1 root root    3642 May 20  2015 bzgrep*\r\n<\/pre>\n<div><strong>2. Linux \u522b\u540d<\/strong><\/div>\n<p>\u8fd9\u4e9b\u662f\u7528\u6237\u5b9a\u4e49\u7684\u547d\u4ee4\uff0c\u5b83\u4eec\u662f\u901a\u8fc7 shell \u5185\u7f6e\u547d\u4ee4 alias \u521b\u5efa\u7684\uff0c\u5176\u4e2d\u5305\u542b\u5176\u5b83\u4e00\u4e9b\u5e26\u6709\u9009\u9879\u548c\u53c2\u6570\u7684 shell \u547d\u4ee4\u3002\u8fd9\u4e2a\u610f\u56fe\u4e3b\u8981\u662f\u4f7f\u7528\u65b0\u9896\u3001\u7b80\u77ed\u7684\u540d\u5b57\u6765\u66ff\u4ee3\u5197\u957f\u7684\u547d\u4ee4\u3002\u521b\u5efa\u4e00\u4e2a\u522b\u540d\u7684\u8bed\u6cd5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre>$ alias newcommand='command -options'<\/pre>\n<p>\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4\uff0c\u53ef\u4ee5\u5217\u4e3e\u7cfb\u7edf\u4e2d\u7684\u6240\u6709\u522b\u540d\uff1a<\/p>\n<pre>$ alias -p\r\nalias alert='notify-send --urgency=low -i \"$([ $? = 0 ] &amp;&amp; echo terminal || echo error)\" \"$(history|tail -n1|sed -e '\/''s\/^\/s*[0-9]\/+\/s*\/\/;s\/[;&amp;|]\/s*alert$\/\/'\/'')\"'\r\nalias egrep='egrep --color=auto'\r\nalias fgrep='fgrep --color=auto'\r\nalias grep='grep --color=auto'\r\nalias l='ls -CF'\r\nalias la='ls -A'\r\nalias ll='ls -alF'\r\nalias ls='ls --color=auto'\r\n<\/pre>\n<p>\u8981\u5728 Linux \u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u522b\u540d\uff0c\u4ed4\u7ec6\u9605\u8bfb\u4e0b\u9762\u7684\u4f8b\u5b50\u3002<\/p>\n<pre>$ alias update='sudo apt update'\r\n$ alias upgrade='sudo apt dist-upgrade'\r\n$ alias -p | grep 'up'\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-93 mBox\"  title=\"Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5\" src=\"http:\/\/idc.birk.cn\/wp-content\/uploads\/2023\/03\/065440kiadkuydu2kamq7d.png\" alt=\"Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5\" width=\"711\" height=\"172\" srcset=\"https:\/\/idc.birk.cn\/wp-content\/uploads\/2023\/03\/065440kiadkuydu2kamq7d.png 711w, https:\/\/idc.birk.cn\/wp-content\/uploads\/2023\/03\/065440kiadkuydu2kamq7d-300x73.png 300w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><\/p>\n<p>\u7136\u800c\uff0c\u4e0a\u9762\u8fd9\u4e9b\u6211\u4eec\u521b\u5efa\u7684\u522b\u540d\u53ea\u80fd\u6682\u65f6\u7684\u5de5\u4f5c\uff0c\u5f53\u7ecf\u8fc7\u4e0b\u4e00\u6b21\u7cfb\u7edf\u542f\u52a8\u540e\u5b83\u4eec\u4e0d\u518d\u5de5\u4f5c\u3002\u4f60\u53ef\u4ee5\u50cf\u4e0b\u9762\u5c55\u793a\u7684\u8fd9\u6837\u5728 '.bashrc' \u6587\u4ef6\u4e2d\u8bbe\u7f6e\u6c38\u4e45\u522b\u540d\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-94 mBox\"  title=\"Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5\" src=\"http:\/\/idc.birk.cn\/wp-content\/uploads\/2023\/03\/065440z58ezkxu7xxvossv.png\" alt=\"Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5\" width=\"712\" height=\"325\" srcset=\"https:\/\/idc.birk.cn\/wp-content\/uploads\/2023\/03\/065440z58ezkxu7xxvossv.png 712w, https:\/\/idc.birk.cn\/wp-content\/uploads\/2023\/03\/065440z58ezkxu7xxvossv-300x137.png 300w\" sizes=\"auto, (max-width: 712px) 100vw, 712px\" \/><\/p>\n<p>\u6dfb\u52a0\u4ee5\u540e\uff0c\u8fd0\u884c\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u6fc0\u6d3b\uff1a<\/p>\n<pre>$ source ~\/.bashrc<\/pre>\n<div><strong>3. Linux Shell \u4fdd\u7559\u5b57<\/strong><\/div>\n<p>\u5728 shell \u7a0b\u5e8f\u8bbe\u8ba1\u4e2d\uff0c<\/p>\n<pre>if\u3001then\u3001fi\u3001for\u3001while\u3001case\u3001esac\u3001else\u3001until<\/pre>\n<p>\u4ee5\u53ca\u5176\u4ed6\u66f4\u591a\u7684\u5b57\u90fd\u662f shell \u4fdd\u7559\u5b57\u3002\u6b63\u5982\u63cf\u8ff0\u6240\u6697\u793a\u7684\uff0c\u5b83\u4eec\u5728 shell \u4e2d\u6709\u7279\u6b8a\u7684\u542b\u4e49\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4e0b\u9762\u5c55\u793a\u7684 type \u547d\u4ee4\u6765\u5217\u51fa\u6240\u6709\u7684 shell \u5173\u952e\u5b57\uff1a<\/p>\n<pre>$ type if then fi for while case esac else until\r\nif is a shell keyword\r\nthen is a shell keyword\r\nfi is a shell keyword\r\nfor is a shell keyword\r\nwhile is a shell keyword\r\ncase is a shell keyword\r\nesac is a shell keyword\r\nelse is a shell keyword\r\nuntil is a shell keyword\r\n<\/pre>\n<div><strong>4. Linux shell \u51fd\u6570<\/strong><\/div>\n<p>\u4e00\u4e2a shell \u51fd\u6570\u662f\u4e00\u7ec4\u5728\u5f53\u524d shell \u5185\u4e00\u8d77\u6267\u884c\u7684\u547d\u4ee4\u3002\u51fd\u6570\u6709\u5229\u4e8e\u5728 shell\u00a0<a title=\"\u811a\u672c\" href=\"https:\/\/www.linuxcool.com\/\" target=\"_blank\" rel=\"noopener\">\u811a\u672c<\/a>\u4e2d\u5b9e\u73b0\u7279\u6b8a\u4efb\u52a1\u3002\u5728 shell\u00a0<a title=\"\u811a\u672c\" href=\"https:\/\/www.linuxcool.com\/\" target=\"_blank\" rel=\"noopener\">\u811a\u672c<\/a>\u4e2d\u5199 shell \u51fd\u6570\u7684\u4f20\u7edf\u5f62\u5f0f\u662f\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre>function_name() {\r\ncommand1\r\ncommand2\r\n......\r\n}\r\n<\/pre>\n<p>\u6216\u8005\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre>function function_name {\r\ncommand1\r\ncommand2\r\n......\r\n}\r\n<\/pre>\n<p>\u8ba9\u6211\u4eec\u770b\u4e00\u770b\u5982\u4f55\u5728\u4e00\u4e2a\u540d\u4e3a shell_functions.sh \u7684\u811a\u672c\u4e2d\u5199 shell \u51fd\u6570\u3002<\/p>\n<pre>#!\/bin\/bash \r\n#write a shell function to update and upgrade installed packages \r\nupgrade_system(){\r\nsudo apt update;\r\nsudo apt dist-upgrade;\r\n}\r\n#execute function\r\nupgrade_system\r\n<\/pre>\n<p>\u53d6\u4ee3\u901a\u8fc7\u547d\u4ee4\u884c\u6267\u884c\u4e24\u6761\u547d\u4ee4\uff1a<\/p>\n<pre>sudo apt update<\/pre>\n<p>\u548c<\/p>\n<pre>sudo apt dist-upgrade<\/pre>\n<p>\u6211\u4eec\u5728\u811a\u672c\u5185\u5199\u4e86\u4e00\u4e2a\u50cf\u6267\u884c\u4e00\u6761\u5355\u4e00\u547d\u4ee4\u4e00\u6837\u6765\u6267\u884c\u4e24\u6761\u547d\u4ee4\u7684 shell \u51fd\u6570 upgrade_system\u3002\u4fdd\u5b58\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u811a\u672c\u53ef\u6267\u884c\u3002\u6700\u540e\u50cf\u4e0b\u9762\u8fd9\u6837\u8fd0\u884c shell \u51fd\u6570\uff1a<\/p>\n<pre>$ chmod +x shell_functions.sh\r\n$ .\/shell_functions.sh\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-95 mBox\"  title=\"Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5\" src=\"http:\/\/idc.birk.cn\/wp-content\/uploads\/2023\/03\/065441n8erpdzm9ipp040p.png\" alt=\"Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5Linux \u4e0b Shell \u547d\u4ee4\u7684\u5206\u7c7b\u53ca\u7528\u6cd5\" width=\"714\" height=\"553\" srcset=\"https:\/\/idc.birk.cn\/wp-content\/uploads\/2023\/03\/065441n8erpdzm9ipp040p.png 714w, https:\/\/idc.birk.cn\/wp-content\/uploads\/2023\/03\/065441n8erpdzm9ipp040p-300x232.png 300w\" sizes=\"auto, (max-width: 714px) 100vw, 714px\" \/><\/p>\n<div><strong>5. Linux Shell \u5185\u7f6e\u547d\u4ee4<\/strong><\/div>\n<p>\u8fd9\u4e9b\u662f\u5728 shell \u4e2d\u5185\u7f6e\u7684 Linux \u547d\u4ee4\uff0c\u6240\u4ee5\u4f60\u65e0\u6cd5\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u627e\u5230\u5b83\u4eec\u3002\u8fd9\u4e9b\u547d\u4ee4\u5305\u62ec pwd\u3001cd\u3001bg\u3001alias\u3001history\u3001type\u3001source\u3001read\u3001exit \u7b49\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u5c55\u793a\u7684 type \u547d\u4ee4\u6765\u5217\u51fa\u6216\u68c0\u67e5 Linux \u5185\u7f6e\u547d\u4ee4\uff1a<\/p>\n<pre>$ type pwd\r\npwd is a shell builtin\r\n$ type cd\r\ncd is a shell builtin\r\n$ type bg\r\nbg is a shell builtin\r\n$ type alias\r\nalias is a shell builtin\r\n$ type history\r\nhistory is a shell builtin\r\n<\/pre>\n<div><strong>\u7ed3\u8bba<\/strong><\/div>\n<p>\u4f5c\u4e3a\u4e00\u4e2a Linux \u7528\u6237\uff0c\u77e5\u9053\u4f60\u6240\u8fd0\u884c\u7684\u547d\u4ee4\u7c7b\u578b\u662f\u5f88\u91cd\u8981\u7684\u3002\u6211\u76f8\u4fe1\uff0c\u901a\u8fc7\u4e0a\u9762\u660e\u786e\u3001\u7b80\u5355\u5e76\u4e14\u6613\u4e8e\u7406\u89e3\u7684\u89e3\u91ca\uff0c\u5305\u62ec\u4e00\u4e9b\u76f8\u5173\u7684\u8bf4\u660e\uff0c\u4f60\u53ef\u80fd\u5bf9 \u201cLinux\u547d\u4ee4\u7684\u4e0d\u540c\u79cd\u7c7b\u201d\u6709\u4e86\u5f88\u597d\u7684\u7406\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Linux \u4e0b\uff0c\u4e3b\u8981\u6709\u53ef\u6267\u884c\u6587\u4ef6\u3001shell\u547d\u4ee4\u7b49\u4e0d\u540c\u7684\u5206\u7c7b\uff0c\u4e0b\u9762\u5217\u51fa\u4e86 Linux \u4e0b\u547d\u4ee4\u7684\u4e0d\u540c\u79cd\u7c7b\uff1a 1. \u7a0b\u5e8f\u53ef\u6267\u884c\u6587\u4ef6\uff08\u6587\u4ef6\u7cfb\u7edf \u4e2d\u7684\u547d\u4ee4\uff09 \u5f53\u4f60\u6267\u884c\u4e00\u6761\u547d\u4ee4\u7684\u65f6\u5019\uff0cLinux \u901a\u8fc7\u4ece\u5de6\u5230\u53f3\u641c\u7d22\u5b58\u50a8\u5728 $PATH \u73af\u5883\u53d8\u91cf\u4e2d\u7684\u76ee [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":93,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[6,12],"topic":[37],"class_list":["post-92","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-linux","tag-shell","topic-linux"],"_links":{"self":[{"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=92"}],"version-history":[{"count":3,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/posts\/92\/revisions"}],"predecessor-version":[{"id":98,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/posts\/92\/revisions\/98"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=\/wp\/v2\/media\/93"}],"wp:attachment":[{"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=92"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/idc.birk.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}