The usual way to write shell scripts is to use $1,$2 .... This way to receive parameters, however, this method of receiving parameters is not only easy to forget and not easy to understand and maintain. The commands that are commonly used by Linux can specify parameter names and parameter values, but how can we get parameter values in the same way that our shell scripts use parameter names and parameter values? Rather than by $1,$2 this way to get it. The following example defines two ways to get parameter values for a short parameter name and a long parameter number. In fact, according to the characteristics provided by getopt to organize.
#!/bin/SH#说明show_usage="args: [-L,-R,-B,-w]\[--local-repository=,--repository-url=,--backup-dir=,--webdir=]"#参数 # Local warehouse directory Opt_localrepo=""# git repository urlopt_url=""# backup Directory Opt_backupdir=""# web Directory opt_webdir=""Getopt_args=`getopt-O l:r:b:W:-al local-repository:,repository-url:,backup-dir:, WebDir:--"[email protected]"' eval set--"$GETOPT _args"#获取参数 while[-N" $" ] Do Case " $" inch-l|--local-repository) opt_localrepo=$2;Shift 2;; -r|--repository-url) opt_url=$2;Shift 2;; -b|--backup-dir) opt_backupdir=$2;Shift 2;; -W|--webdir) opt_webdir=$2;Shift 2;; --) break;; *)Echo$1,$2, $show _usage; Esac Doneif[[-Z $opt _localrepo | |-Z $opt _url | |-Z $opt _backupdir | |-Z $opt _webdir]]; Then Echo$show _usageEcho "Opt_localrepo: $opt _localrepo, Opt_url: $opt _url, Opt_backupdir: $opt _backupdir, Opt_webdir: $opt _webdir"Exit0fi
The acquisition of parameter values in this way is easier to understand and maintain.
Linux shell scripts pass parameter values through parameter names