Options for UNIX types: UNIX has the advantage that standard UNIX commands have the same command-line format at execution time: command-options parameters if the shell is executed in the same format, the Bourne shell provides a A statement that takes and processes command-line options, that is, the getopts statement. The format of the statement is: getopts option_string variable where option_string contains a valid single-character option. If the getopts command finds a hyphen on the command line, it is compared with the word descriptor option_string after the hyphen. If there is a match, set the value of the variable variable to this option. If there is no match, then the variable is set to?. When Getopts discovers that there are no characters behind the hyphen, it returns a nonzero status value. The shell program can use the return value of the getopts to create a loop. The following code illustrates how the date command handles various options using the getopts command, which adds many new options in addition to completing the UNIX standard command date feature.
#新date程序
If [$#-lt 1]
Then
Date
Else
While Getopts Mdydhmstjjwahr OPTION
Do
Case $OPTION
Inch
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 ' +%a ';;
h) Date ' +%h ';;
R) Date ' +%r ';;
/?) echo "Invalid option! $OPTION";;
Esac
Done
Fi Sometimes the option also has a value, and the getopts command also supports this feature. You need to add a colon to the option_string after you select the letter. When the getopts command finds a colon, the value is read from the command line after the option. If the value exists, then there will be a special variable in the Optarg. If the value does not exist, the getopts command holds a question mark in the Optarg and displays a message on the standard error output. In the following example, the implementation copies a file and assigns a new name to the file. The-c option specifies the number of copies of the program, and the-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
Inch
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))
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.