{"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":"

\"learn_shell_00\"<\/p>\n

\u901a\u914d\u7b26<\/strong><\/div>\n
    \n
  1. \u6ca1\u6709 '.'<\/li>\n
  2. [Aa] \u8868\u793a A \u6216\u8005 a<\/li>\n
  3. ? \u8868\u793a\u4efb\u610f\u5b57\u7b26<\/li>\n
  4. * \u8868\u793a\u4efb\u610f\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n
    IFS<\/strong><\/div>\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>\n
    var='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>\n
    s1='abc'\r\ns2='def'\r\ns3=`printf \"%s%s\" $s1 $s2`\r\necho $s3<\/pre>\n
    \u53d8\u91cf\u9ed8\u8ba4\u503c<\/strong><\/span><\/div>\n
    var=${var-'value'} # \u5982\u679c var \u6ca1\u88ab\u58f0\u660e, \u5c31\u7528 value<\/pre>\n
    \u5b50\u4e32<\/strong><\/span><\/div>\n
    a='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
    \n
    str='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>\n
    RONDOM<\/strong><\/div>\n

    shell \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>\n
    echo $((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>\n
    myvar=\"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
    \n
    myvar=\"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
    \n
    repeat() {\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>\n
    declare -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>\n
    for 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>\n
    while 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>\n
    while 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

    \u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/learn-shell.html<\/a>\u7f16\u8f91\uff1a\u632f\u7fc5\u98de\u7fd4\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

    \u901a\u914d\u7b26 \u6ca1\u6709 ‘.’ [Aa] \u8868\u793a A \u6216\u8005 a ? \u8868\u793a\u4efb\u610f\u5b57\u7b26 * \u8868\u793a\u4efb\u610f\u5b57\u7b26\u4e32 IFS data=’ […]<\/p>\n","protected":false},"author":309,"featured_media":10431,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-16385","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\/16385","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\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=16385"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/16385\/revisions"}],"predecessor-version":[{"id":254371,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/16385\/revisions\/254371"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/10431"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=16385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=16385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=16385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}