{"id":103483,"date":"2024-09-02T02:32:12","date_gmt":"2024-09-01T18:32:12","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=103483"},"modified":"2024-09-02T02:32:12","modified_gmt":"2024-09-01T18:32:12","slug":"shell-script-variable","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/shell-script-variable.html","title":{"rendered":"\u8be6\u89e3\uff1aShell\u811a\u672c\u53d8\u91cf\u5224\u65ad\u53c2\u6570\u547d\u4ee4"},"content":{"rendered":"
<\/p>\n
\r\n$n \u4f20\u9012\u7ed9\u811a\u672c\u6216\u51fd\u6570\u7684\u53c2\u6570\u3002n \u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u8868\u793a\u7b2c\u51e0\u4e2a\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f$1\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f$2\r\n$? \u4e0a\u4e2a\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\uff0c\u6216\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002\u6210\u529f\u8fd4\u56de0\uff0c\u5931\u8d25\u8fd4\u56de1\r\n$# \u4f20\u9012\u7ed9\u811a\u672c\u6216\u51fd\u6570\u7684\u53c2\u6570\u4e2a\u6570\r\n$* \u6240\u6709\u8fd9\u4e9b\u53c2\u6570\u90fd\u88ab\u53cc\u5f15\u53f7\u5f15\u4f4f\u3002\u82e5\u4e00\u4e2a\u811a\u672c\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c$*\u7b49\u4e8e$1$2\r\n$0 \u6b63\u5728\u88ab\u6267\u884c\u547d\u4ee4\u7684\u540d\u5b57\u3002\u5bf9\u4e8eshell\u811a\u672c\u800c\u8a00\uff0c\u8fd9\u662f\u88ab\u6fc0\u6d3b\u547d\u4ee4\u7684\u8def\u5f84\r\n$@ \u88ab\u53cc\u5f15\u53f7(\u201d \u201c)\u5305\u542b\u65f6\uff0c\u4e0e $* \u7a0d\u6709\u4e0d\u540c\u3002\u82e5\u4e00\u4e2a\u811a\u672c\u63a5\u6536\u5230\u4e24\u4e2a\u53c2\u6570\uff0c$@\u7b49\u4ef7\u4e8e$1$2\r\n$$ \u5f53\u524dshell\u7684\u8fdb\u7a0b\u53f7\u3002\u5bf9\u4e8eshell\u811a\u672c\uff0c\u8fd9\u662f\u5176\u6b63\u5728\u6267\u884c\u65f6\u7684\u8fdb\u7a0bID\r\n$! \u524d\u4e00\u4e2a\u540e\u53f0\u547d\u4ee4\u7684\u8fdb\u7a0b\u53f7\r\n<\/pre>\n\u6587\u4ef6\u6216\u76ee\u5f55\u5224\u65ad<\/strong><\/span><\/div>\n\r\n-b file \u82e5\u6587\u4ef6\u5b58\u5728\u4e14\u662f\u4e00\u4e2a\u5757\u7279\u6b8a\u6587\u4ef6,\u5219\u4e3a\u771f\r\n-c file \u82e5\u6587\u4ef6\u5b58\u5728\u4e14\u662f\u4e00\u4e2a\u5b57\u7b26\u7279\u6b8a\u6587\u4ef6,\u5219\u4e3a\u771f\r\n-d file \u82e5\u6587\u4ef6\u5b58\u5728\u4e14\u662f\u4e00\u4e2a\u76ee\u5f55,\u5219\u4e3a\u771f\r\n-e file \u82e5\u6587\u4ef6\u5b58\u5728,\u5219\u4e3a\u771f\r\n-f file \u82e5\u6587\u4ef6\u5b58\u5728\u4e14\u662f\u4e00\u4e2a\u89c4\u5219\u6587\u4ef6,\u5219\u4e3a\u771f\r\n-g file \u82e5\u6587\u4ef6\u5b58\u5728\u4e14\u8bbe\u7f6e\u4e86SGID\u4f4d\u7684\u503c,\u5219\u4e3a\u771f\r\n-h file \u82e5\u6587\u4ef6\u662f\u8f6f\u94fe\u63a5,\u5219\u4e3a\u771f\r\n-k file \u82e5\u6587\u4ef6\u5b58\u5728\u4e14\u8bbe\u7f6e\u4e86\u201dsticky\u201d\u4f4d\u7684\u503c\r\n-L file \u82e5\u6587\u4ef6\u4e3a\u7b26\u53f7\u94fe\u63a5,\u5219\u4e3a\u771f\r\n-p file \u82e5\u6587\u4ef6\u5b58\u5728\u4e14\u4e3a\u4e00\u5df2\u547d\u540d\u7ba1\u9053,\u5219\u4e3a\u771f\r\n-r file \u82e5\u6587\u4ef6\u53ef\u8bfb,\u5219\u4e3a\u771f\r\n-s file \u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728,\u4e14\u662f\u5426\u4e3a\u975e\u7a7a,\u4e0d\u4e3a\u7a7a,\u5219\u4e3a\u771f\r\n-S file\t\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728,\u4e14\u662f\u5426\u4e3a\u5957\u63a5\u5b57\u6587\u4ef6\r\n-t file \u6587\u4ef6\u63cf\u8ff0\u7b26(\u9ed8\u8ba4\u4e3a1)\u6307\u5b9a\u7684\u8bbe\u5907\u4e3a\u7ec8\u7aef\u65f6\u4e3a\u771f\r\n-u file \u82e5\u6587\u4ef6\u5b58\u5728\u4e14\u8bbe\u7f6e\u4e86SUID\u4f4d,\u5219\u4e3a\u771f\r\n-w file \u82e5\u6587\u4ef6\u53ef\u5199,\u5219\u4e3a\u771f\r\n-x file \u82e5\u6587\u4ef6\u53ef\u6267\u884c,\u5219\u4e3a\u771f\r\n[ file1 -nt file2 ] \u82e5file1\u6bd4file2\u65b0,\u6216file1\u5b58\u5728\u4f46file2\u4e0d\u5b58\u5728,\u5219\u4e3a\u771f\r\n[ file1 -ot file2 ] \u82e5file1\u6bd4file2\u8001,\u6216file2\u5b58\u5728\u4f46file1\u4e0d\u5b58\u5728,\u5219\u4e3a\u771f\r\n[ file1 -ef file2 ] \u82e5file1\u548cfile2\u6307\u5411\u76f8\u540c\u7684\u8bbe\u5907\u548c\u8282\u70b9\u53f7\u5219\u8fd4\u56de,\u4e3a\u771f\r\n<\/pre>\n\u6574\u6570\u5224\u65ad<\/strong><\/span><\/div>\n\r\n-eq \u4e24\u6570\u76f8\u7b49,\u5219\u4e3a\u771f \u4f8b:if [ \"$a\" -eq \"$b\" ]\r\n-ne \u4e24\u6570\u4e0d\u76f8\u7b49,\u5219\u4e3a\u771f \u4f8b:if [ \"$a\" -ne \"$b\" ]\r\n-gt a\u5927\u4e8eb,\u5219\u4e3a\u771f \u4f8b:if [ \"$a\" -gt \"$b\" ]\r\n-ge \u5927\u4e8e\u6216\u7b49\u4e8e,\u5219\u4e3a\u771f \u4f8b:if [ \"$a\" -ge \"$b\" ]\r\n-lt a\u5c0f\u4e8eb,\u5219\u4e3a\u771f \u4f8b:if [ \"$a\" -lt \"$b\" ]\r\n-le a\u5c0f\u4e8e\u6216\u7b49\u4e8eb,\u5219\u4e3a\u771f \u4f8b:if [ \"$a\" -le \"$b\" ]\r\n\r\n< \u5c0f\u4e8e(\u9700\u53cc\u62ec\u53f7) \u4f8b:((\"$a\" < \"$b\"))\r\n<= \u5c0f\u4e8e\u7b49\u4e8e(\u9700\u53cc\u62ec\u53f7) \u4f8b:((\"$a\" <= \"$b\")) \r\n> \u5927\u4e8e(\u9700\u53cc\u62ec\u53f7) \u4f8b:((\"$a\" > \"$b\"))\r\n>= \u5927\u4e8e\u7b49\u4e8e(\u9700\u53cc\u62ec\u53f7) \u4f8b:((\"$a\" >= \"$b\"))\r\n\u5c0f\u6570\u636e\u6bd4\u8f83\u53ef\u4f7f\u7528AWK\u3002\r\n<\/pre>\n\u903b\u8f91\u8fd0\u7b97\u5224\u65ad\u7b26<\/strong><\/span><\/div>\n\r\n! [ ! false ] \u8fd4\u56detrue \u903b\u8f91\u5426,\u6761\u4ef6\u4e3a\u5047,\u7ed3\u679c\u4e3a\u771f\r\n-a [ $a -lt 2 -a $b -gt 5 ] \u8fd4\u56detrue \u903b\u8f91\u4e0e,\u4e24\u4e2a\u8868\u8fbe\u5f0f\u90fd\u4e3a\u771f,\u5219\u4e3a\u771f\r\n-o [ $a -lt 2 -o $b -gt 5 ] \u8fd4\u56detrue \u903b\u8f91\u6216,\u53ea\u8981\u6709\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e3atrue,\u5219\u4e3a\u771f\r\n\r\n[ ] || [ ] \u7528OR\u6765\u5408\u5e76\u4e24\u4e2a\u6761\u4ef6\r\n[ ] && [ ] \u7528AND\u6765\u5408\u5e76\u4e24\u4e2a\u6761\u4ef6\r\n<\/pre>\n\u5b57\u7b26\u4e32\u5224\u65ad<\/strong><\/span><\/div>\n\r\n== \u82e5\u4e24\u4e2a\u5b57\u7b26\u4e32\u76f8\u540c\u5219\u4e3a\u771f,\u4e0e=\u7b49\u4ef7 \u4f8b:[ \"str1\" = \"str2\" ]\r\n!= \u82e5\u5b57\u7b26\u4e32\u4e0d\u76f8\u540c\u5219\u4e3a\u771f \u4f8b:[ \"str1\" != \"str2\" ]\r\n< \u82e5str1\u5b57\u5178\u6392\u5e8f\u5728str2\u524d\u5219\u4e3a\u771f \u4f8b:[[ \"str1\" < \"str2\" ]] > \u82e5str1\u5b57\u5178\u6392\u5e8f\u5728str2\u540e\u5219\u4e3a\u771f \u4f8b:[ \"str1\" \\> \"str2\" ]\r\n-n \u82e5str\u957f\u5ea6\u975e\u96f6\u5219\u4e3a\u771f,\u5373\u975e\u7a7a \u4f8b:[ -n \"str1\" ]\r\n-z \u82e5file\u957f\u5ea6\u4e3a\u96f6,\u5373\u7a7a,\u5219\u4e3a\u771f \u4f8b:[ -z \"str1\" ]\r\n<\/pre>\n\u6ce8\u610f:\u5728[]\u7ed3\u6784\u4e2d\u201d< \u201c\u9700\u8981\u88ab\u8f6c\u4e49\uff0c\u4f8b\u5982:[ \"str1\" \/< \"str2\" ]\uff0c\u5728\u53cc\u4e2d\u62ec\u53f7\u4e2d\u5219\u65e0\u9700\u8f6c\u4e49\u3002\n\n\u603b\u7ed3:\u4f7f\u7528-n\u5728[]\u7ed3\u6784\u4e2d\u6d4b\u8bd5\uff0c\u5fc5\u987b\u8981\u7528\"\"\u628a\u53d8\u91cf\u5f15\u8d77\u6765\uff0c\u4f7f\u7528\u4e00\u4e2a\u672a\u88ab\"\"\u7684\u5b57\u7b26\u4e32\uff0c\u8bf7\u4f7f\u7528 ! -z \uff0c\u5982\u679c\u4f7f\u7528\u672a\u88ab\u53cc\u5f15\u53f7\u53d8\u91cf\uff0c\u867d\u7136\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u8fd9\u5e76\u4e0d\u5b89\u5168\uff0c\u4e60\u60ef\u4e8e\u4f7f\u7528\u53cc\u5f15\u53f7\u62ec\u4f4f\u53d8\u91cf\u6d4b\u8bd5\u5b57\u7b26\u4e32\u662f\u4e00\u79cd\u826f\u597d\u7684\u4e60\u60ef\u3002\n\u53e6\u5916\uff0c[[ ]] \u7ed3\u6784\u6bd4[ ]\u7ed3\u6784\u66f4\u52a0\u901a\u7528\u3002\n\n\n\n\n\n
\n\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/renwole.com\/archives\/1204<\/a><\/p>\n