{"id":145,"date":"2021-12-13T19:53:49","date_gmt":"2021-12-13T11:53:49","guid":{"rendered":"http:\/\/139.9.1.231\/?p=145"},"modified":"2021-12-13T19:53:49","modified_gmt":"2021-12-13T11:53:49","slug":"linux-7","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2021\/12\/13\/linux-7\/","title":{"rendered":"11.29\u8bfe\u7a0b\u603b\u7ed3"},"content":{"rendered":"\n<h3>shell \u51fd\u6570<\/h3>\n\n\n\n<p>\u51fd\u6570\uff08function\uff09\u662f\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u6709\u5229\u4e8e\u4ee3\u7801\u7684\u590d\u7528\u3002\u5b83\u4e0e\u522b\u540d\uff08alias\uff09\u7684\u533a\u522b\u662f\uff0c\u522b\u540d\u53ea\u9002\u5408\u5c01\u88c5\u7b80\u5355\u7684\u5355\u4e2a\u547d\u4ee4\uff0c\u51fd\u6570\u5219\u53ef\u4ee5\u5c01\u88c5\u590d\u6742\u7684\u591a\u884c\u547d\u4ee4\u3002<\/p>\n\n\n\n<p>\u51fd\u6570\u603b\u662f\u5728\u5f53\u524d Shell \u6267\u884c\uff0c\u8fd9\u662f\u8ddf\u811a\u672c\u7684\u4e00\u4e2a\u91cd\u5927\u533a\u522b\uff0cBash \u4f1a\u65b0\u5efa\u4e00\u4e2a\u5b50 Shell \u6267\u884c\u811a\u672c\u3002\u5982\u679c\u51fd\u6570\u4e0e\u811a\u672c\u540c\u540d\uff0c\u51fd\u6570\u4f1a\u4f18\u5148\u6267\u884c\u3002\u4f46\u662f\uff0c\u51fd\u6570\u7684\u4f18\u5148\u7ea7\u4e0d\u5982\u522b\u540d\uff0c\u5373\u5982\u679c\u51fd\u6570\u4e0e\u522b\u540d\u540c\u540d\uff0c\u90a3\u4e48\u522b\u540d\u4f18\u5148\u6267\u884c\u3002<\/p>\n\n\n\n<p>Bash \u51fd\u6570\u5b9a\u4e49\u7684\u8bed\u6cd5\u6709\u4e24\u79cd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u7b2c\u4e00\u79cd\nfn() {\n  # codes\n}\n\n# \u7b2c\u4e8c\u79cd\nfunction fn() {\n  # codes\n}<\/code><\/pre>\n\n\n\n<p>\u53c2\u6570\u53d8\u91cf<br>\n\u51fd\u6570\u4f53\u5185\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570\u53d8\u91cf\uff0c\u83b7\u53d6\u51fd\u6570\u53c2\u6570\u3002\u51fd\u6570\u7684\u53c2\u6570\u53d8\u91cf\uff0c\u4e0e\u811a\u672c\u53c2\u6570\u53d8\u91cf\u662f\u4e00\u81f4\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$1~$9\uff1a\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u5230\u7b2c9\u4e2a\u7684\u53c2\u6570\u3002\n$0\uff1a\u51fd\u6570\u6240\u5728\u7684\u811a\u672c\u540d\u3002\n$#\uff1a\u51fd\u6570\u7684\u53c2\u6570\u603b\u6570\u3002\n$@\uff1a\u51fd\u6570\u7684\u5168\u90e8\u53c2\u6570\uff0c\u53c2\u6570\u4e4b\u95f4\u4f7f\u7528\u7a7a\u683c\u5206\u9694\u3002\n$*\uff1a\u51fd\u6570\u7684\u5168\u90e8\u53c2\u6570\uff0c\u53c2\u6570\u4e4b\u95f4\u4f7f\u7528\u53d8\u91cf$IFS\u503c\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u5206\u9694\uff0c\u9ed8\u8ba4\u4e3a\u7a7a\u683c\uff0c\u4f46\u662f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u3002\n\u5982\u679c\u51fd\u6570\u7684\u53c2\u6570\u591a\u4e8e9\u4e2a\uff0c\u90a3\u4e48\u7b2c10\u4e2a\u53c2\u6570\u53ef\u4ee5\u7528${10}\u7684\u5f62\u5f0f\u5f15\u7528\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>hello() {\n  echo \"Hello $1\"\n}\n\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570\u4f53\u91cc\u9762\u7684$1\u8868\u793a\u51fd\u6570\u8c03\u7528\u65f6\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002\n\n\u8c03\u7528\u65f6\uff0c\u5c31\u76f4\u63a5\u5199\u51fd\u6570\u540d\uff0c\u53c2\u6570\u8ddf\u5728\u51fd\u6570\u540d\u540e\u9762\u3002\n\n$ hello world\nHello world\n\u4e0b\u9762\u662f\u4e00\u4e2a\u591a\u884c\u51fd\u6570\u7684\u4f8b\u5b50\uff0c\u663e\u793a\u5f53\u524d\u65e5\u671f\u65f6\u95f4\u3002\n\ntoday() {\n  echo -n \"Today's date is: \"\n  date +\"%A, %B %-d, %Y\"\n}<\/code><\/pre>\n\n\n\n<p>return\u547d\u4ee4\u7528\u4e8e\u4ece\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u51fd\u6570\u6267\u884c\u5230\u8fd9\u6761\u547d\u4ee4\uff0c\u5c31\u4e0d\u518d\u5f80\u4e0b\u6267\u884c\u4e86\uff0c\u76f4\u63a5\u8fd4\u56de\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function func_return_value {\n  return 10\n}<\/code><\/pre>\n\n\n\n<p>\u51fd\u6570\u5c06\u8fd4\u56de\u503c\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002\u5982\u679c\u547d\u4ee4\u884c\u76f4\u63a5\u6267\u884c\u51fd\u6570\uff0c\u4e0b\u4e00\u4e2a\u547d\u4ee4\u53ef\u4ee5\u7528$?\u62ff\u5230\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ func_return_value\n$ echo \"Value returned by function is: $?\"\nValue returned by function is: 10\nreturn\u540e\u9762\u4e0d\u8ddf\u53c2\u6570\uff0c\u53ea\u7528\u4e8e\u8fd4\u56de\u4e5f\u662f\u53ef\u4ee5\u7684\u3002\n\nfunction name {\n  commands\n  return\n}<\/code><\/pre>\n\n\n\n<p><strong>Bash \u51fd\u6570\u4f53\u5185\u76f4\u63a5\u58f0\u660e\u7684\u53d8\u91cf\uff0c\u5c5e\u4e8e\u5168\u5c40\u53d8\u91cf\uff0c\u6574\u4e2a\u811a\u672c\u90fd\u53ef\u4ee5\u8bfb\u53d6\u3002\u8fd9\u4e00\u70b9\u9700\u8981\u7279\u522b\u5c0f\u5fc3\u3002<\/strong><br>\n\u51fd\u6570\u4f53\u5185\u4e0d\u4ec5\u53ef\u4ee5\u58f0\u660e\u5168\u5c40\u53d8\u91cf\uff0c\u8fd8\u53ef\u4ee5\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#! \/bin\/bash\nfoo=1\n\nfn () {\n  foo=2\n}\n\nfn\n\necho $foo<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u91cc\u9762\u53ef\u4ee5\u7528local\u547d\u4ee4\u58f0\u660e\u5c40\u90e8\u53d8\u91cf\u3002 local \u53d8\u91cf\u540d<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#! \/bin\/bash\n# \u811a\u672c test.sh\nfn () {\n  local foo\n  foo=1\n  echo \"fn: foo = $foo\"\n}\n\nfn\necho \"global: foo = $foo\"<\/code><\/pre>\n\n\n\n<h3>\u5411\u51fd\u6570\u4f20\u9012\u6570\u7ec4\/\u51fd\u6570\u8fd4\u56de\u6570\u7ec4<\/h3>\n\n\n\n<p>1\u3001\u5411\u51fd\u6570\u4f20\u9012\u6570\u7ec4 \u4f7f\u7528 &#8220;$@&#8221;(\u5168\u90e8\u53c2\u6570)<\/p>\n\n\n\n<p><strong>\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u76f4\u63a5\u5199\u51fd\u6570\u540d\uff0c\u53c2\u6570\u8ddf\u5728\u51fd\u6570\u540d\u540e\u9762\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function array(){\n  for i in \"$@\"\n   do \n      echo ${i}\n   done\n}\n#\u8c03\u7528\u51fd\u6570\uff1a\na=(1 3 5 6)\nayrray  ${a[*]}<\/code><\/pre>\n\n\n\n<ol><li>\u51fd\u6570\u8fd4\u56de\u6570\u7ec4<\/li><\/ol>\n\n\n\n<p>\u5728\u51fd\u6570\u5185\u90e8 echo ${array[*]}<\/p>\n\n\n\n<p>\u7136\u540e arry=\u51fd\u6570\u540d \u8f93\u5165\u53c2\u6570<br>\n\u5c31\u5c06echo\u7684\u6570\u7ec4\u8d4b\u503c\u7ed9arry<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function array(){\n  echo new value:${newwarray[*]}\n}\n\nresult = `array`<\/code><\/pre>\n\n\n\n<h3>\u6570\u7ec4<\/h3>\n\n\n\n<p>\u521b\u5efa\u6570\u7ec4<br>\n\u6570\u7ec4\u53ef\u4ee5\u91c7\u7528\u9010\u4e2a\u8d4b\u503c\u7684\u65b9\u6cd5\u521b\u5efa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ARRAY[INDEX]=value\n\u4e0a\u9762\u8bed\u6cd5\u4e2d\uff0cARRAY\u662f\u6570\u7ec4\u7684\u540d\u5b57\uff0c\u53ef\u4ee5\u662f\u4efb\u610f\u5408\u6cd5\u7684\u53d8\u91cf\u540d\u3002INDEX\u662f\u4e00\u4e2a\u5927\u4e8e\u6216\u7b49\u4e8e\u96f6\u7684\u6574\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u7b97\u672f\u8868\u8fbe\u5f0f\u3002\u6ce8\u610f\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u4e0b\u6807\u662f0\uff0c \u800c\u4e0d\u662f1\u3002<\/code><\/pre>\n\n\n\n<p><strong>\u4e0b\u9762\u521b\u5efa\u4e00\u4e2a\u4e09\u4e2a\u6210\u5458\u7684\u6570\u7ec4\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ array[0]=val\n$ array[1]=val\n$ array[2]=val<\/code><\/pre>\n\n\n\n<p>\u6570\u7ec4\u4e5f\u53ef\u4ee5\u91c7\u7528\u4e00\u6b21\u6027\u8d4b\u503c\u7684\u65b9\u5f0f\u521b\u5efa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ARRAY=(value1 value2 ... valueN)\n\n# \u7b49\u540c\u4e8e\n\nARRAY=(\n  value1\n  value2\n  value3\n)<\/code><\/pre>\n\n\n\n<p>\u91c7\u7528\u4e0a\u9762\u65b9\u5f0f\u521b\u5efa\u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u6309\u7167\u9ed8\u8ba4\u987a\u5e8f\u8d4b\u503c\uff0c\u4e5f\u53ef\u4ee5\u5728\u6bcf\u4e2a\u503c\u524d\u9762\u6307\u5b9a\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ array=(a b c)\n$ array=([2]=c [0]=a [1]=b)\n\n$ days=(Sun Mon Tue Wed Thu Fri Sat)\n$ days=([0]=Sun [1]=Mon [2]=Tue [3]=Wed [4]=Thu [5]=Fri [6]=Sat)\n\u53ea\u4e3a\u67d0\u4e9b\u503c\u6307\u5b9a\u4f4d\u7f6e\uff0c\u4e5f\u662f\u53ef\u4ee5\u7684\u3002\n\nnames=(hatter [5]=duchess alice)<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0chatter\u662f\u6570\u7ec4\u76840\u53f7\u4f4d\u7f6e\uff0cduchess\u662f5\u53f7\u4f4d\u7f6e\uff0calice\u662f6\u53f7\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p>\u6ca1\u6709\u8d4b\u503c\u7684\u6570\u7ec4\u5143\u7d20\u7684\u9ed8\u8ba4\u503c\u662f\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>\u5b9a\u4e49\u6570\u7ec4\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u7528\u901a\u914d\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mp3s=( *.mp3 )<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u5c06\u5f53\u524d\u76ee\u5f55\u7684\u6240\u6709 MP3 \u6587\u4ef6\uff0c\u653e\u8fdb\u4e00\u4e2a\u6570\u7ec4\u3002<\/p>\n\n\n\n<p>\u5148\u7528declare -a\u547d\u4ee4\u58f0\u660e\u4e00\u4e2a\u6570\u7ec4\uff0c\u4e5f\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ declare -a ARRAYNAME\nread -a\u547d\u4ee4\u5219\u662f\u5c06\u7528\u6237\u7684\u547d\u4ee4\u884c\u8f93\u5165\uff0c\u5b58\u5165\u4e00\u4e2a\u6570\u7ec4\u3002\n\n$ read -a dice<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u547d\u4ee4\u5c06\u7528\u6237\u7684\u547d\u4ee4\u884c\u8f93\u5165\uff0c\u5b58\u5165\u6570\u7ec4dice\u3002<\/p>\n\n\n\n<h4>\u8bfb\u53d6\u6570\u7ec4<\/h4>\n\n\n\n<p>\u8bfb\u53d6\u6570\u7ec4\u6307\u5b9a\u4f4d\u7f6e\u7684\u6210\u5458\uff0c\u8981\u4f7f\u7528\u4e0b\u9762\u7684\u8bed\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo ${array[i]}     # i \u662f\u7d22\u5f15<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u8bed\u6cd5\u91cc\u9762\u7684\u5927\u62ec\u53f7\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u5426\u5219 Bash \u4f1a\u628a\u7d22\u5f15\u90e8\u5206[i]\u6309\u7167\u539f\u6837\u8f93\u51fa\u3002<\/p>\n\n\n\n<p>\u8bfb\u53d6\u6240\u6709\u6210\u5458<br>\n<strong>@\u548c*\u662f\u6570\u7ec4\u7684\u7279\u6b8a\u7d22\u5f15\uff0c\u8868\u793a\u8fd4\u56de\u6570\u7ec4\u7684\u6240\u6709\u6210\u5458\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ foo=(a b c d e f)\n$ echo ${foo[@]}\na b c d e f<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e24\u4e2a\u7279\u6b8a\u7d22\u5f15\u914d\u5408for\u5faa\u73af\uff0c\u5c31\u53ef\u4ee5\u7528\u6765\u904d\u5386\u6570\u7ec4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in \"${names[@]}\"; do\n  echo $i\ndone<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u8bfb\u53d6\u6570\u7ec4\u6210\u5458\u65f6\uff0c\u6ca1\u6709\u8bfb\u53d6\u6307\u5b9a\u54ea\u4e00\u4e2a\u4f4d\u7f6e\u7684\u6210\u5458\uff0c\u9ed8\u8ba4\u4f7f\u75280\u53f7\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ declare -a foo\n$ foo=A\n$ echo ${foo[0]}\nA<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0cfoo\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u8d4b\u503c\u7684\u65f6\u5019\u4e0d\u6307\u5b9a\u4f4d\u7f6e\uff0c\u5b9e\u9645\u4e0a\u662f\u7ed9foo[0]\u8d4b\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ foo=(a b c d e f)\n$ echo ${foo}\na\n$ echo $foo\na<\/code><\/pre>\n\n\n\n<p>\u8981\u60f3\u77e5\u9053\u6570\u7ec4\u7684\u957f\u5ea6\uff08\u5373\u4e00\u5171\u5305\u542b\u591a\u5c11\u6210\u5458\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u4e24\u79cd\u8bed\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>${#array[*]}\n${#array[@]}\n\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\u3002\n\n$ a[100]=foo\n\n$ echo ${#a[*]}\n1\n\n$ echo ${#a[@]}\n1<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>${!array[@]}\u6216${!array[*]}\uff0c\u53ef\u4ee5\u8fd4\u56de\u6570\u7ec4\u7684\u6210\u5458\u5e8f\u53f7\uff0c\u5373\u54ea\u4e9b\u4f4d\u7f6e\u662f\u6709\u503c\u7684\u3002\n\n$ arr=([5]=a [9]=b [23]=c)\n$ echo ${!arr[@]}\n5 9 23\n$ echo ${!arr[*]}\n5 9 23<\/code><\/pre>\n\n\n\n<p>${array[@]:position:length}\u7684\u8bed\u6cd5\u53ef\u4ee5\u63d0\u53d6\u6570\u7ec4\u6210\u5458\u3002<\/p>\n\n\n\n<p>\u6570\u7ec4\u672b\u5c3e\u8ffd\u52a0\u6210\u5458\uff0c\u53ef\u4ee5\u4f7f\u7528+=\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002\u5b83\u80fd\u591f\u81ea\u52a8\u5730\u628a\u503c\u8ffd\u52a0\u5230\u6570\u7ec4\u672b\u5c3e\u3002\u5426\u5219\uff0c\u5c31\u9700\u8981\u77e5\u9053\u6570\u7ec4\u7684\u6700\u5927\u5e8f\u53f7\uff0c\u6bd4\u8f83\u9ebb\u70e6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ foo=(a b c)\n$ echo ${foo[@]}\na b c\n\n$ foo+=(d e f)\n$ echo ${foo[@]}\na b c d e f<\/code><\/pre>\n\n\n\n<h2>sleep num<\/h2>\n\n\n\n<p>\u7a0b\u5e8f\u6682\u505cnum\u79d2<\/p>\n","protected":false},"excerpt":{"rendered":"<p>shell \u51fd\u6570 \u51fd\u6570\uff08function\uff09\u662f\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u6709\u5229\u4e8e\u4ee3\u7801\u7684\u590d\u7528\u3002\u5b83\u4e0e\u522b\u540d\uff08alias\uff09\u7684 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2021\/12\/13\/linux-7\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">11.29\u8bfe\u7a0b\u603b\u7ed3<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/145"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=145"}],"version-history":[{"count":1,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":146,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/145\/revisions\/146"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}