{"id":463132,"date":"2025-06-13T09:29:13","date_gmt":"2025-06-13T09:29:13","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=463132"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=463132","title":{"rendered":"<span>getopts: \u043a\u0430\u043a \u043f\u0438\u0441\u0430\u0442\u044c CLI-\u0443\u0442\u0438\u043b\u0438\u0442\u044b \u0441 \u0444\u043b\u0430\u0433\u0430\u043c\u0438 \u0431\u0435\u0437 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p><code>getopts<\/code> \u2014 \u044d\u0442\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0432 \u043b\u044e\u0431\u043e\u0439 POSIX-\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0439 Linux\/Unix-shell \u043c\u0438\u043d\u0438-\u043f\u0430\u0440\u0441\u0435\u0440 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u041e\u0434\u0438\u043d shebang \u2014 \u0438 \u0443 \u0432\u0430\u0441 CLI-\u0443\u0442\u0438\u043b\u0438\u0442\u0430 \u0431\u0435\u0437 \u0435\u0434\u0438\u043d\u043e\u0439 \u0432\u043d\u0435\u0448\u043d\u0435\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0432\u044b\u0436\u0430\u0442\u044c \u0438\u0437 <code>getopts<\/code> \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c, \u0433\u0434\u0435 \u043e\u043d \u0441\u043f\u043e\u0442\u044b\u043a\u0430\u0435\u0442\u0441\u044f \u0438 \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u0440\u0430 \u043f\u0435\u0440\u0435\u0435\u0445\u0430\u0442\u044c \u043d\u0430 <code>getopt<\/code> \u0438\u043b\u0438 Argbash.<\/p>\n<h4>\u041f\u043e\u0447\u0435\u043c\u0443 \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u0441\u0451 \u0435\u0449\u0451 getopts<\/h4>\n<ul>\n<li>\n<p><strong>\u0412\u0445\u043e\u0434\u0438\u0442 \u0432 POSIX<\/strong> \u2014 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432 \/bin\/sh \u043d\u0430 \u043b\u044e\u0431\u043e\u043c \u00ab\u0436\u0438\u0432\u043e\u043c\u00bb \u044e\u043d\u0438\u043a\u0441\u0435.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439<\/strong> \u2014  \u0441\u043a\u0440\u0438\u043f\u0442 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0444\u0430\u0439\u043b\u043e\u043c.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0447\u0451\u0442\u043a\u043e \u0440\u0435\u0433\u043b\u0430\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043e<\/strong> \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439 Open Group \u2014 \u0442. \u0435. \u0441\u043a\u0440\u0438\u043f\u0442 \u0431\u0443\u0434\u0435\u0442 \u0432\u0435\u0441\u0442\u0438 \u0441\u0435\u0431\u044f \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e \u043f\u043e\u0434 bash, zsh, dash, ksh. <\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u0438\u043d\u0443\u0441 \u2014 \u043b\u0438\u0448\u044c \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0444\u043b\u0430\u0433\u0438 \u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0440\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 <code>--long-option<\/code>. \u041d\u043e \u0438 \u044d\u0442\u043e \u0440\u0435\u0448\u0430\u0435\u043c\u043e.<\/p>\n<h3>\u0411\u0430\u0437\u043e\u0432\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441: \u043e\u0434\u043d\u043e \u043a\u043e\u043b\u044c\u0446\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435\u043c\u0438<\/h3>\n<pre><code class=\"bash\">#!\/usr\/bin\/env sh set -euo pipefail IFS=' '  usage() {   cat &lt;&lt;EOF Usage: ${0##*\/} [-a] [-b ARG] file...   -a        : \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0436\u0438\u043c   -b ARG    : \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 EOF   exit 2 }  # \u0434\u0432\u043e\u0435\u0442\u043e\u0447\u0438\u0435 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u2192 \u0442\u0438\u0445\u0438\u0439 \u0440\u0435\u0436\u0438\u043c \u043e\u0448\u0438\u0431\u043e\u043a while getopts \":ab:\" opt; do   case \"$opt\" in     a) flag_a=true ;;     b) param_b=$OPTARG ;;     :) echo \"\u041e\u043f\u0446\u0438\u044f -$OPTARG \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\" &gt;&amp;2; usage ;;     \\?) echo \"\u041d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430\u044f \u043e\u043f\u0446\u0438\u044f -$OPTARG\" &gt;&amp;2; usage ;;   esac done shift \"$((OPTIND-1))\"   # \u0443\u0431\u0438\u0440\u0430\u0435\u043c \u0443\u0436\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b<\/code><\/pre>\n<p>\u0414\u0432\u043e\u0435\u0442\u043e\u0447\u0438\u0435 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 <code>optstring<\/code> \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u043f\u0430\u0440\u0441\u0435\u0440 \u0432 \u00absilent mode\u00bb \u2014 \u043e\u0448\u0438\u0431\u043a\u0438 \u043f\u043e argv \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u043e\u0432\u0438\u0442\u044c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u0437\u0430\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0434\u0430\u0442\u044c \u043b\u0430\u0439\u0442\u043e\u0432\u044b\u0439 help, \u0430 \u043d\u0435 cryptic usage \u0438\u0437 \u043d\u0435\u0434\u0440 \u0448\u0435\u043b\u043b\u0430. \u041f\u0440\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c <code>shift \"$((OPTIND-1))\"<\/code> \u0437\u0430\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0434\u0430\u0436\u0435 \u0432 posix man-pages \u2014 \u0442\u0430\u043a \u0443\u0431\u0438\u0440\u0430\u0435\u043c \u0438\u0437 <code>$@<\/code> \u043e\u043f\u0446\u0438\u0438 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0447\u0438\u0441\u0442\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. <\/p>\n<h3>\u041a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0444\u043b\u0430\u0433\u0438 \u0438 \u0438\u0445 \u043a\u043e\u043c\u0431\u043e<\/h3>\n<p><code>getopts<\/code> \u0443\u043c\u0435\u0435\u0442 \u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0441\u043b\u0438\u043f\u0448\u0438\u0435\u0441\u044f \u0444\u043b\u0430\u0433\u0438: <code>-abc<\/code> \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a <code>-a -b -c<\/code>. \u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u043e\u0436\u0438\u0434\u0430\u0435\u0442\u0441\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u0440\u0430\u0437\u0431\u043e\u0440 \u043f\u0440\u0435\u043a\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0440\u043e\u0432\u043d\u043e \u0442\u0430\u043c:<\/p>\n<pre><code class=\"bash\"># optstring=\"a:b\" $ my.sh -ac   # OK: -a, -c \u043a\u0430\u043a \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043a\u0430 $ my.sh -abx  # -a, -b x $ my.sh -ab   # \u043e\u0448\u0438\u0431\u043a\u0430: -b \u0436\u0434\u0451\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442<\/code><\/pre>\n<p><strong>Edge-case<\/strong> \u2014 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 (<code>-5<\/code>) \u0432\u043d\u0435\u0437\u0430\u043f\u043d\u043e \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0442\u0441\u044f \u0437\u0430 \u0444\u043b\u0430\u0433. \u041d\u0430\u0447\u0438\u043d\u0430\u0439\u0442\u0435 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 \u043e\u043f\u0446\u0438\u0439 \u0441 <code>--<\/code>, \u0442\u043e\u0433\u0434\u0430 <code>getopts<\/code> \u043f\u0440\u0435\u043a\u0440\u0430\u0442\u0438\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u043f\u0440\u0438 \u0432\u0438\u0434\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043d\u0435-\u0444\u043b\u0430\u0433\u0430, \u0438 \u043c\u0438\u043d\u0443\u0441\u044b \u0432 \u0447\u0438\u0441\u043b\u0430\u0445 \u043e\u0441\u0442\u0430\u043d\u0443\u0442\u0441\u044f \u043d\u0435\u0442\u0440\u043e\u043d\u0443\u0442\u044b\u043c\u0438.<\/p>\n<h3>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c &#8212;long \u0431\u0435\u0437 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u0442\u0443\u043b\u043e\u0432<\/h3>\n<p>\u0427\u0430\u0441\u0442\u044b\u0439 \u043f\u0440\u0438\u0435\u043c: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0447\u0435\u0440\u0435\u0437 <code>case \"$1\"<\/code> \u043b\u043e\u0432\u0438\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b <code>--help<\/code>, <code>--version<\/code>, <code>--long=value<\/code>, \u0430 \u0437\u0430\u0442\u0435\u043c \u043e\u0442\u0434\u0430\u0451\u043c \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0432 <code>getopts<\/code> \u2014 \u0442\u0430\u043a \u043d\u0435 \u043b\u043e\u043c\u0430\u0435\u043c POSIX-\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0438 \u043d\u0435 \u0442\u0430\u0449\u0438\u043c GNU <code>getopt<\/code>.<\/p>\n<pre><code class=\"bash\">while [ $# -gt 0 ]; do   case \"$1\" in     --help) usage ;;     --output=*) opt_o=${1#*=}; shift ;;     --) shift; break ;;     # \u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u0442\u0438\u0440\u0435 \u2014 \u043a\u043e\u043d\u0435\u0446 \u043e\u043f\u0446\u0438\u0439     -*) break ;;            # \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u043e\u043f\u0446\u0438\u0438 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u0442 getopts     *)  break ;;   esac done  # \u0442\u0435\u043f\u0435\u0440\u044c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 getopts while getopts \":o:f:\" opt; do   ... done shift \"$((OPTIND-1))\"<\/code><\/pre>\n<p>\u0421\u0445\u0435\u043c\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u044f: \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u2014 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u0435 alias-\u044b \u0432\u0440\u043e\u0434\u0435 <code>--verbose\/-v<\/code>, \u0438\u043b\u0438 \u0432\u043e\u043e\u0431\u0449\u0435 YAML-\u043a\u043e\u043d\u0444\u0438\u0433 \u043f\u043e\u0441\u043b\u0435 <code>--config=path.yml<\/code>.<\/p>\n<h3>\u0424\u0430\u0439\u043b &gt; \u0444\u0430\u0439\u043b: \u043f\u0440\u0438\u043c\u0435\u0440 (-f input -o output)<\/h3>\n<p>\u0421\u043e\u0431\u0435\u0440\u0451\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 CLI-\u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440:<\/p>\n<pre><code class=\"bash\">#!\/usr\/bin\/env bash set -Eeuo pipefail trap 'echo \" \u0427\u0442\u043e-\u0442\u043e \u043f\u043e\u0448\u043b\u043e \u043d\u0435 \u0442\u0430\u043a \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 $LINENO\" &gt;&amp;2' ERR VERSION=\"1.2.3\"  usage() {   cat &lt;&lt;EOF ${0##*\/} \u2014 \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440\u0430.  \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:   -f FILE    \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0444\u0430\u0439\u043b (\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e)   -o FILE    \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0444\u0430\u0439\u043b (\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e)   -t TYPE    \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 (txt|json|xml), \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e txt   -v         \u0431\u043e\u043b\u0442\u043b\u0438\u0432\u044b\u0439 \u0440\u0435\u0436\u0438\u043c   -h         \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0439 \u0441\u043f\u0440\u0430\u0432\u043a\u0438 EOF }  infile= outfile= outtype=txt verbose=false  while getopts \":f:o:t:vh\" opt; do   case \"$opt\" in     f) infile=$OPTARG ;;     o) outfile=$OPTARG ;;     t) outtype=$OPTARG ;;     v) verbose=true ;;     h) usage; exit 0 ;;     :) echo \" \u041e\u043f\u0446\u0438\u044f -$OPTARG \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\" &gt;&amp;2; usage; exit 2 ;;     \\?) echo \" \u041d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430\u044f \u043e\u043f\u0446\u0438\u044f -$OPTARG\" &gt;&amp;2; usage; exit 2 ;;   esac done shift \"$((OPTIND-1))\"  # sanity-check [ -n \"$infile\" ]  || { echo \"\u041d\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430\" &gt;&amp;2; exit 2; } [ -n \"$outfile\" ] || { echo \"\u041d\u0435\u0442 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430\" &gt;&amp;2; exit 2; } [ -r \"$infile\" ]  || { echo \"\u0424\u0430\u0439\u043b '$infile' \u043d\u0435 \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f\" &gt;&amp;2; exit 3; }  $verbose &amp;&amp; echo \"\u21e2 \u041a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u044e $infile \u2192 $outfile ($outtype)\"  case \"$outtype\" in   txt)   cp \"$infile\" \"$outfile\" ;;              # stub   json)  jq -R -s '.' \"$infile\" &gt;\"$outfile\" ;;   # example   xml)   iconv -f utf8 -t utf16 \"$infile\" &gt;\"$outfile\" ;;   *) echo \"\u041d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 $outtype\" &gt;&amp;2; exit 4 ;; esac<\/code><\/pre>\n<p>\u0412 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u043d \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0441\u0442\u0440\u043e\u0433\u0438\u0439 \u0440\u0435\u0436\u0438\u043c Bash (<code>set -Eeuo pipefail<\/code>) \u0438 \u043b\u043e\u0432\u0438\u043c \u043b\u044e\u0431\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0447\u0435\u0440\u0435\u0437 <code>trap<\/code>, \u0447\u0442\u043e\u0431\u044b \u0441\u0440\u0430\u0437\u0443 \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c, \u043d\u0430 \u043a\u0430\u043a\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0441\u0451 \u0443\u043f\u0430\u043b\u043e. \u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>usage<\/code> \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u0442 \u0441\u043f\u0440\u0430\u0432\u043a\u0443. \u0414\u0430\u043b\u0435\u0435 <code>getopts<\/code> \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u0442 \u0444\u043b\u0430\u0433\u0438: <code>-f<\/code> \u2014 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0444\u0430\u0439\u043b, <code>-o<\/code> \u2014 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439, <code>-t<\/code> \u2014 \u0444\u043e\u0440\u043c\u0430\u0442 (txt-|json|xml, \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e txt), <code>-v<\/code> \u2014 \u0431\u043e\u043b\u0442\u043b\u0438\u0432\u044b\u0439 \u0440\u0435\u0436\u0438\u043c, <code>-h<\/code> \u2014 \u043f\u043e\u043c\u043e\u0449\u044c. \u041f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u0431\u043e\u0440\u043a\u0438 \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0447\u0442\u043e \u0444\u0430\u0439\u043b\u044b \u0437\u0430\u0434\u0430\u043d\u044b \u0438 \u0432\u0445\u043e\u0434 \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f. \u0415\u0441\u043b\u0438 \u0432\u043a\u043b\u044e\u0447\u0451\u043d <code>-v<\/code>, \u043f\u0438\u0448\u0435\u0442, \u0447\u0442\u043e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442. \u0414\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0443: <code>txt<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u0444\u0430\u0439\u043b, <code>json<\/code> \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0432 JSON \u0447\u0435\u0440\u0435\u0437 <code>jq<\/code>, <code>xml<\/code> \u043f\u0435\u0440\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 \u0432 UTF-16 \u0441 <code>iconv<\/code>. \u041d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u2014 \u0432\u044b\u0445\u043e\u0434 \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439.  <\/p>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a: \u0434\u0432\u043e\u0435\u0442\u043e\u0447\u0438\u0435, \u0432\u043e\u043f\u0440\u043e\u0441, \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u043a \u0436\u0438\u0437\u043d\u0438<\/h3>\n<p><code>getopts<\/code> \u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0442 \u043e \u043d\u0435\u0448\u0442\u0430\u0442\u043d\u044b\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u0445 \u0434\u0432\u0443\u043c\u044f \u043c\u0430\u0440\u043a\u0435\u0440\u0430\u043c\u0438:<\/p>\n<ul>\n<li>\n<p><code>?<\/code> \u2014 \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430\u044f \u043e\u043f\u0446\u0438\u044f (<code>-z<\/code>).<\/p>\n<\/li>\n<li>\n<p><code>:<\/code> \u2014 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 (<code>-f<\/code> \u0431\u0435\u0437 \u0444\u0430\u0439\u043b\u0430), \u0435\u0441\u043b\u0438 \u0432 <code>optstring<\/code> \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043e \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 <code>:<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"bash\">while getopts \":f:o:\" opt; do   case $opt in     f) infile=$OPTARG ;;     o) outfile=$OPTARG ;;     :) printf &gt;&amp;2 ' %s: \u043e\u043f\u0446\u0438\u044f -%s \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\\n' \"$0\" \"$OPTARG\" ;;     \\?) printf &gt;&amp;2 ' %s: \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430\u044f \u043e\u043f\u0446\u0438\u044f -%s\\n'   \"$0\" \"$OPTARG\" ;;   esac done<\/code><\/pre>\n<p>\u0412 <code>case<\/code> \u043b\u0443\u0447\u0448\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e <code>\\?)<\/code>, \u0430 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e <code>?<\/code>, \u0438\u043d\u0430\u0447\u0435 shell \u0432\u043e\u0441\u043f\u0440\u0438\u043c\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u043a\u0430\u043a wildcard.<\/p>\n<h3>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c: Bats &amp; shellcheck<\/h3>\n<ol>\n<li>\n<p><strong>ShellCheck<\/strong> \u2014 \u043b\u0438\u043d\u0442\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e \u043b\u043e\u0432\u0438\u0442 \u00ab\u0443\u0445 \u0442\u044b, \u0434\u0432\u043e\u0439\u043d\u044b\u0435 \u043a\u0430\u0432\u044b\u0447\u043a\u0438 \u0437\u0430\u0431\u044b\u043b\u00bb \u0438 <code>[ $var == foo ]<\/code> \u0431\u0435\u0437 \u043a\u0430\u0432\u044b\u0447\u0435\u043a.<\/p>\n<\/li>\n<li>\n<p><strong>Bats-core<\/strong> \u2014 unit-\u0442\u0435\u0441\u0442\u044b \u0434\u043b\u044f bash. \u0421\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a:<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"bash\">@test \"\u0432\u044b\u0432\u043e\u0434\u0438\u0442 usage \u0431\u0435\u0437 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432\" {   run .\/convert.sh   [ \"$status\" -eq 2 ]   [[ \"${lines[0]}\" =~ Usage ]] }  @test \"\u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0444\u0430\u0439\u043b \u0432 txt\" {   run .\/convert.sh -f sample.in -o sample.out   [ \"$status\" -eq 0 ]   cmp sample.in sample.out }<\/code><\/pre>\n<h3>\u041a\u043e\u0433\u0434\u0430 getopts \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0451\u0442 \u0445\u0432\u0430\u0442\u0430\u0442\u044c<\/h3>\n<p><code>getopts<\/code> \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 90 % \u0437\u0430\u0434\u0430\u0447: \u043e\u043d \u0432\u0441\u0442\u0440\u043e\u0435\u043d \u0432 \u043b\u044e\u0431\u043e\u0439 shell, \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0438 \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0434\u0430\u0436\u0435 \u043d\u0430 BusyBox-\u043f\u0440\u043e\u0448\u0438\u0432\u043a\u0438. \u041d\u043e \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0443\u0436\u0435\u043d \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 <code>--long<\/code>-\u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441, \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u043b\u0438 \u0433\u0440\u0430\u043c\u043e\u0442\u043d\u043e\u0435 \u044d\u043a\u0440\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0431\u0435\u043b\u043e\u0432, \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0437\u0432\u0430\u0442\u044c GNU <code>getopt<\/code>. \u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043d\u0430 macOS \u0438 Alpine \u0441\u0442\u0430\u0432\u044f\u0442 \u0443\u0440\u0435\u0437\u0430\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e: \u0441\u043a\u0440\u0438\u043f\u0442 \u0440\u0438\u0441\u043a\u0443\u0435\u0442 \u0441\u043b\u043e\u043c\u0430\u0442\u044c\u0441\u044f \u0438\u043b\u0438 \u043d\u0430\u0447\u0430\u0442\u044c \u00ab\u0433\u043b\u043e\u0442\u0430\u0442\u044c\u00bb \u043f\u0443\u0441\u0442\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u2014 \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 BashGuide \u0441\u043e\u0432\u0435\u0442\u0443\u0435\u0442 \u0434\u0435\u0440\u0436\u0430\u0442\u044c\u0441\u044f \u043e\u0442 <code>getopt(1)<\/code> \u043f\u043e\u0434\u0430\u043b\u044c\u0448\u0435.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 CLI \u043e\u0431\u0440\u0430\u0441\u0442\u0430\u0435\u0442 \u0434\u0435\u0441\u044f\u0442\u043a\u043e\u043c \u0444\u043b\u0430\u0433\u043e\u0432, autocompletion \u0438 \u0441\u0442\u0440\u043e\u0433\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u043e\u0439 \u0442\u0438\u043f\u043e\u0432, \u043b\u0443\u0447\u0448\u0435 \u0441\u0440\u0430\u0437\u0443 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0451\u0440\u0442\u043a\u0443 \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/argbash.readthedocs.io\/en\/latest\/\" rel=\"noopener noreferrer nofollow\">Argbash<\/a>. \u041e\u043d \u0432\u044b\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0441\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u043c <code>--long<\/code>, bash-completion \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439 \u0432\u0440\u043e\u0434\u0435 <code>INT&gt;=0<\/code> \u0438\u043b\u0438 <code>PATH<\/code>. \u041c\u0438\u043d\u0443\u0441 \u043e\u0434\u0438\u043d: \u0441\u0430\u043c\u0430 \u0443\u0442\u0438\u043b\u0438\u0442\u0430 Argbash \u043d\u0443\u0436\u043d\u0430 \u043b\u0438\u0448\u044c \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0431\u0438\u043b\u0434\u0430, \u0442\u0430\u043a \u0447\u0442\u043e \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u043d\u0435\u0442, \u043d\u043e \u0432 CI \u0435\u0451 \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c.<\/p>\n<hr\/>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<ol>\n<li>\n<p><strong>\u0411\u0435\u0440\u0438\u0442\u0435 <\/strong><code><strong>getopts<\/strong><\/code><strong> \u043f\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u0443<\/strong>: \u0434\u043b\u044f 90 % \u0443\u0442\u0438\u043b\u0438\u0442 \u0445\u0432\u0430\u0442\u0438\u0442 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0445 \u0444\u043b\u0430\u0436\u043a\u043e\u0432 \u0438 5-10 \u0441\u0442\u0440\u043e\u043a \u043a\u043e\u0434\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u0435 \u0431\u043e\u0439\u0442\u0435\u0441\u044c \u00ab&#8212;long\u00bb<\/strong> \u2014 \u0441\u043c\u0435\u0448\u0430\u043d\u043d\u044b\u0439 \u043f\u0430\u0440\u0441\u0438\u043d\u0433 (pre-loop + <code>getopts<\/code>) \u0440\u0435\u0448\u0430\u0435\u0442 \u0438 \u043d\u0435 \u043b\u043e\u043c\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u043c\u043e\u0441\u0442\u044c.<\/p>\n<\/li>\n<li>\n<p><strong>\u0425\u043e\u0442\u0438\u0442\u0435 UX \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 <\/strong><code><strong>kubectl<\/strong><\/code><strong> \u2014 \u0438\u0434\u0438\u0442\u0435 \u0432 <\/strong><code><strong>getopt_long<\/strong><\/code><strong> \u0438\u043b\u0438 Argbash<\/strong>, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044f \u0440\u0438\u0441\u043a\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u0438 \u044d\u0442\u0438\u0445 \u0442\u0443\u043b\u043e\u0432 \u0443 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0430\u0443\u0434\u0438\u0442\u043e\u0440\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p><strong>\u0411\u0435\u0439\u0442\u0435 \u043f\u043e \u0440\u0443\u043a\u0430\u043c \u0441\u0435\u0431\u044f (\u0438 \u043a\u043e\u043b\u043b\u0435\u0433) \u0437\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 <\/strong><code><strong>set -euo pipefail<\/strong><\/code><strong> \u0438 \u043d\u0435\u0437\u0430\u043a\u0440\u044b\u0442\u044b\u0435 \u043a\u0430\u0432\u044b\u0447\u043a\u0438<\/strong> \u2014 \u044d\u0442\u043e \u0434\u0435\u0448\u0435\u0432\u043b\u0435, \u0447\u0435\u043c \u043d\u043e\u0447\u043d\u043e\u0439 \u0438\u043d\u0446\u0438\u0434\u0435\u043d\u0442.<\/p>\n<\/li>\n<li>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0439\u0442\u0435<\/strong>: ShellCheck + Bats \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u044e\u0442 80 % \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0444\u0430\u043a\u0430\u043f\u043e\u0432 \u0435\u0449\u0451 \u0434\u043e code-review.<\/p>\n<\/li>\n<\/ol>\n<hr\/>\n<p>\u0412\u0441\u0435\u0445 \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a <a href=\"https:\/\/otus.pw\/EUxB\/\" rel=\"noopener noreferrer nofollow\"><strong>\u00ab\u041f\u0430\u043c\u044f\u0442\u044c \u0432 Linux: Cache, swap, dirty pages\u00bb<\/strong><\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0439\u0434\u0451\u0442 <strong><em>16 \u0438\u044e\u043d\u044f \u0432 20:00<\/em><\/strong>, \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u043a\u0443\u0440\u0441\u0430 <a href=\"https:\/\/otus.pw\/oa3X\/\" rel=\"noopener noreferrer nofollow\"><strong>\u00abAdministrator Linux. Professional\u00bb<\/strong><\/a>, \u0441\u0442\u0430\u0440\u0442\u0443\u044e\u0449\u0435\u0433\u043e <strong><em>26 \u0438\u044e\u043d\u044f<\/em><\/strong>.<\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0441\u0432\u043e\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u043c \u043f\u0440\u043e\u0439\u0442\u0438 <a href=\"https:\/\/otus.pw\/MnDL\/\" rel=\"noopener noreferrer nofollow\"><strong>\u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/a>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/911338\/\"> https:\/\/habr.com\/ru\/articles\/911338\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p><code>getopts<\/code> \u2014 \u044d\u0442\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0432 \u043b\u044e\u0431\u043e\u0439 POSIX-\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0439 Linux\/Unix-shell \u043c\u0438\u043d\u0438-\u043f\u0430\u0440\u0441\u0435\u0440 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u041e\u0434\u0438\u043d shebang \u2014 \u0438 \u0443 \u0432\u0430\u0441 CLI-\u0443\u0442\u0438\u043b\u0438\u0442\u0430 \u0431\u0435\u0437 \u0435\u0434\u0438\u043d\u043e\u0439 \u0432\u043d\u0435\u0448\u043d\u0435\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0432\u044b\u0436\u0430\u0442\u044c \u0438\u0437 <code>getopts<\/code> \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c, \u0433\u0434\u0435 \u043e\u043d \u0441\u043f\u043e\u0442\u044b\u043a\u0430\u0435\u0442\u0441\u044f \u0438 \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u0440\u0430 \u043f\u0435\u0440\u0435\u0435\u0445\u0430\u0442\u044c \u043d\u0430 <code>getopt<\/code> \u0438\u043b\u0438 Argbash.<\/p>\n<h4>\u041f\u043e\u0447\u0435\u043c\u0443 \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u0441\u0451 \u0435\u0449\u0451 getopts<\/h4>\n<ul>\n<li>\n<p><strong>\u0412\u0445\u043e\u0434\u0438\u0442 \u0432 POSIX<\/strong> \u2014 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432 \/bin\/sh \u043d\u0430 \u043b\u044e\u0431\u043e\u043c \u00ab\u0436\u0438\u0432\u043e\u043c\u00bb \u044e\u043d\u0438\u043a\u0441\u0435.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439<\/strong> \u2014  \u0441\u043a\u0440\u0438\u043f\u0442 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0444\u0430\u0439\u043b\u043e\u043c.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0447\u0451\u0442\u043a\u043e \u0440\u0435\u0433\u043b\u0430\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043e<\/strong> \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439 Open Group \u2014 \u0442. \u0435. \u0441\u043a\u0440\u0438\u043f\u0442 \u0431\u0443\u0434\u0435\u0442 \u0432\u0435\u0441\u0442\u0438 \u0441\u0435\u0431\u044f \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e \u043f\u043e\u0434 bash, zsh, dash, ksh. <\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u0438\u043d\u0443\u0441 \u2014 \u043b\u0438\u0448\u044c \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0444\u043b\u0430\u0433\u0438 \u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0440\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 <code>--long-option<\/code>. \u041d\u043e \u0438 \u044d\u0442\u043e \u0440\u0435\u0448\u0430\u0435\u043c\u043e.<\/p>\n<h3>\u0411\u0430\u0437\u043e\u0432\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441: \u043e\u0434\u043d\u043e \u043a\u043e\u043b\u044c\u0446\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435\u043c\u0438<\/h3>\n<pre><code class=\"bash\">#!\/usr\/bin\/env sh set -euo pipefail IFS=' '  usage() {   cat &lt;&lt;EOF Usage: ${0##*\/} [-a] [-b ARG] file...   -a        : \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0436\u0438\u043c   -b ARG    : \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 EOF   exit 2 }  # \u0434\u0432\u043e\u0435\u0442\u043e\u0447\u0438\u0435 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u2192 \u0442\u0438\u0445\u0438\u0439 \u0440\u0435\u0436\u0438\u043c \u043e\u0448\u0438\u0431\u043e\u043a while getopts \":ab:\" opt; do   case \"$opt\" in     a) flag_a=true ;;     b) param_b=$OPTARG ;;     :) echo \"\u041e\u043f\u0446\u0438\u044f -$OPTARG \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\" &gt;&amp;2; usage ;;     \\?) echo \"\u041d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430\u044f \u043e\u043f\u0446\u0438\u044f -$OPTARG\" &gt;&amp;2; usage ;;   esac done shift \"$((OPTIND-1))\"   # \u0443\u0431\u0438\u0440\u0430\u0435\u043c \u0443\u0436\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b<\/code><\/pre>\n<p>\u0414\u0432\u043e\u0435\u0442\u043e\u0447\u0438\u0435 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 <code>optstring<\/code> \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u043f\u0430\u0440\u0441\u0435\u0440 \u0432 \u00absilent mode\u00bb \u2014 \u043e\u0448\u0438\u0431\u043a\u0438 \u043f\u043e argv \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u043e\u0432\u0438\u0442\u044c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u0437\u0430\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0434\u0430\u0442\u044c \u043b\u0430\u0439\u0442\u043e\u0432\u044b\u0439 help, \u0430 \u043d\u0435 cryptic usage \u0438\u0437 \u043d\u0435\u0434\u0440 \u0448\u0435\u043b\u043b\u0430. \u041f\u0440\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c <code>shift \"$((OPTIND-1))\"<\/code> \u0437\u0430\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0434\u0430\u0436\u0435 \u0432 posix man-pages \u2014 \u0442\u0430\u043a \u0443\u0431\u0438\u0440\u0430\u0435\u043c \u0438\u0437 <code>$@<\/code> \u043e\u043f\u0446\u0438\u0438 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0447\u0438\u0441\u0442\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. <\/p>\n<h3>\u041a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0444\u043b\u0430\u0433\u0438 \u0438 \u0438\u0445 \u043a\u043e\u043c\u0431\u043e<\/h3>\n<p><code>getopts<\/code> \u0443\u043c\u0435\u0435\u0442 \u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0441\u043b\u0438\u043f\u0448\u0438\u0435\u0441\u044f \u0444\u043b\u0430\u0433\u0438: <code>-abc<\/code> \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a <code>-a -b -c<\/code>. \u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u043e\u0436\u0438\u0434\u0430\u0435\u0442\u0441\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u0440\u0430\u0437\u0431\u043e\u0440 \u043f\u0440\u0435\u043a\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0440\u043e\u0432\u043d\u043e \u0442\u0430\u043c:<\/p>\n<pre><code class=\"bash\"># optstring=\"a:b\" $ my.sh -ac   # OK: -a, -c \u043a\u0430\u043a \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043a\u0430 $ my.sh -abx  # -a, -b x $ my.sh -ab   # \u043e\u0448\u0438\u0431\u043a\u0430: -b \u0436\u0434\u0451\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442<\/code><\/pre>\n<p><strong>Edge-case<\/strong> \u2014 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 (<code>-5<\/code>) \u0432\u043d\u0435\u0437\u0430\u043f\u043d\u043e \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0442\u0441\u044f \u0437\u0430 \u0444\u043b\u0430\u0433. \u041d\u0430\u0447\u0438\u043d\u0430\u0439\u0442\u0435 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 \u043e\u043f\u0446\u0438\u0439 \u0441 <code>--<\/code>, \u0442\u043e\u0433\u0434\u0430 <code>getopts<\/code> \u043f\u0440\u0435\u043a\u0440\u0430\u0442\u0438\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u043f\u0440\u0438 \u0432\u0438\u0434\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043d\u0435-\u0444\u043b\u0430\u0433\u0430, \u0438 \u043c\u0438\u043d\u0443\u0441\u044b \u0432 \u0447\u0438\u0441\u043b\u0430\u0445 \u043e\u0441\u0442\u0430\u043d\u0443\u0442\u0441\u044f \u043d\u0435\u0442\u0440\u043e\u043d\u0443\u0442\u044b\u043c\u0438.<\/p>\n<h3>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c &#8212;long \u0431\u0435\u0437 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u0442\u0443\u043b\u043e\u0432<\/h3>\n<p>\u0427\u0430\u0441\u0442\u044b\u0439 \u043f\u0440\u0438\u0435\u043c: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0447\u0435\u0440\u0435\u0437 <code>case \"$1\"<\/code> \u043b\u043e\u0432\u0438\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b <code>--help<\/code>, <code>--version<\/code>, <code>--long=value<\/code>, \u0430 \u0437\u0430\u0442\u0435\u043c \u043e\u0442\u0434\u0430\u0451\u043c \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0432 <code>getopts<\/code> \u2014 \u0442\u0430\u043a \u043d\u0435 \u043b\u043e\u043c\u0430\u0435\u043c POSIX-\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0438 \u043d\u0435 \u0442\u0430\u0449\u0438\u043c GNU <code>getopt<\/code>.<\/p>\n<pre><code class=\"bash\">while [ $# -gt 0 ]; do   case \"$1\" in     --help) usage ;;     --output=*) opt_o=${1#*=}; shift ;;     --) shift; break ;;     # \u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u0442\u0438\u0440\u0435 \u2014 \u043a\u043e\u043d\u0435\u0446 \u043e\u043f\u0446\u0438\u0439     -*) break ;;            # \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u043e\u043f\u0446\u0438\u0438 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u0442 getopts     *)  break ;;   esac done  # \u0442\u0435\u043f\u0435\u0440\u044c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 getopts while getopts \":o:f:\" opt; do   ... done shift \"$((OPTIND-1))\"<\/code><\/pre>\n<p>\u0421\u0445\u0435\u043c\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u044f: \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u2014 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u0435 alias-\u044b \u0432\u0440\u043e\u0434\u0435 <code>--verbose\/-v<\/code>, \u0438\u043b\u0438 \u0432\u043e\u043e\u0431\u0449\u0435 YAML-\u043a\u043e\u043d\u0444\u0438\u0433 \u043f\u043e\u0441\u043b\u0435 <code>--config=path.yml<\/code>.<\/p>\n<h3>\u0424\u0430\u0439\u043b &gt; \u0444\u0430\u0439\u043b: \u043f\u0440\u0438\u043c\u0435\u0440 (-f input -o output)<\/h3>\n<p>\u0421\u043e\u0431\u0435\u0440\u0451\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 CLI-\u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440:<\/p>\n<pre><code class=\"bash\">#!\/usr\/bin\/env bash set -Eeuo pipefail trap 'echo \" \u0427\u0442\u043e-\u0442\u043e \u043f\u043e\u0448\u043b\u043e \u043d\u0435 \u0442\u0430\u043a \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 $LINENO\" &gt;&amp;2' ERR VERSION=\"1.2.3\"  usage() {   cat &lt;&lt;EOF ${0##*\/} \u2014 \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440\u0430.  \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:   -f FILE    \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0444\u0430\u0439\u043b (\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e)   -o FILE    \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0444\u0430\u0439\u043b (\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e)   -t TYPE    \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 (txt|json|xml), \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e txt   -v         \u0431\u043e\u043b\u0442\u043b\u0438\u0432\u044b\u0439 \u0440\u0435\u0436\u0438\u043c   -h         \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0439 \u0441\u043f\u0440\u0430\u0432\u043a\u0438 EOF }  infile= outfile= outtype=txt verbose=false  while getopts \":f:o:t:vh\" opt; do   case \"$opt\" in     f) infile=$OPTARG ;;     o) outfile=$OPTARG ;;     t) outtype=$OPTARG ;;     v) verbose=true ;;     h) usage; exit 0 ;;     :) echo \" \u041e\u043f\u0446\u0438\u044f -$OPTARG \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\" &gt;&amp;2; usage; exit 2 ;;     \\?) echo \" \u041d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430\u044f \u043e\u043f\u0446\u0438\u044f -$OPTARG\" &gt;&amp;2; usage; exit 2 ;;   esac done shift \"$((OPTIND-1))\"  # sanity-check [ -n \"$infile\" ]  || { echo \"\u041d\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430\" &gt;&amp;2; exit 2; } [ -n \"$outfile\" ] || { echo \"\u041d\u0435\u0442 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430\" &gt;&amp;2; exit 2; } [ -r \"$infile\" ]  || { echo \"\u0424\u0430\u0439\u043b '$infile' \u043d\u0435 \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f\" &gt;&amp;2; exit 3; }  $verbose &amp;&amp; echo \"\u21e2 \u041a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u044e $infile \u2192 $outfile ($outtype)\"  case \"$outtype\" in   txt)   cp \"$infile\" \"$outfile\" ;;              # stub   json)  jq -R -s '.' \"$infile\" &gt;\"$outfile\" ;;   # example   xml)   iconv -f utf8 -t utf16 \"$infile\" &gt;\"$outfile\" ;;   *) echo \"\u041d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 $outtype\" &gt;&amp;2; exit 4 ;; esac<\/code><\/pre>\n<p>\u0412 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u043d \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0441\u0442\u0440\u043e\u0433\u0438\u0439 \u0440\u0435\u0436\u0438\u043c Bash (<code>set -Eeuo pipefail<\/code>) \u0438 \u043b\u043e\u0432\u0438\u043c \u043b\u044e\u0431\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0447\u0435\u0440\u0435\u0437 <code>trap<\/code>, \u0447\u0442\u043e\u0431\u044b \u0441\u0440\u0430\u0437\u0443 \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c, \u043d\u0430 \u043a\u0430\u043a\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0441\u0451 \u0443\u043f\u0430\u043b\u043e. \u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>usage<\/code> \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u0442 \u0441\u043f\u0440\u0430\u0432\u043a\u0443. \u0414\u0430\u043b\u0435\u0435 <code>getopts<\/code> \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u0442 \u0444\u043b\u0430\u0433\u0438: <code>-f<\/code> \u2014 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0444\u0430\u0439\u043b, <code>-o<\/code> \u2014 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439, <code>-t<\/code> \u2014 \u0444\u043e\u0440\u043c\u0430\u0442 (txt-|json|xml, \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e txt), <code>-v<\/code> \u2014 \u0431\u043e\u043b\u0442\u043b\u0438\u0432\u044b\u0439 \u0440\u0435\u0436\u0438\u043c, <code>-h<\/code> \u2014 \u043f\u043e\u043c\u043e\u0449\u044c. \u041f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u0431\u043e\u0440\u043a\u0438 \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0447\u0442\u043e \u0444\u0430\u0439\u043b\u044b \u0437\u0430\u0434\u0430\u043d\u044b \u0438 \u0432\u0445\u043e\u0434 \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f. \u0415\u0441\u043b\u0438 \u0432\u043a\u043b\u044e\u0447\u0451\u043d <code>-v<\/code>, \u043f\u0438\u0448\u0435\u0442, \u0447\u0442\u043e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442. \u0414\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0443: <code>txt<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u0444\u0430\u0439\u043b, <code>json<\/code> \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0432 JSON \u0447\u0435\u0440\u0435\u0437 <code>jq<\/code>, <code>xml<\/code> \u043f\u0435\u0440\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 \u0432 UTF-16 \u0441 <code>iconv<\/code>. \u041d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u2014 \u0432\u044b\u0445\u043e\u0434 \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439.  <\/p>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a: \u0434\u0432\u043e\u0435\u0442\u043e\u0447\u0438\u0435, \u0432\u043e\u043f\u0440\u043e\u0441, \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u043a \u0436\u0438\u0437\u043d\u0438<\/h3>\n<p><code>getopts<\/code> \u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0442 \u043e \u043d\u0435\u0448\u0442\u0430\u0442\u043d\u044b\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u0445 \u0434\u0432\u0443\u043c\u044f \u043c\u0430\u0440\u043a\u0435\u0440\u0430\u043c\u0438:<\/p>\n<ul>\n<li>\n<p><code>?<\/code> \u2014 \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430\u044f \u043e\u043f\u0446\u0438\u044f (<code>-z<\/code>).<\/p>\n<\/li>\n<li>\n<p><code>:<\/code> \u2014 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 (<code>-f<\/code> \u0431\u0435\u0437 \u0444\u0430\u0439\u043b\u0430), \u0435\u0441\u043b\u0438 \u0432 <code>optstring<\/code> \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043e \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 <code>:<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"bash\">while getopts \":f:o:\" opt; do   case $opt in     f) infile=$OPTARG ;;     o) outfile=$OPTARG ;;     :) printf &gt;&amp;2 ' %s: \u043e\u043f\u0446\u0438\u044f -%s \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\\n' \"$0\" \"$OPTARG\" ;;     \\?) printf &gt;&amp;2 ' %s: \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430\u044f \u043e\u043f\u0446\u0438\u044f -%s\\n'   \"$0\" \"$OPTARG\" ;;   esac done<\/code><\/pre>\n<p>\u0412 <code>case<\/code> \u043b\u0443\u0447\u0448\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e <code>\\?)<\/code>, \u0430 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e <code>?<\/code>, \u0438\u043d\u0430\u0447\u0435 shell \u0432\u043e\u0441\u043f\u0440\u0438\u043c\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u043a\u0430\u043a wildcard.<\/p>\n<h3>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c: Bats &amp; shellcheck<\/h3>\n<ol>\n<li>\n<p><strong>ShellCheck<\/strong> \u2014 \u043b\u0438\u043d\u0442\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e \u043b\u043e\u0432\u0438\u0442 \u00ab\u0443\u0445 \u0442\u044b, \u0434\u0432\u043e\u0439\u043d\u044b\u0435 \u043a\u0430\u0432\u044b\u0447\u043a\u0438 \u0437\u0430\u0431\u044b\u043b\u00bb \u0438 <code>[ $var == foo ]<\/code> \u0431\u0435\u0437 \u043a\u0430\u0432\u044b\u0447\u0435\u043a.<\/p>\n<\/li>\n<li>\n<p><strong>Bats-core<\/strong> \u2014 unit-\u0442\u0435\u0441\u0442\u044b \u0434\u043b\u044f bash. \u0421\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a:<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"bash\">@test \"\u0432\u044b\u0432\u043e\u0434\u0438\u0442 usage \u0431\u0435\u0437 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432\" {   run .\/convert.sh   [ \"$status\" -eq 2 ]   [[ \"${lines[0]}\" =~ Usage ]] }  @test \"\u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0444\u0430\u0439\u043b \u0432 txt\" {   run .\/convert.sh -f sample.in -o sample.out   [ \"$status\" -eq 0 ]   cmp sample.in sample.out }<\/code><\/pre>\n<h3>\u041a\u043e\u0433\u0434\u0430 getopts \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0451\u0442 \u0445\u0432\u0430\u0442\u0430\u0442\u044c<\/h3>\n<p><code>getopts<\/code> \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 90 % \u0437\u0430\u0434\u0430\u0447: \u043e\u043d \u0432\u0441\u0442\u0440\u043e\u0435\u043d \u0432 \u043b\u044e\u0431\u043e\u0439 shell, \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0438 \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0434\u0430\u0436\u0435 \u043d\u0430 BusyBox-\u043f\u0440\u043e\u0448\u0438\u0432\u043a\u0438. \u041d\u043e \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0443\u0436\u0435\u043d \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 <code>--long<\/code>-\u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441, \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u043b\u0438 \u0433\u0440\u0430\u043c\u043e\u0442\u043d\u043e\u0435 \u044d\u043a\u0440\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0431\u0435\u043b\u043e\u0432, \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0437\u0432\u0430\u0442\u044c GNU <code>getopt<\/code>. \u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043d\u0430 macOS \u0438 Alpine \u0441\u0442\u0430\u0432\u044f\u0442 \u0443\u0440\u0435\u0437\u0430\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e: \u0441\u043a\u0440\u0438\u043f\u0442 \u0440\u0438\u0441\u043a\u0443\u0435\u0442 \u0441\u043b\u043e\u043c\u0430\u0442\u044c\u0441\u044f \u0438\u043b\u0438 \u043d\u0430\u0447\u0430\u0442\u044c \u00ab\u0433\u043b\u043e\u0442\u0430\u0442\u044c\u00bb \u043f\u0443\u0441\u0442\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u2014 \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 BashGuide \u0441\u043e\u0432\u0435\u0442\u0443\u0435\u0442 \u0434\u0435\u0440\u0436\u0430\u0442\u044c\u0441\u044f \u043e\u0442 <code>getopt(1)<\/code> \u043f\u043e\u0434\u0430\u043b\u044c\u0448\u0435.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 CLI \u043e\u0431\u0440\u0430\u0441\u0442\u0430\u0435\u0442 \u0434\u0435\u0441\u044f\u0442\u043a\u043e\u043c \u0444\u043b\u0430\u0433\u043e\u0432, autocompletion \u0438 \u0441\u0442\u0440\u043e\u0433\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u043e\u0439 \u0442\u0438\u043f\u043e\u0432, \u043b\u0443\u0447\u0448\u0435 \u0441\u0440\u0430\u0437\u0443 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0451\u0440\u0442\u043a\u0443 \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/argbash.readthedocs.io\/en\/latest\/\" rel=\"noopener noreferrer nofollow\">Argbash<\/a>. \u041e\u043d \u0432\u044b\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0441\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u043c <code>--long<\/code>, bash-completion \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439 \u0432\u0440\u043e\u0434\u0435 <code>INT&gt;=0<\/code> \u0438\u043b\u0438 <code>PATH<\/code>. \u041c\u0438\u043d\u0443\u0441 \u043e\u0434\u0438\u043d: \u0441\u0430\u043c\u0430 \u0443\u0442\u0438\u043b\u0438\u0442\u0430 Argbash \u043d\u0443\u0436\u043d\u0430 \u043b\u0438\u0448\u044c \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0431\u0438\u043b\u0434\u0430, \u0442\u0430\u043a \u0447\u0442\u043e \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u043d\u0435\u0442, \u043d\u043e \u0432 CI \u0435\u0451 \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c.<\/p>\n<hr\/>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<ol>\n<li>\n<p><strong>\u0411\u0435\u0440\u0438\u0442\u0435 <\/strong><code><strong>getopts<\/strong><\/code><strong> \u043f\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u0443<\/strong>: \u0434\u043b\u044f 90 % \u0443\u0442\u0438\u043b\u0438\u0442 \u0445\u0432\u0430\u0442\u0438\u0442 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0445 \u0444\u043b\u0430\u0436\u043a\u043e\u0432 \u0438 5-10 \u0441\u0442\u0440\u043e\u043a \u043a\u043e\u0434\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u0435 \u0431\u043e\u0439\u0442\u0435\u0441\u044c \u00ab&#8212;long\u00bb<\/strong> \u2014 \u0441\u043c\u0435\u0448\u0430\u043d\u043d\u044b\u0439 \u043f\u0430\u0440\u0441\u0438\u043d\u0433 (pre-loop + <code>getopts<\/code>) \u0440\u0435\u0448\u0430\u0435\u0442 \u0438 \u043d\u0435 \u043b\u043e\u043c\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u043c\u043e\u0441\u0442\u044c.<\/p>\n<\/li>\n<li>\n<p><strong>\u0425\u043e\u0442\u0438\u0442\u0435 UX \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 <\/strong><code><strong>kubectl<\/strong><\/code><strong> \u2014 \u0438\u0434\u0438\u0442\u0435 \u0432 <\/strong><code><strong>getopt_long<\/strong><\/code><strong> \u0438\u043b\u0438 Argbash<\/strong>, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044f \u0440\u0438\u0441\u043a\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u0438 \u044d\u0442\u0438\u0445 \u0442\u0443\u043b\u043e\u0432 \u0443 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0430\u0443\u0434\u0438\u0442\u043e\u0440\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p><strong>\u0411\u0435\u0439\u0442\u0435 \u043f\u043e \u0440\u0443\u043a\u0430\u043c \u0441\u0435\u0431\u044f (\u0438 \u043a\u043e\u043b\u043b\u0435\u0433) \u0437\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 <\/strong><code><strong>set -euo pipefail<\/strong><\/code><strong> \u0438 \u043d\u0435\u0437\u0430\u043a\u0440\u044b\u0442\u044b\u0435 \u043a\u0430\u0432\u044b\u0447\u043a\u0438<\/strong> \u2014 \u044d\u0442\u043e \u0434\u0435\u0448\u0435\u0432\u043b\u0435, \u0447\u0435\u043c \u043d\u043e\u0447\u043d\u043e\u0439 \u0438\u043d\u0446\u0438\u0434\u0435\u043d\u0442.<\/p>\n<\/li>\n<li>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0439\u0442\u0435<\/strong>: ShellCheck + Bats \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u044e\u0442 80 % \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0444\u0430\u043a\u0430\u043f\u043e\u0432 \u0435\u0449\u0451 \u0434\u043e code-review.<\/p>\n<\/li>\n<\/ol>\n<hr\/>\n<p>\u0412\u0441\u0435\u0445 \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a <a href=\"https:\/\/otus.pw\/EUxB\/\" rel=\"noopener noreferrer nofollow\"><strong>\u00ab\u041f\u0430\u043c\u044f\u0442\u044c \u0432 Linux: Cache, swap, dirty pages\u00bb<\/strong><\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0439\u0434\u0451\u0442 <strong><em>16 \u0438\u044e\u043d\u044f \u0432 20:00<\/em><\/strong>, \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u043a\u0443\u0440\u0441\u0430 <a href=\"https:\/\/otus.pw\/oa3X\/\" rel=\"noopener noreferrer nofollow\"><strong>\u00abAdministrator Linux. Professional\u00bb<\/strong><\/a>, \u0441\u0442\u0430\u0440\u0442\u0443\u044e\u0449\u0435\u0433\u043e <strong><em>26 \u0438\u044e\u043d\u044f<\/em><\/strong>.<\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0441\u0432\u043e\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u043c \u043f\u0440\u043e\u0439\u0442\u0438 <a href=\"https:\/\/otus.pw\/MnDL\/\" rel=\"noopener noreferrer nofollow\"><strong>\u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/a>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/911338\/\"> https:\/\/habr.com\/ru\/articles\/911338\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-463132","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/463132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=463132"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/463132\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=463132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=463132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=463132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}