Shell getopt usage

Source: Internet
Author: User
Obtain UNIX options: one advantage of UNIX is that standard Unix commands have the same command line format during execution: Command-options parameters. If the shell program is executed in the preceding format, the Bourne shell provides a statement for obtaining and processing command line options, that is, the getopts statement. The statement format is getopts option_string variable. option_string contains a valid single-character option. If the getopts Command finds a hyphen in the command line, it will use the character after the hyphen to compare with option_string. If there is a match, set the variable value to this option. If no match exists, set variable ?. If getopts finds that there are no characters after a hyphen, a non-zero status value is returned. The return value of getopts can be used in Shell programs to create a loop. The following code illustrates how to use the getopts command to process various options in the date command. In addition to the date function of the standard Unix Command, this program also adds many new options.
# New date program
If [$ #-LT 1]
Then
Date
Else
While getopts mdydhmstj1_ahr Option
Do
Case $ Option
In
M) date '+ % m ';;
D) Date '+ % d ';;
Y) date '+ % y ';;
D) Date '+ % d ';;
H0date '+ % H ';;
M) date '+ % m ';;
S) date '+ % s ';;
T) date '+ % T ';;
J) date '+ % J ';;
J) date '+ % Y % J ';;
W) date '+ % W ';;
A) date '+ % ';;
H) date '+ % H ';;
R) date '+ % R ';;
/?) Echo "invalid option! $ Option ";;
Esac
Done
Sometimes the fi option also contains a value. The getopts command also supports this function. In this case, you need to add a colon after the option letter in option_string. When the getopts Command finds a colon, it reads the value from the option on the command line. If this value exists, it will be included in a special variable optarg. If this value does not exist, the getopts command will store a question mark in the optarg and display a message on the standard error output. The following example copies an object and assigns a new name to the object. -C option specifies the number of times the program is copied.-V option requires that the file name of the newly created file be displayed. # -- Copy program copies = 1
Verbose = N
While getopts VC: Option
Do
Case $ Option
In
C) copies = $ optarg ;;
V) verbose = y ;;
/?) Echo "invalid parameter! "
Exit 1 ;;
Esac
Done
If [$ optind-GT $ #]
Then
Echo "no file name specified"
Exit 2
Fi
Shift 'expr $ optind-1'
File = $1
Copy = 0
While [$ copies-GT $ copy]
Do
Copy = 'expr $ copy + 1'
CP $ File $ {file }$ {copy}
If [verbose = y}
Then
Echo $ {file }$ {copy}
Fi
Done Another example:#! /Bin/bash
While getopts "AB: CD:" option
# B and D take arguments
#
Do
Case $ option in
A) echo-e "A = $ optind ";;
B) echo-e "B = $ optind $ optarg ";;
C) echo-e "c = $ optind ";;
D) echo-e "d = $ optind $ optarg ";;
Esac
Done
Shift $ ($ optind-1 ))
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.