{"id":1822,"date":"2010-04-21T19:48:21","date_gmt":"2010-04-21T11:48:21","guid":{"rendered":"http:\/\/www.orczhou.com\/?p=1822"},"modified":"2024-09-18T14:35:50","modified_gmt":"2024-09-18T06:35:50","slug":"linux-shell-getopt","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2010\/04\/linux-shell-getopt\/","title":{"rendered":"\u4f7f\u7528getopts\u5904\u7406Shell\u811a\u672c\u53c2\u6570"},"content":{"rendered":"\n<p>\u7f16\u5199\u4e00\u4e2ashell\u811a\u672c\uff0c\u505a\u4e00\u4e9b\u4e8b\uff1b\u6539\u8fdb\u8fd9\u4e2a\u811a\u672c\uff0c\u66f4\u597d\u505a\u8fd9\u4ef6\u4e8b\uff1b\u518d\u6539\u8fdb\u8fd9\u4e2a\u811a\u672c\uff0c\u5e2e\u81ea\u5df1\u505a\u4e9b\u5176\u4ed6\u7684\u4e8b\u60c5\uff1b\u518d\u6539\u8fdb\u8fd9\u4e2a\u811a\u672c\u5e2e\u52a9\u5176\u4ed6\u4eba\u505a\u4e00\u4e9b\u4e8b&#8230;&#8230;<\/p>\n\n\n\n<p>\u7b80\u5355\u7684\u811a\u672c\u5904\u7406\uff0c\u4e00\u822c\u4f7f\u7528\u53d8\u91cf$0 $1 $2 &#8230;\u5c31\u53ef\u4ee5\u4f9d\u6b21\u83b7\u5f97\u5168\u90e8\u53c2\u6570\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7$#\u83b7\u5f97\u8fd9\u4e2a\u811a\u672c\u4e00\u5171\u6709\u591a\u5c11\u4e2a\u53c2\u6570\u3002\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u7684\u60c5\u51b5\uff08\u6216\u8005\u5206\u652f\uff09\u66f4\u591a\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u4e0d\u51d1\u6548\u4e86\uff0c\u8fd9\u65f6\u5019\uff0c\u5c31\u53ef\u4ee5\u8003\u8651\u4f7f\u7528getopts\u4e86\uff08man getopts\uff09\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>\u8fd9\u91cc\u5c06\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\u6765\u4ecb\u7ecdgetopts\u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u901a\u8fc7&#8221;.\/sample -d 5&#8243;\u7684\u65b9\u5f0f\u83b7\u53d6\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">vi sample.sh\n#!\/bin\/sh\nday=7  #default value\nwhile getopts \":d:\" opt; do\n  case $opt in\n    d)\n      day=$OPTARG   #get the value\n      ;;\n    ?)\n      echo \"How to use: $0 [-d DAY]\" >&amp;2\n      exit 1\n      ;;\n    :)\n      echo \"Option -$OPTARG requires an argument.\" >&amp;2\n      exit 1\n      ;;\n  esac\ndone\necho $day<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\u9700\u8981\u89e3\u91ca\u7684\u662f\u4e0b\u9762\u7684\u90e8\u5206\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">while getopts \":d:\" opt; do<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\uff0c\u7b2c\u4e00\u4e2a\u5192\u53f7\u8868\u793a\u5ffd\u7565\u9519\u8bef\uff08\u4f8b\u5982\u51fa\u73b0\u4e86\u4e0d\u8ba4\u8bc6\u7684\u53c2\u6570\uff09\uff0c\u5e76\u5728\u811a\u672c\u4e2d\u901a\u8fc7::)\u6765\u5904\u7406\u8fd9\u6837\u7684\u9519\u8bef\uff1b\u5b57\u6bcdd\u5219\u8868\u793a\uff0c\u63a5\u53d7\u53c2\u6570-d\uff1bd\u540e\u9762\u7684\u5192\u53f7\u8868\u793a\u53c2\u6570d\u63a5\u6536\u503c\uff0c\u5373\u201c-d 7\u201d\u8fd9\u6837\u7684\u5f62\u5f0f\uff1b\uff08\u8fd9\u91ccopt\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728while\u5faa\u73af\u4e2d\u5f15\u7528\u5f53\u524d\u627e\u5230\u7684\u53c2\u6570\uff0c\u8bd5\u8bd5\u8f93\u51fa$opt\u8bd5\u8bd5\uff09<\/p>\n\n\n\n<p>\u5982\u679c\u662f\u8981\u6709\u5f88\u591a\u53c2\u6570\uff0c\u90a3\u4e48\u5199\u6cd5\u53ef\u80fd\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">while getopts \":ixarm:uneh\" opt; do<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u5199\u4e00\u4e2ashell\u811a\u672c\uff0c\u505a\u4e00\u4e9b\u4e8b\uff1b\u6539\u8fdb\u8fd9\u4e2a\u811a\u672c\uff0c\u66f4\u597d\u505a\u8fd9\u4ef6\u4e8b\uff1b\u518d\u6539\u8fdb\u8fd9\u4e2a\u811a\u672c\uff0c\u5e2e\u81ea\u5df1\u505a\u4e9b\u5176\u4ed6\u7684\u4e8b\u60c5\uff1b\u518d\u6539\u8fdb\u8fd9\u4e2a\u811a\u672c\u5e2e\u52a9\u5176\u4ed6\u4eba\u505a\u4e00\u4e9b\u4e8b&#8230;&#8230; \u7b80\u5355\u7684\u811a\u672c\u5904\u7406\uff0c\u4e00\u822c\u4f7f\u7528\u53d8\u91cf$0 $1 $2 &#8230;\u5c31\u53ef\u4ee5\u4f9d\u6b21\u83b7\u5f97\u5168\u90e8\u53c2\u6570\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7$#\u83b7\u5f97\u8fd9\u4e2a\u811a\u672c\u4e00\u5171\u6709\u591a\u5c11\u4e2a\u53c2\u6570\u3002\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u7684\u60c5\u51b5\uff08\u6216\u8005\u5206\u652f\uff09\u66f4\u591a\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u4e0d\u51d1\u6548\u4e86\uff0c\u8fd9\u65f6\u5019\uff0c\u5c31\u53ef\u4ee5\u8003\u8651\u4f7f\u7528getopts\u4e86\uff08man getopts\uff09\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[4,137],"tags":[121,25],"class_list":["post-1822","post","type-post","status-publish","format-standard","hentry","category-code-detail","category-learning-more","tag-linux","tag-shell"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/1822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=1822"}],"version-history":[{"count":12,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/1822\/revisions"}],"predecessor-version":[{"id":14441,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/1822\/revisions\/14441"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=1822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=1822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=1822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}