{"id":158816,"date":"2019-10-03T08:00:29","date_gmt":"2019-10-03T00:00:29","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=158816"},"modified":"2019-09-23T10:01:36","modified_gmt":"2019-09-23T02:01:36","slug":"linux-variable","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-variable.html","title":{"rendered":"\u5982\u4f55\u5728Linux \u4e2d\u4f7f\u7528\u53d8\u91cf"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u53d8\u91cf\u901a\u5e38\u770b\u8d77\u6765\u50cf $var \u8fd9\u6837\uff0c\u4f46\u5b83\u4eec\u4e5f\u6709 $1\u3001$*\u3001$? \u548c $$ \u8fd9\u79cd\u5f62\u5f0f\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u6240\u6709\u8fd9\u4e9b $ \u503c\u53ef\u4ee5\u544a\u8bc9\u4f60\u4ec0\u4e48\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u6709\u8bb8\u591a\u91cd\u8981\u7684\u503c\u90fd\u5b58\u50a8\u5728 Linux \u7cfb\u7edf\u4e2d\uff0c\u6211\u4eec\u79f0\u4e3a\u201c\u53d8\u91cf\u201d\uff0c\u4f46\u5b9e\u9645\u4e0a\u53d8\u91cf\u6709\u51e0\u79cd\u7c7b\u578b\uff0c\u5e76\u4e14\u4e00\u4e9b\u6709\u8da3\u7684\u547d\u4ee4\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4f7f\u7528\u5b83\u4eec\u3002\u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u7814\u7a76\u4e86\u73af\u5883\u53d8\u91cf\u4ee5\u53ca\u5b83\u4eec\u5b9a\u4e49\u5728\u4f55\u5904\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u6765\u770b\u4e00\u770b\u5728\u547d\u4ee4\u884c\u548c\u811a\u672c\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u3002<\/p>\n

\u7528\u6237\u53d8\u91cf<\/strong><\/div>\n

\u867d\u7136\u5728\u547d\u4ee4\u884c\u4e2d\u8bbe\u7f6e\u53d8\u91cf\u975e\u5e38\u5bb9\u6613\uff0c\u4f46\u662f\u6709\u4e00\u4e9b\u6709\u8da3\u7684\u6280\u5de7\u3002\u8981\u8bbe\u7f6e\u53d8\u91cf\uff0c\u4f60\u53ea\u9700\u8fd9\u6837\u505a\uff1a<\/p>\n

\r\n$ myvar=11\r\n$ myvar2=\"eleven\"\r\n<\/pre>\n

\u8981\u663e\u793a\u8fd9\u4e9b\u503c\uff0c\u53ea\u9700\u8fd9\u6837\u505a\uff1a<\/p>\n

\r\n$ echo $myvar\r\n11\r\n$ echo $myvar2\r\neleven\r\n<\/pre>\n

\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u8981\u9012\u589e\u4e00\u4e2a\u6570\u5b57\u53d8\u91cf\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4efb\u610f\u4e00\u4e2a\u547d\u4ee4\uff1a<\/p>\n

\r\n$ myvar=$((myvar+1))\r\n$ echo $myvar\r\n12\r\n$ ((myvar=myvar+1))\r\n$ echo $myvar\r\n13\r\n$ ((myvar+=1))\r\n$ echo $myvar\r\n14\r\n$ ((myvar++))\r\n$ echo $myvar\r\n15\r\n$ let \"myvar=myvar+1\"\r\n$ echo $myvar\r\n16\r\n$ let \"myvar+=1\"\r\n$ echo $myvar\r\n17\r\n$ let \"myvar++\"\r\n$ echo $myvar\r\n18\r\n<\/pre>\n

\u4f7f\u7528\u5176\u4e2d\u7684\u4e00\u4e9b\uff0c\u4f60\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n

\r\n$ myvar0=0\r\n$ ((myvar0++))\r\n$ echo $myvar0\r\n1\r\n$ ((myvar0+=10))\r\n$ echo $myvar0\r\n11\r\n<\/pre>\n

\u901a\u8fc7\u8fd9\u4e9b\u9009\u9879\uff0c\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u5b83\u4eec\u662f\u5bb9\u6613\u8bb0\u5fc6\u3001\u4f7f\u7528\u65b9\u4fbf\u7684\u3002
\n\u4f60\u4e5f\u53ef\u4ee5\u5220\u9664\u4e00\u4e2a\u53d8\u91cf \u2013 \u8fd9\u610f\u5473\u7740\u6ca1\u6709\u5b9a\u4e49\u5b83\u3002<\/p>\n

\r\n$ unset myvar\r\n$ echo $myvar\r\n<\/pre>\n

\u53e6\u4e00\u4e2a\u6709\u8da3\u7684\u9009\u9879\u662f\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u53d8\u91cf\u5e76\u5c06\u5176\u8bbe\u4e3a\u53ea\u8bfb\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u53d8\u91cf\u4e00\u65e6\u8bbe\u7f6e\u4e3a\u53ea\u8bfb\uff0c\u5b83\u7684\u503c\u5c31\u4e0d\u80fd\u6539\u53d8\uff08\u9664\u975e\u4e00\u4e9b\u975e\u5e38\u590d\u6742\u7684\u547d\u4ee4\u884c\u9b54\u6cd5\u624d\u53ef\u4ee5\uff09\u3002\u8fd9\u610f\u5473\u7740\u4f60\u4e5f\u4e0d\u80fd\u5220\u9664\u5b83\u3002<\/p>\n

\r\n$ readonly myvar3=1\r\n$ echo $myvar3\r\n1\r\n$ ((myvar3++))\r\n-bash: myvar3: readonly variable\r\n$ unset myvar3\r\n-bash: unset: myvar3: cannot unset: readonly variable\r\n<\/pre>\n

\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u8bbe\u7f6e\u548c\u9012\u589e\u9009\u9879\u4e2d\u6765\u8d4b\u503c\u548c\u64cd\u4f5c\u811a\u672c\u4e2d\u7684\u53d8\u91cf\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u975e\u5e38\u6709\u7528\u7684\u5185\u90e8\u53d8\u91cf\u53ef\u4ee5\u7528\u4e8e\u5728\u811a\u672c\u4e2d\u3002\u6ce8\u610f\uff0c\u4f60\u65e0\u6cd5\u91cd\u65b0\u8d4b\u503c\u6216\u589e\u52a0\u5b83\u4eec\u7684\u503c\u3002<\/p>\n

\u5185\u90e8\u53d8\u91cf<\/strong><\/div>\n

\u5728\u811a\u672c\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5f88\u591a\u53d8\u91cf\u6765\u8ba1\u7b97\u53c2\u6570\u5e76\u663e\u793a\u6709\u5173\u811a\u672c\u672c\u8eab\u7684\u4fe1\u606f\u3002
\n$1\u3001$2\u3001$3 \u7b49\u8868\u793a\u811a\u672c\u7684\u7b2c\u4e00\u4e2a\u3001\u7b2c\u4e8c\u4e2a\u3001\u7b2c\u4e09\u4e2a\u7b49\u53c2\u6570\u3002
\n$# \u8868\u793a\u53c2\u6570\u7684\u6570\u91cf\u3002
\n$* \u8868\u793a\u6240\u6709\u53c2\u6570\u3002
\n$0 \u8868\u793a\u811a\u672c\u7684\u540d\u79f0\u3002
\n$? \u8868\u793a\u5148\u524d\u8fd0\u884c\u7684\u547d\u4ee4\u7684\u8fd4\u56de\u7801\uff080 \u4ee3\u8868\u6210\u529f\uff09\u3002
\n$$ \u663e\u793a\u811a\u672c\u7684\u8fdb\u7a0b ID\u3002
\n$PPID \u663e\u793a shell \u7684\u8fdb\u7a0b ID\uff08\u811a\u672c\u7684\u7236\u8fdb\u7a0b\uff09\u3002
\n\u5176\u4e2d\u4e00\u4e9b\u53d8\u91cf\u4e5f\u9002\u7528\u4e8e\u547d\u4ee4\u884c\uff0c\u4f46\u663e\u793a\u76f8\u5173\u4fe1\u606f\uff1a
\n$0 \u663e\u793a\u4f60\u6b63\u5728\u4f7f\u7528\u7684 shell \u7684\u540d\u79f0\uff08\u4f8b\u5982\uff0c-bash\uff09\u3002
\n$$ \u663e\u793a shell \u7684\u8fdb\u7a0b ID\u3002
\n$PPID \u663e\u793a shell \u7684\u7236\u8fdb\u7a0b\u7684\u8fdb\u7a0b ID\uff08\u5bf9\u6211\u6765\u8bf4\uff0c\u662f sshd\uff09\u3002
\n\u4e3a\u4e86\u67e5\u770b\u5b83\u4eec\u7684\u7ed3\u679c\uff0c\u5982\u679c\u6211\u4eec\u5c06\u6240\u6709\u8fd9\u4e9b\u53d8\u91cf\u90fd\u653e\u5165\u4e00\u4e2a\u811a\u672c\u4e2d\uff0c\u6bd4\u5982\uff1a<\/p>\n

\r\n#!\/bin\/bash\r\necho $0\r\necho $1\r\necho $2\r\necho $#\r\necho $*\r\necho $?\r\necho $$\r\necho $PPID\r\n<\/pre>\n

\u5f53\u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u811a\u672c\u65f6\uff0c\u6211\u4eec\u4f1a\u770b\u5230\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n

\r\n$ tryme one two three\r\n\/home\/shs\/bin\/tryme     < == \u811a\u672c\u540d\u79f0\r\none                     <== \u7b2c\u4e00\u4e2a\u53c2\u6570\r\ntwo                     <== \u7b2c\u4e8c\u4e2a\u53c2\u6570\r\n3                       <== \u53c2\u6570\u7684\u4e2a\u6570\r\none two three           <== \u6240\u6709\u7684\u53c2\u6570\r\n0                       <== \u4e0a\u4e00\u6761 echo \u547d\u4ee4\u7684\u8fd4\u56de\u7801\r\n10410                   <== \u811a\u672c\u7684\u8fdb\u7a0b ID\r\n10109                   <== \u7236\u8fdb\u7a0b ID\r\n<\/pre>\n

\u5982\u679c\u6211\u4eec\u5728\u811a\u672c\u8fd0\u884c\u5b8c\u6bd5\u540e\u68c0\u67e5 shell \u7684\u8fdb\u7a0b ID\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b83\u4e0e\u811a\u672c\u4e2d\u663e\u793a\u7684 PPID \u76f8\u5339\u914d\uff1a<\/p>\n

\r\n$ echo $$\r\n10109                   < == shell \u7684\u8fdb\u7a0b ID\r\n<\/pre>\n

\u5f53\u7136\uff0c\u6bd4\u8d77\u7b80\u5355\u5730\u663e\u793a\u5b83\u4eec\u7684\u503c\uff0c\u66f4\u6709\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u5b83\u4eec\u3002\u6211\u4eec\u6765\u770b\u4e00\u770b\u5b83\u4eec\u53ef\u80fd\u7684\u7528\u5904\u3002
\n\u68c0\u67e5\u662f\u5426\u5df2\u63d0\u4f9b\u53c2\u6570\uff1a<\/p>\n

\r\nif [ $# == 0 ]; then\r\n    echo \"$0 filename\"\r\n    exit 1\r\nfi\r\n<\/pre>\n

\u68c0\u67e5\u7279\u5b9a\u8fdb\u7a0b\u662f\u5426\u6b63\u5728\u8fd0\u884c\uff1a<\/p>\n

\r\nps -ef | grep apache2 > \/dev\/null\r\nif [ $? != 0 ]; then\r\n    echo Apache is not running\r\n    exit\r\nfi\r\n<\/pre>\n

\u5728\u5c1d\u8bd5\u8bbf\u95ee\u6587\u4ef6\u4e4b\u524d\u9a8c\u8bc1\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1a<\/p>\n

\r\nif [ $# -lt 2 ]; then\r\n    echo \"Usage: $0 lines filename\"\r\n    exit 1\r\nfi\r\nif [ ! -f $2 ]; then\r\n    echo \"Error: File $2 not found\"\r\n    exit 2\r\nelse\r\n    head -$1 $2\r\nfi\r\n<\/pre>\n

\u5728\u4e0b\u9762\u7684\u5c0f\u811a\u672c\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u662f\u5426\u63d0\u4f9b\u4e86\u6b63\u786e\u6570\u91cf\u7684\u53c2\u6570\u3001\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5426\u4e3a\u6570\u5b57\uff0c\u4ee5\u53ca\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4ee3\u8868\u7684\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n

\r\n#!\/bin\/bash\r\nif [ $# -lt 2 ]; then\r\n    echo \"Usage: $0 lines filename\"\r\n    exit 1\r\nfi\r\nif [[ $1 != [0-9]* ]]; then\r\n    echo \"Error: $1 is not numeric\"\r\n    exit 2\r\nfi\r\nif [ ! -f $2 ]; then\r\n    echo \"Error: File $2 not found\"\r\n    exit 3\r\nelse\r\n    echo top of file\r\n    head -$1 $2\r\nfi\r\n<\/pre>\n
\u91cd\u547d\u540d\u53d8\u91cf<\/strong><\/div>\n

\u5728\u7f16\u5199\u590d\u6742\u7684\u811a\u672c\u65f6\uff0c\u4e3a\u811a\u672c\u7684\u53c2\u6570\u6307\u5b9a\u540d\u79f0\u901a\u5e38\u5f88\u6709\u7528\uff0c\u800c\u4e0d\u662f\u7ee7\u7eed\u5c06\u5b83\u4eec\u79f0\u4e3a $1\u3001$2 \u7b49\u3002\u7b49\u5230\u7b2c 35 \u884c\uff0c\u9605\u8bfb\u4f60\u811a\u672c\u7684\u4eba\u53ef\u80fd\u5df2\u7ecf\u5fd8\u4e86 $2 \u8868\u793a\u4ec0\u4e48\u3002\u5982\u679c\u4f60\u5c06\u4e00\u4e2a\u91cd\u8981\u53c2\u6570\u7684\u503c\u8d4b\u7ed9 $filename \u6216 $numlines\uff0c\u90a3\u4e48\u4ed6\u5c31\u4e0d\u5bb9\u6613\u5fd8\u8bb0\u3002<\/p>\n

\r\n#!\/bin\/bash\r\nif [ $# -lt 2 ]; then\r\n    echo \"Usage: $0 lines filename\"\r\n    exit 1\r\nelse\r\n    numlines=$1\r\n    filename=$2\r\nfi\r\nif [[ $numlines != [0-9]* ]]; then\r\n    echo \"Error: $numlines is not numeric\"\r\n    exit 2\r\nfi\r\nif [ ! -f $ filename]; then\r\n    echo \"Error: File $filename not found\"\r\n    exit 3\r\nelse\r\n    echo top of file\r\n    head -$numlines $filename\r\nfi\r\n<\/pre>\n

\u5f53\u7136\uff0c\u8fd9\u4e2a\u793a\u4f8b\u811a\u672c\u53ea\u662f\u8fd0\u884c head \u547d\u4ee4\u6765\u663e\u793a\u6587\u4ef6\u4e2d\u7684\u524d x \u884c\uff0c\u4f46\u5b83\u7684\u76ee\u7684\u662f\u663e\u793a\u5982\u4f55\u5728\u811a\u672c\u4e2d\u4f7f\u7528\u5185\u90e8\u53c2\u6570\u6765\u5e2e\u52a9\u786e\u4fdd\u811a\u672c\u8fd0\u884c\u826f\u597d\uff0c\u6216\u5728\u5931\u8d25\u65f6\u6e05\u6670\u5730\u77e5\u9053\u5931\u8d25\u539f\u56e0\u3002<\/p>\n

via: https:\/\/www.networkworld.com\/article\/3387154\/working-with-variables-on-linux.html#tk.rss_all<\/p>\n

\u4f5c\u8005\uff1aSandra Henry-Stocker \u9009\u9898\uff1alujun9972 \u8bd1\u8005\uff1aMjSeven \u6821\u5bf9\uff1awxy<\/p>\n

\u672c\u6587\u7531 LCTT \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd \u8363\u8a89\u63a8\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6709\u8bb8\u591a\u91cd\u8981\u7684\u503c\u90fd\u5b58\u50a8\u5728 Linux \u7cfb\u7edf\u4e2d\uff0c\u6211\u4eec\u79f0\u4e3a\u201c\u53d8\u91cf\u201d\uff0c\u4f46\u5b9e\u9645\u4e0a\u53d8\u91cf\u6709\u51e0\u79cd\u7c7b\u578b\uff0c\u5e76\u4e14\u4e00\u4e9b\u6709\u8da3\u7684\u547d\u4ee4\u53ef\u4ee5\u5e2e […]<\/p>\n","protected":false},"author":1920,"featured_media":158821,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-158816","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\/158816","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\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=158816"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/158816\/revisions"}],"predecessor-version":[{"id":159088,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/158816\/revisions\/159088"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/158821"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=158816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=158816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=158816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}