{"id":16385,"date":"2022-10-07T09:13:52","date_gmt":"2022-10-07T01:13:52","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=16385"},"modified":"2022-10-07T09:14:38","modified_gmt":"2022-10-07T01:14:38","slug":"learn-shell","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/learn-shell.html","title":{"rendered":"shell \u7b14\u8bb0"},"content":{"rendered":"
<\/p>\n
data='name, sex, rollno, location'\r\noldIFS=$IFS\r\nIFS=', '\r\nfor i in $data; do\r\necho $i;\r\ndone<\/pre>\n\u53d8\u91cf<\/strong><\/div>\n$* \u53c2\u6570\u88ab\u4f5c\u4e3a\u4e00\u4e2a\u5355\u8bcd (\u8fd9\u4e2a\u53d8\u91cf\u5f88\u5c11\u88ab\u4f7f\u7528)\r\n$@ \u53c2\u6570\u6309 IFS \u5206\u5272\r\n$? \u9000\u51fa\u53c2\u6570\r\n$$ \u5f53\u524d shell \u8fdb\u7a0b\u7684 pid\r\n$! \u540e\u53f0\u8fd0\u884c\u7684\u6700\u540e\u4e00\u4e2a\u8fdb\u7a0b\u7684 pid\r\n\r\n<\/pre>\n\u957f\u5ea6<\/strong><\/span><\/div>\nvar='xxxx'\r\nlength=${#var}\r\nlength=`echo -n $var | wc -m`\r\nword=`echo -n $var | wc -w`<\/pre>\n\u62fc\u63a5<\/strong><\/span><\/div>\ns1='abc'\r\ns2='def'\r\ns3=`printf \"%s%s\" $s1 $s2`\r\necho $s3<\/pre>\n\u53d8\u91cf\u9ed8\u8ba4\u503c<\/strong><\/span><\/div>\nvar=${var-'value'} # \u5982\u679c var \u6ca1\u88ab\u58f0\u660e, \u5c31\u7528 value<\/pre>\n\u5b50\u4e32<\/strong><\/span><\/div>\na='abcd'\r\necho ${a:0} # abcd\r\necho ${a:1} # bcd\r\necho ${a:1:2} # bc<\/pre>\n\u5b50\u4e32\u8868\u8fbe\u5f0f<\/strong><\/span><\/div>\n{$var#str} # \u4ece\u5f00\u5934\u5220\u9664 str\r\n{$var##str} # \u4ece\u5f00\u5934\u5220\u9664\u6700\u957f\u5339\u914d\u7684 str\r\n{$var%str} # \u4ece\u7ed3\u5c3e\u5220\u9664 str\r\n{$var%%str} # \u4ece\u7ed3\u5c3e\u5220\u9664\u6700\u957f\u5339\u914d\u7684 str\r\n\r\ndir=\/etc\/httpd\/httpd.conf\r\necho ${dir#\/*\/} # httpd\/httpd.conf\r\necho ${dir##\/*\/} # httpd.conf\r\n\r\necho ${dir%\/*\/} # \u6ca1\u6709\u5220\u9664\u4efb\u4f55\r\necho ${dir%\/*} # \/etc\/httpd\r\necho ${dir%%\/*} # \u5168\u90e8\u88ab\u5220\u9664 \u4e3a\u7a7a<\/pre>\n\u5b57\u7b26\u4e32\u66ff\u6362 (\u53ef\u4ee5\u6709\u6b63\u5219\u8868\u8fbe\u5f0f)<\/strong><\/span><\/div>\n\nstr='abc 123 def abc'\r\necho ${str\/abc \/def} # def123 def abc\r\necho ${str\/\/abc\/def} # def 123 def def\r\necho ${str\/\/[0-9]\/} # abc def abc<\/pre>\n\u5224\u65ad<\/strong><\/div>\n\u5b57\u7b26\u4e32<\/strong><\/span><\/div>\n[ $var == \"xxx\" ]\r\n[ $var != \"xxx\" ]\r\n[ -n STRING ] # the length of STRING is nonzero\r\n[ -z STRING ] # the length of STRING is zero\r\n<\/pre>\n\u6570\u5b57<\/strong><\/span><\/div>\n[ $num -eq 10 ]\r\n[ $num -ne 10 ]\r\n[ $num -lt 10 ]\r\n[ $num -le 10 ]\r\n[ $num -gt 10 ]\r\n[ $num -ge 10 ]\r\n<\/pre>\n\u6587\u4ef6<\/strong><\/span><\/div>\n[ -d $FILE_NAME ] # dir\r\n[ -e $FILE_NAME ] # exist\r\n[ -f $FILE_NAME ] # regular file\r\n[ -L $FILE_NAME ] # symbol link\r\n[ -r $FILE_NAME ]\r\n[ -w $FILE_NAME ]\r\n[ -x $FILE_NAME ]\r\n[ -S $FILE_NAME ] # socket\r\n[ -b $FILE_NAME ] # block\r\n[ -c $FILE_NAME ] # char<\/pre>\nRONDOM<\/strong><\/div>\nshell \u7684 $((RANDOM)) \u4f1a\u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u503c<\/p>\n
if [ ${#} -eq 0 ]; then\r\necho $((RANDOM))\r\nelif [ $1 == '--help' -o $1 == '-h' ]; then\r\necho \"usage: random num1, num2 [num1, num2)\"\r\nelif [ ${#} -eq 1 ]; then\r\necho $((RANDOM%$1))\r\nelse\r\necho $((RANDOM%(($2-$1))+$1))\r\nfi<\/pre>\n\u6570\u5b66<\/strong><\/div>\necho $((100 \/ 3))\r\n\r\ni=8\r\necho $((i + 12))\r\n\r\ni=8\r\nlet i=5+3 # '=' \u4e24\u4fa7\u4e0d\u5141\u8bb8\u6709\u7a7a\u683c\r\ni=$((i+1))\r\nlet i++<\/pre>\n\u63a7\u5236\u6d41<\/strong><\/div>\n# if\r\nif [ xxx ]; then\r\nxxx\r\nelif [ xxx ]; then\r\nxxx\r\nelse\r\nxxx\r\nfi\r\n\r\nif [ xxx ] && [ xxx ]; then\r\nxxx\r\nfi\r\n\r\n# while\r\nwhile [ xxx ]; do\r\nxxx\r\ndone\r\n\r\nwhile read line; do\r\necho $line;\r\ndone < input.txt;\r\n\r\n# for\r\nfor i in `seq 1 10`; do\r\necho $i;\r\ndone\r\n\r\nfor x in one two three four; do\r\necho number $x\r\ndone\r\n\r\n# case\r\nx=\"start\"\r\ncase $x in\r\nstart)\r\necho 'start'\r\n;;\r\nstop)\r\necho \"stop\"\r\n;;\r\n*)\r\necho stop\r\n;;\r\nesac\r\n\r\nx=\"restart\"\r\ncase $x in\r\nstart|restart)\r\necho 'start'\r\n;;\r\nstop)\r\necho \"stop\"\r\n;;\r\n*)\r\necho stop\r\n;;\r\nesac<\/pre>\n\u51fd\u6570<\/strong><\/div>\nmyvar=\"hello\"\r\nmyfunc() {\r\nmyvar=\"one two three\"\r\nfor x in $myvar; do\r\necho $x\r\ndone\r\n}\r\nmyfunc # \u6267\u884c\u51fd\u6570\r\necho $myvar $x # one two three (\u8bf4\u660e\u53d8\u91cf\u662f\u5168\u5c40\u7684)\r\n<\/pre>\n<\/div>\n\nmyvar=\"hello\"\r\nmyfunc() {\r\nlocal x\r\nlocal myvar=\"one two three\"\r\nfor x in $myvar; do\r\necho $x\r\ndone\r\n}\r\nmyfunc\r\necho $myvar $x # hello ($x \u6ca1\u6709\u8f93\u51fa)\r\n<\/pre>\n<\/div>\n\nrepeat() {\r\nwhile true; do\r\n$@ && return\r\nsleep 5\r\ndone\r\n}<\/pre>\n\u5b50 shell<\/strong><\/div>\n\u5b50 shell \u5c31\u662f () \u91cc\u9762\u7684\u5185\u5bb9\uff0c\u76f8\u5f53\u4e8e\u91cd\u65b0\u5f00\u542f\u4e00\u4e2a shell\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f \u5b50 shell \u91cc\u9762\u505a\u7684\u4e8b\u60c5\u4e0d\u4f1a\u5f71\u54cd\u5230\u5f53\u524d\u7684 shell\uff0c\u6bd4\u5982\u5b9a\u4e49\u53d8\u91cf\uff0ccd \u5230\u522b\u7684\u8def\u5f84..<\/p>\n
pwd;\r\n(cd \/bin; ls);\r\npwd;<\/pre>\n<\/div>\n\u6570\u7ec4<\/strong><\/div>\ndeclare -A arr\r\narr=([index1]='abc' [index2]='def')\r\necho ${arr[index1]}\r\necho ${arr['index1']}\r\n\r\narr[index3]='xyz'\r\necho ${arr[index3]}\r\n<\/pre>\nfor i in ${arr[@]}; do\r\necho $i; # abc def xyz\r\ndone\r\n\r\nfor i in ${!arr[@]}; do\r\necho $i; # index1 index2 index3\r\ndone<\/pre>\n\u83b7\u53d6\u547d\u4ee4\u53c2\u6570<\/strong><\/div>\nwhile getopts xyz opt; do\r\ncase $opt in\r\nx)\r\necho 'x'\r\n;;\r\ny)\r\necho 'y'\r\n;;\r\nz)\r\necho 'z'\r\n;;\r\n?)\r\necho 'invalid'\r\n;;\r\nesac\r\ndone<\/pre>\n.\/run -xyz \uff08\u6267\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff09\r\n<\/pre>\nwhile getopts x:y:z: opt; do\r\ncase $opt in\r\nx)\r\necho 'x' $OPTARG\r\n;;\r\ny)\r\necho 'y' $OPTARG\r\n;;\r\nz)\r\necho 'z' $OPTARG\r\n;;\r\n*)\r\necho 'invalid'\r\n;;\r\nesac\r\ndone\r\n<\/pre>\n.\/run -x abc -y def -z ghi\r\n<\/pre>\n\n\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/sunznx.github.io\/linux\/shell<\/a><\/p>\n