{"id":210759,"date":"2021-02-02T09:21:08","date_gmt":"2021-02-02T01:21:08","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=210759"},"modified":"2021-01-29T14:21:33","modified_gmt":"2021-01-29T06:21:33","slug":"shell-read-files","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/shell-read-files.html","title":{"rendered":"\u5982\u4f55\u5728Shell\u811a\u672c\u4e2d\u9010\u884c\u8bfb\u53d6\u6587\u4ef6"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5b66\u4e60Shell\u811a\u672c\u4e2d\u76843\u79cd\u65b9\u6cd5\u6765\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u65b9\u6cd5\u4e00\u3001\u4f7f\u7528\u8f93\u5165\u91cd\u5b9a\u5411<\/strong><\/div>\n

\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u5728while\u5faa\u73af\u4e2d\u4f7f\u7528\u8f93\u5165\u91cd\u5b9a\u5411\u3002<\/p>\n

\u4e3a\u4e86\u6f14\u793a\uff0c\u5728\u6b64\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201c mycontent.txt\u201d\u7684\u6587\u672c\u6587\u4ef6\uff0c\u6587\u4ef6\u5185\u5bb9\u5728\u4e0b\u9762\uff1a<\/p>\n

\r\n[root@localhost ~]# cat mycontent.txt \r\nThis is a sample file\r\nWe are going through contents\r\nline by line\r\nto understand\r\n<\/pre>\n

\"\"
\n\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201c example1.sh\u201d\u7684\u811a\u672c\uff0c\u8be5\u811a\u672c\u4f7f\u7528\u8f93\u5165\u91cd\u5b9a\u5411\u548c\u5faa\u73af\uff1a<\/p>\n

\r\n[root@localhost ~]# cat example1.sh \r\n#!\/bin\/bash\r\nwhile read rows\r\ndo\r\n  echo \"Line contents are : $rows \"\r\ndone < mycontent.txt\r\n<\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a
\n\"\"
\n\u5982\u4f55\u5de5\u4f5c\u7684\uff1a<\/p>\n

    \n
  • - \u5f00\u59cbwhile\u5faa\u73af\uff0c\u5e76\u5728\u53d8\u91cf\u201crows\u201d\u4e2d\u4fdd\u5b58\u6bcf\u4e00\u884c\u7684\u5185\u5bb9<\/li>\n
  • - \u4f7f\u7528echo\u663e\u793a\u8f93\u51fa\u5185\u5bb9\uff0c$rows\u53d8\u91cf\u4e3a\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u6bcf\u884c\u5185\u5bb9<\/li>\n
  • - \u4f7f\u7528echo\u663e\u793a\u8f93\u51fa\u5185\u5bb9\uff0c\u8f93\u51fa\u5185\u5bb9\u5305\u62ec\u81ea\u5b9a\u4e49\u7684\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\uff0c$rows\u53d8\u91cf\u4e3a\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u6bcf\u884c\u5185\u5bb9<\/li>\n<\/ul>\n

    Tips\uff1a\u53ef\u4ee5\u5c06\u4e0a\u9762\u7684\u811a\u672c\u7f29\u51cf\u4e3a\u4e00\u884c\u547d\u4ee4\uff0c\u5982\u4e0b\uff1a<\/p>\n

    \r\n[root@localhost ~]# while read rows; do echo \"Line contents are : $rows\"; done < mycontent.txt\r\n<\/pre>\n

    \"\"<\/p>\n

    \u65b9\u6cd5\u4e8c\u3001\u4f7f\u7528cat\u547d\u4ee4\u548c\u7ba1\u9053\u7b26<\/strong><\/div>\n

    \u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528cat<\/code>\u547d\u4ee4\u548c\u7ba1\u9053\u7b26|<\/code>\uff0c\u7136\u540e\u4f7f\u7528\u7ba1\u9053\u7b26\u5c06\u5176\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\u4f20\u9001\u5230while\u5faa\u73af\u3002<\/p>\n

    \u521b\u5efa\u811a\u672c\u6587\u4ef6\u201c example2.sh\u201d\uff0c\u5176\u5185\u5bb9\u4e3a\uff1a<\/p>\n

    \r\n[root@localhost ~]# cat example2.sh \r\n#!\/bin\/bash\r\ncat mycontent.txt | while read rows\r\ndo\r\n  echo \"Line contents are : $rows \"\r\ndone\r\n<\/pre>\n

    \u8fd0\u884c\u7ed3\u679c\uff1a
    \n\"\"
    \n\u5982\u4f55\u5de5\u4f5c\u7684\uff1a<\/p>\n

      \n
    • - \u4f7f\u7528\u7ba1\u9053\u5c06cat\u547d\u4ee4\u7684\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\u53d1\u9001\u5230while\u5faa\u73af\u3002<\/li>\n
    • - |<\/code>\u7ba1\u9053\u7b26\u5c06cat\u8f93\u51fa\u7684\u5185\u5bb9\u4fdd\u5b58\u5728\"$rows\"\u53d8\u91cf\u4e2d\u3002<\/li>\n
    • - \u4f7f\u7528echo\u663e\u793a\u8f93\u51fa\u5185\u5bb9\uff0c\u8f93\u51fa\u5185\u5bb9\u5305\u62ec\u81ea\u5b9a\u4e49\u7684\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\uff0c$rows\u53d8\u91cf\u4e3a\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u6bcf\u884c\u5185\u5bb9<\/li>\n<\/ul>\n

      Tips\uff1a\u53ef\u4ee5\u5c06\u4e0a\u9762\u7684\u811a\u672c\u7f29\u51cf\u4e3a\u4e00\u884c\u547d\u4ee4\uff0c\u5982\u4e0b\uff1a<\/p>\n

      \r\n[root@localhost ~]# cat mycontent.txt |while read rows;do echo \"Line contents are : $rows\";done\r\n<\/pre>\n

      \"\"<\/p>\n

      \u65b9\u6cd5\u4e09\u3001\u4f7f\u7528\u4f20\u5165\u7684\u6587\u4ef6\u540d\u4f5c\u4e3a\u53c2\u6570<\/strong><\/div>\n

      \u7b2c\u4e09\u79cd\u65b9\u6cd5\u5c06\u901a\u8fc7\u6dfb\u52a0$1\u53c2\u6570\uff0c\u6267\u884c\u811a\u672c\u65f6\uff0c\u5728\u811a\u672c\u540e\u9762\u8ffd\u52a0\u6587\u672c\u6587\u4ef6\u540d\u79f0\u3002<\/p>\n

      \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201c example3.sh\u201d\u7684\u811a\u672c\u6587\u4ef6\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

      \r\n[root@localhost ~]# cat example3.sh \r\n#!\/bin\/bash\r\nwhile read rows\r\ndo\r\n  echo \"Line contents are : $rows \"\r\ndone < $1\r\n<\/pre>\n

      \u8fd0\u884c\u7ed3\u679c\uff1a
      \n\"\"
      \n\u5982\u4f55\u5de5\u4f5c\u7684\uff1a<\/p>\n

        \n
      • - \u5f00\u59cbwhile\u5faa\u73af\uff0c\u5e76\u5728\u53d8\u91cf\u201crows\u201d\u4e2d\u4fdd\u5b58\u6bcf\u4e00\u884c\u7684\u5185\u5bb9<\/li>\n
      • - \u4f7f\u7528echo\u663e\u793a\u8f93\u51fa\u5185\u5bb9\uff0c$rows\u53d8\u91cf\u4e3a\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u6bcf\u884c\u5185\u5bb9<\/li>\n
      • - \u4f7f\u7528\u8f93\u5165\u91cd\u5b9a\u5411<<\/code>\u4ece\u547d\u4ee4\u884c\u53c2\u6570$1<\/code>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/li>\n<\/ul>\n
        \u65b9\u6cd5\u56db\u3001\u4f7f\u7528awk\u547d\u4ee4<\/strong><\/div>\n

        \u901a\u8fc7\u4f7f\u7528awk\u547d\u4ee4\uff0c\u53ea\u9700\u8981\u4e00\u884c\u547d\u4ee4\u5c31\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n

        \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201c example4.sh\u201d\u7684\u811a\u672c\u6587\u4ef6\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

        \r\n[root@localhost ~]# cat example4.sh \r\n#!\/bin\/bash\r\n\r\ncat mycontent.txt |awk '{print \"Line contents are: \"$0}'\r\n<\/pre>\n

        \u8fd0\u884c\u7ed3\u679c\uff1a
        \n\"\"<\/p>\n

        \u603b\u7ed3<\/strong><\/div>\n

        \u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528shell\u811a\u672c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u901a\u8fc7\u5355\u72ec\u8bfb\u53d6\u884c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u641c\u7d22\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

        \u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u5728while\u5faa\u73af\u4e2d\u4f7f\u7528\u8f93\u5165\u91cd\u5b9a\u5411\u3002 \u4e3a\u4e86\u6f14\u793a\uff0c\u5728\u6b64\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201c mycontent […]<\/p>\n","protected":false},"author":309,"featured_media":210760,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-210759","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\/210759","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=210759"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210759\/revisions"}],"predecessor-version":[{"id":210768,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210759\/revisions\/210768"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/210760"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=210759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=210759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=210759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}