Linux shell scripts pass parameter values through parameter names

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.