JavaScript is required to for searching.
跳过导航链接
退出打印视图
手册页第 1 部分:用户命令     Oracle Solaris 11.1 Information Library (简体中文)
为本文档评分
search filter icon
search icon

文档信息

前言

简介

User Commands

acctcom(1)

adb(1)

addbib(1)

admin(1)

alias(1)

allocate(1)

amt(1)

appcert(1)

apptrace(1)

apropos(1)

ar(1)

arch(1)

as(1)

asa(1)

at(1)

atq(1)

atrm(1)

audioconvert(1)

audioctl(1)

audioplay(1)

audiorecord(1)

audiotest(1)

auths(1)

auto_ef(1)

awk(1)

banner(1)

basename(1)

basename(1B)

batch(1)

bc(1)

bdiff(1)

bfs(1)

bg(1)

biff(1B)

break(1)

builtin(1)

cal(1)

calendar(1)

case(1)

cat(1)

cd(1)

cdc(1)

cdrw(1)

chdir(1)

checkeq(1)

checknr(1)

chgrp(1)

chkey(1)

chmod(1)

chown(1)

chown(1B)

ckdate(1)

ckgid(1)

ckint(1)

ckitem(1)

ckkeywd(1)

ckpath(1)

ckrange(1)

ckstr(1)

cksum(1)

cktime(1)

ckuid(1)

ckyorn(1)

clear(1)

cmp(1)

col(1)

comb(1)

comm(1)

command(1)

compress(1)

continue(1)

cp(1)

cpio(1)

cpp(1)

cputrack(1)

crle(1)

crontab(1)

csh(1)

csplit(1)

ct(1C)

ctags(1)

ctrun(1)

ctstat(1)

ctwatch(1)

cu(1C)

cut(1)

date(1)

dc(1)

deallocate(1)

decrypt(1)

delta(1)

deroff(1)

df(1B)

dhcpinfo(1)

diff(1)

diff3(1)

diffmk(1)

digest(1)

digestp(1)

dircmp(1)

dirname(1)

dirs(1)

dis(1)

disown(1)

dispgid(1)

dispuid(1)

dos2unix(1)

dpost(1)

du(1)

du(1B)

dump(1)

dumpcs(1)

dumpkeys(1)

echo(1)

echo(1B)

ed(1)

edit(1)

egrep(1)

eject(1)

elfdump(1)

elfedit(1)

elffile(1)

elfsign(1)

elfwrap(1)

encrypt(1)

enhance(1)

env(1)

eqn(1)

errange(1)

errdate(1)

errgid(1)

errint(1)

erritem(1)

error(1)

errpath(1)

errstr(1)

errtime(1)

erruid(1)

erryorn(1)

eval(1)

ex(1)

exec(1)

exit(1)

expand(1)

export(1)

exportfs(1B)

expr(1)

expr(1B)

exstr(1)

factor(1)

false(1)

fastboot(1B)

fasthalt(1B)

fc(1)

fg(1)

fgrep(1)

file(1)

file(1B)

filebench(1)

filep(1)

filesync(1)

filofaxp(1)

find(1)

finger(1)

fmt(1)

fmtmsg(1)

fold(1)

for(1)

foreach(1)

franklinp(1)

from(1B)

ftp(1)

function(1)

gcore(1)

gencat(1)

geniconvtbl(1)

genmsg(1)

get(1)

getconf(1)

getfacl(1)

getlabel(1)

getopt(1)

getoptcvt(1)

getopts(1)

gettext(1)

gettxt(1)

getzonepath(1)

glob(1)

goto(1)

gprof(1)

grep(1)

groups(1)

groups(1B)

grpck(1B)

hash(1)

hashcheck(1)

hashmake(1)

hashstat(1)

head(1)

helpdate(1)

helpgid(1)

helpint(1)

helpitem(1)

helppath(1)

helprange(1)

helpstr(1)

helptime(1)

helpuid(1)

helpyorn(1)

hist(1)

history(1)

hostid(1)

hostname(1)

i386(1)

i486(1)

iconv(1)

if(1)

indxbib(1)

install(1B)

ipcrm(1)

ipcs(1)

isainfo(1)

isalist(1)

jobs(1)

join(1)

jsh(1)

kbd(1)

kdestroy(1)

keylogin(1)

keylogout(1)

kill(1)

kinit(1)

klist(1)

kmdb(1)

kmfcfg(1)

kpasswd(1)

krb5-config(1)

ksh(1)

ksh88(1)

ksh93(1)

ktutil(1)

kvno(1)

lari(1)

last(1)

lastcomm(1)

ld(1)

ldapadd(1)

ldapdelete(1)

ldaplist(1)

ldapmodify(1)

ldapmodrdn(1)

ldapsearch(1)

ldd(1)

ld.so.1(1)

let(1)

lex(1)

lgrpinfo(1)

limit(1)

line(1)

list_devices(1)

listusers(1)

llc2_autoconfig(1)

llc2_config(1)

llc2_stats(1)

ln(1)

ln(1B)

loadkeys(1)

locale(1)

localedef(1)

logger(1)

logger(1B)

login(1)

logname(1)

logout(1)

look(1)

lookbib(1)

lorder(1)

ls(1)

ls(1B)

m4(1)

mac(1)

mach(1)

machid(1)

madv.so.1(1)

mail(1)

Mail(1B)

mail(1B)

mailcompat(1)

mailp(1)

mailq(1)

mailstats(1)

mailx(1)

make(1S)

makekey(1)

man(1)

mconnect(1)

mcs(1)

mdb(1)

mesg(1)

mkdir(1)

mkmsgs(1)

mkstr(1B)

mktemp(1)

moe(1)

more(1)

mp(1)

mpss.so.1(1)

msgcc(1)

msgcpp(1)

msgcvt(1)

msgfmt(1)

msggen(1)

msgget(1)

mt(1)

mv(1)

nawk(1)

nc(1)

ncab2clf(1)

ncakmod(1)

neqn(1)

netcat(1)

newform(1)

newgrp(1)

newsp(1)

newtask(1)

nice(1)

nl(1)

nm(1)

nohup(1)

notify(1)

nroff(1)

od(1)

on(1)

onintr(1)

optisa(1)

pack(1)

packagemanager(1)

page(1)

pagesize(1)

pargs(1)

passwd(1)

paste(1)

patch(1)

pathchk(1)

pax(1)

pcat(1)

pcred(1)

perl(1)

pfbash(1)

pfcsh(1)

pfexec(1)

pfiles(1)

pfksh(1)

pflags(1)

pfsh(1)

pftcsh(1)

pfzsh(1)

pg(1)

pgrep(1)

pkcs11_inspect(1)

pkg(1)

pkgdepend(1)

pkgdiff(1)

pkgfmt(1)

pkginfo(1)

pkglint(1)

pkgmerge(1)

pkgmk(1)

pkgmogrify(1)

pkgparam(1)

pkgproto(1)

pkgrecv(1)

pkgrepo(1)

pkgsend(1)

pkgsign(1)

pkgtrans(1)

pkill(1)

pklogin_finder(1)

pktool(1)

plabel(1)

pldd(1)

plgrp(1)

plimit(1)

pmadvise(1)

pmap(1)

pm-updatemanager(1)

popd(1)

ppgsz(1)

ppriv(1)

pr(1)

praliases(1)

prctl(1)

preap(1)

print(1)

printenv(1B)

printf(1)

priocntl(1)

proc(1)

prof(1)

profiles(1)

projects(1)

prs(1)

prt(1)

prun(1)

ps(1)

ps(1B)

psig(1)

pstack(1)

pstop(1)

ptime(1)

ptree(1)

pushd(1)

pvs(1)

pwait(1)

pwd(1)

pwdx(1)

radadrgen(1)

ranlib(1)

rcapstat(1)

rcp(1)

read(1)

readonly(1)

red(1)

refer(1)

regcmp(1)

rehash(1)

remote_shell(1)

remsh(1)

renice(1)

repeat(1)

reset(1B)

return(1)

rksh(1)

rksh88(1)

rlogin(1)

rm(1)

rmail(1)

rmdel(1)

rmdir(1)

rmformat(1)

rmmount(1)

rmumount(1)

roffbib(1)

roles(1)

rpcgen(1)

rpm2cpio(1)

rsh(1)

runat(1)

rup(1)

rup(1C)

ruptime(1)

rusage(1B)

rusers(1)

rwho(1)

sact(1)

sar(1)

sccs(1)

sccs-admin(1)

sccs-cdc(1)

sccs-comb(1)

sccs-delta(1)

sccsdiff(1)

sccs-get(1)

sccs-help(1)

sccshelp(1)

sccs-prs(1)

sccs-prt(1)

sccs-rmdel(1)

sccs-sact(1)

sccs-sccsdiff(1)

sccs-unget(1)

sccs-val(1)

scp(1)

script(1)

sdiff(1)

sed(1)

sed(1B)

select(1)

set(1)

setenv(1)

setfacl(1)

setlabel(1)

setpgrp(1)

settime(1)

sftp(1)

sh(1)

shcomp(1)

shell_builtins(1)

shift(1)

shutdown(1B)

size(1)

sleep(1)

soelim(1)

sort(1)

sortbib(1)

sotruss(1)

source(1)

sparc(1)

spell(1)

spellin(1)

split(1)

srchtxt(1)

ssh(1)

ssh-add(1)

ssh-agent(1)

ssh-http-proxy-connect(1)

ssh-keygen(1)

ssh-keyscan(1)

ssh-socks5-proxy-connect(1)

stop(1)

strchg(1)

strconf(1)

strings(1)

strip(1)

stty(1)

stty(1B)

sum(1)

sum(1B)

sun(1)

suspend(1)

svcprop(1)

svcs(1)

switch(1)

symorder(1)

sys-suspend(1)

sysV-make(1)

t300(1)

t300s(1)

t4014(1)

t450(1)

tabs(1)

tail(1)

talk(1)

tar(1)

tbl(1)

tcopy(1)

tee(1)

tek(1)

telnet(1)

test(1)

test(1B)

tftp(1)

time(1)

timemanp(1)

times(1)

timesysp(1)

timex(1)

tip(1)

touch(1)

touch(1B)

tplot(1)

tput(1)

tr(1)

tr(1B)

trap(1)

troff(1)

true(1)

truss(1)

tset(1B)

tsort(1)

tty(1)

type(1)

typeset(1)

ul(1)

ulimit(1)

umask(1)

unalias(1)

uname(1)

uncompress(1)

unexpand(1)

unget(1)

unhash(1)

unifdef(1)

uniq(1)

units(1)

unix2dos(1)

unlimit(1)

unpack(1)

unset(1)

unsetenv(1)

until(1)

updatehome(1)

uptime(1)

userattr(1)

users(1B)

uucp(1C)

uudecode(1C)

uuencode(1C)

uuglist(1C)

uulog(1C)

uuname(1C)

uupick(1C)

uustat(1C)

uuto(1C)

uux(1C)

vacation(1)

val(1)

valdate(1)

valgid(1)

valint(1)

valpath(1)

valrange(1)

valstr(1)

valtime(1)

valuid(1)

valyorn(1)

vc(1)

vedit(1)

ver(1)

vgrind(1)

vi(1)

view(1)

vipw(1B)

volcheck(1)

volrmmount(1)

w(1)

wait(1)

wc(1)

what(1)

whatis(1)

whence(1)

whereis(1B)

which(1)

while(1)

who(1)

whoami(1B)

whocalls(1)

whois(1)

write(1)

xargs(1)

xgettext(1)

xstr(1)

yacc(1)

yes(1)

ypcat(1)

ypmatch(1)

yppasswd(1)

ypwhich(1)

zcat(1)

zlogin(1)

zonename(1)

zonestat(1)

请告诉我们如何提高我们的文档:
过于简略
不易阅读或难以理解
重要信息缺失
错误的内容
需要翻译的版本
其他
Your rating has been updated
感谢您的反馈!

您的反馈将非常有助于我们提供更好的文档。 您是否愿意参与我们的内容改进并提供进一步的意见?

getopts

- 解析实用程序选项

用法概要

/usr/bin/getopts optstring  name [arg...]

sh

getopts optstring name [argument]...

ksh88

getopts optstring name [arg]...

ksh

getopts [-a name] optstring name [arg]...

描述

/usr/bin/getopts

getopts 实用程序可用于从参数列表中检索选项和选项参数。

每次被调用时,getopts 实用程序都会将下一选项的值放入 name 操作数指定的 shell 变量中,将要处理的下一参数的索引放入 shell 变量 OPTIND 中。每当调用 shell 时,OPTIND 都将初始化为 1

如果某个选项需要选项参数,getopts 实用程序会将其放置在 shell 变量 OPTARG 中。如果未找到选项,或找到的选项没有选项参数,则取消设置 OPTARG

如果在需要选项字符的位置发现了 optstring 操作数中未包含的选项字符,由 name 指定的 shell 变量将设置为问号 ( ? ) 字符。在此情况下,如果 optstring 中的第一个字符是冒号 (:),则 shell 变量 OPTARG 将设置为所找到的选项字符,但不会有任何输出写入到标准错误;其他情况下,将取消设置 shell 变量 OPTARG 并将一条诊断信息写入到标准错误。此情况将被视为在将参数提供给调用应用程序时检测到的错误,而不是 getopts 处理中的错误。

如果缺少某个选项参数:

当遇到选项结尾时,getopts 实用程序将以一个大于零的返回值退出;shell 变量 OPTIND 将设置为第一个非选项参数的索引(如果第一个 - - 参数之前没有其他非选项参数出现,则会将该参数视为选项参数)或设置为值 $# + 1(如果没有非选项参数);name 变量将设置为问号符号。以下任意项都表示选项的结尾:特殊选项 - -,发现未以 - 开头的参数,或遇到错误。

shell 变量 OPTINDOPTARG 对于 getopts 的调用者而言是本地的,且缺省情况下不会导出。

name 操作数指定的 shell 变量、OPTINDOPTARG 会影响当前的 shell 执行环境。

如果应用程序将 OPTIND 设置为值 1,则可使用一个新的参数集:当前的位置参数或新的 arg 值均可。对于尝试在单个 shell 执行环境中多次调用 getopts 的其他情况,如果所使用的参数(位置参数或 arg 操作数)不是在所有调用中都相同,或者使用了修改为非 1 值的 OPTIND 值,则会产生意外的结果。

sh

getopts 是一个内置 Bourne shell 命令,用于解析位置参数和检查有效选项。请参见 sh(1)。它支持命令语法标准的所有适用规则(请参见 Intro(1) 中的规则 3 至 10)。应该使用该命令来替代 getopt 命令。

optstring 必须包含使用 getopts 的命令可以识别的选项字母。如果某个字母后面跟有冒号,则该选项需要有一个参数或一组参数,必须使用空格将其与参数隔开。

每次被调用时,getopts 会将下一个选项放在 shell 变量 name 中,将下一个要处理的参数的索引放在 shell 变量 OPTIND 中。只要调用 shell 或 shell 脚本,就会将 OPTIND 初始化为 1

如果某个选项需要选项参数,getopts 会将其放置在 shell 变量 OPTARG 中。

如果遇到非法选项,会将 ? 放置在 name 中。

当遇到选项的结尾时,getopts 将以非零退出状态退出。可使用特殊选项 来限定选项的结尾。

缺省情况下,getopts 解析位置参数。如果在 (argument . . .) getopts 命令行上指定了额外的参数 (argument ., . .),getopts 将改而对它们进行解析。

/usr/lib/getoptcvt 读取 filename 中的 shell 脚本,将其转换为使用 getopts 而不是 getopt,然后在标准输出中写入结果。

这样,所有新命令将遵循 Intro(1) 中介绍的命令语法标准,它们应该使用 getoptsgetopt 来解析位置参数和检查对该命令有效的选项。

如果 getopts 遇到不包括在 optstring 中的选项字母,将会在标准错误上输出一条错误消息。

尽管在当前实现中,允许放宽以下命令语法规则(请参见 Intro(1)),但也不应使用这些语法规则,因为系统的将来发行版可能不支持它们。如下面的“示例”部分所示,-a-b 均为选项,选项 - o 需要一个选项参数。

以下示例违反了规则 5:具有选项参数的选项不得与其他选项组合在一起:

example% cmd -aboxxx filename

以下示例违反了规则 6:在接受选项参数的选项后面必须有空格:

example% cmd -ab oxxx filename

更改 shell 变量 OPTIND 的值或解析不同的参数集合可能会导致意外结果。

ksh88

检查 arg 是否具有合法选项。如果省略了 arg,则会使用位置参数。选项参数以 +- 开头。未以 +- 开头的选项或者参数 表示选项的结尾。optstring 包含 getopts 可识别的字母。如果字母后面跟有一个 :,则该选项需要有一个参数。选项与参数之间可以用空格隔开。

每次在有前缀 + 的情况下被调用时,如果 arg+ 开头,则 getopts 会将它发现的下一个选项字母放置在变量 name 中。下一个 arg 的索引存储在 OPTIND 中。选项参数(如果有)存储在 OPTARG 中。

optstring 中的前导 : 将导致 getopts 将无效选项的字母存储在 OPTARG 中,并针对未知选项将 name 设置为 ?,而在缺少某个必需的选项时将 name 设置为 :。其他情况下,getopts 将显示一条错误消息。如果没有更多选项,则退出状态是非零的

getopts 支持传统的单字符短选项和由 Sun 的命令行接口范例 (Command Line Interface Paradigm, CLIP) 定义的长选项。

每个长选项都是短选项的别名,是在紧跟在其等效短选项后的括号中指定的。例如,可以使用以下脚本行将长选项 file 指定为短选项 f 的别名:

getopts "f(file)" opt

在命令行上,请在长选项之前加上前缀 --++。在上面的示例中,命令行上的 --file 等同于 -f,而命令行上的 ++file 等同于 +f

每个短选项可以有多个长选项等效体,但是这有违 CLIP 规范,应谨慎使用。必须将每个长选项等效体都放置到括号中,如下所示:

getopts "f:(file)(input-file)o:(output-file)"

在上面的示例中,--file--input-file 均等同于 -f,而 --output-file 等同于 -o

变量 name 始终设置为短选项。当在命令行上指定长选项时,name 将被设置为其短选项等效体。

有关 Korn shell 的 getopts 内置命令的进一步讨论,请参见本手册页中之前 Bourne shell (sh) 部分中的讨论。

ksh

getopts 实用程序可用于从 arg 指定的参数列表或从位置参数(如果省略了 arg)检索选项和参数。它还可以基于 optstring 中的信息为命令生成用法消息和手册页。

每次被调用时,getopts 实用程序都会将下一选项的值放入 name 操作数指定的 shell 变量中,将要处理的下一参数的索引放入 shell 变量 OPTIND 中。当调用 shell 时,OPTIND 将初始化为 1。当选项需要或允许选项参数时,getopts 会将选项参数置于 shell 变量 OPTARG 中。其他情况下,OPTARG 将设置为 1(当设置了该选项时)或 0(当取消设置该选项时)。

optstring 字符串包含字母数字字符、特殊字符 +-?: 以及空格或括在 [...] 中的字符组。字符组可以嵌套在 {...} 中。在 [...] 组以外,后跟零个或多个空格的单个 NEWLINE 将被忽略。一个或多个空白行将选项与命令参数概要隔开。

每个 [...] 组包含一个可选标签、由 : 分隔的可选属性以及跟在 ? 后面的可选描述字符串。对于选项解析和简短用法消息,从 ? 到下一个 ] 结尾之间的字符将被忽略。它们用于生成详细帮助或手册页。: 字符不能出现在标签中。? 字符在标签中必须指定为 ??] 字符在描述字符串中必须指定为 ]]。文本位于两个 \b (backspace) 字符之间表示该文本在显示时应使用粗体。文本位于两个 \a (bell) 字符之间表示该文本在显示时应进行强调或使用斜体。文本位于两个 \v (vertical tab) 字符之间表示该文本应以定宽字体显示。位于两个 \f (form feed) 字符之间的文本将由其名称是被围文本的 shell 函数的输出替换。

此接口的所有输出都将写入到标准错误。

有多种组类型:

如果 optstring 的前导字符是 +,则以 + 开头的参数也将被视为选项。

optstring 中的前导 : 字符或跟随在前导 + 后的 : 字符会影响错误的处理方式。如果在处理选项时遇到 optstring 中未指定的选项字符或 longname 参数,则其名称是 name 的 shell 变量将设置为 ? 字符。shell 变量 OPTARG 将设置为找到的字符。如果某个选项参数缺失或具有无效值,则 name 将设置为 : 字符,shell 变量 OPTARG 将设置为找到的选项字符。若无前导 :,在遇到错误时,name 将设置为 ? 字符,OPTARG 将取消设置,且会将一条错误消息写入到标准错误。

以下任一情况都表示选项的结尾:

  1. 遇到特殊参数 --

  2. 遇到未以 - 开头的参数。

  3. 指定了帮助参数。

  4. 遇到了错误。

如果 OPTIND 设置为值 1,则可以使用新的参数集合。

getopts 还可用于生成帮助消息,其中包含命令用法和详细的描述。将 args 指定为:

-?

这用来生成用法概要。

--??

这用来生成详细的用法消息。

--??man

这用来生成格式化的手册页。

--??api

这用来生成易于解析的用法消息。

--??html

这用来生成 html 格式的手册页。

--??nroff

这用来生成 nroff 格式的手册页。

--??使用

这用来列出当前的 optstring

--???name

这用来列出 version=n,其中 n 大于 0(如果 getopts 可以识别选项 name)。

当遇到选项结尾时,getopts 将以非零返回值退出,变量 OPTIND 将设置为第一个非选项参数的索引。

选项

ksh

ksh 支持以下选项:

-a name

使用 name 而不是用法消息中的命令名称。

操作数

支持下列操作数:

optstring

一个包含调用 getopts 的实用程序可识别的选项字符的字符串。如果某个字符后跟有一个冒号,则该选项需要有一个参数,该参数应作为单独的参数提供。应用程序应将选项字符及其选项参数指定为单独的参数,但 getopts 会将跟随在需要参数的选项字符后的字符解释为参数,无论实际情况是否如此。在调用 getopts 时,如果显式的空选项参数不是作为单独的参数提供的,则无需识别该参数;请参见getopt(3C)。应用程序不得使用问号字符 (?) 和冒号字符 (:) 作为选项字符。使用非字母数字的其他选项字符会产生意外结果。如果选项参数不是作为与选项字符隔开的参数提供的,则 OPTARG 中的值会被除去选项字符和 -optstring 中的第一个字符决定了当某个选项字符未知或缺少某个选项参数时 getopts 的行为。

name

shell 变量的名称,由 getopts 实用程序设置为发现的选项字符。

缺省情况下,getopts 实用程序解析传递到调用方 shell 过程的位置参数。如果指定了 arg,则将解析这些参数而不解析位置参数。

用法

因为 getopts 会影响当前的 shell 执行环境,因此,它通常是作为 shell 常规内置项提供的。如果它是在一个子 shell 或单独的实用程序执行环境中调用的,例如下面所示的环境之一:

      (getopts abc value "$@")
       nohup getopts ...
       find . -exec getopts ... \;

则它不会影响调用者的环境中的 shell 变量。

请注意,尽管位置参数已更改,但 shell 函数与调用方 shell 共享 OPTIND。想使用 getopts 来解析参数的函数通常希望将 OPTIND 的值保存在条目上并在返回之前对其进行还原。然而,有时候函数希望为调用方 shell 更改 OPTIND

示例

示例 1 解析并显示参数

以下示例脚本将解析并显示其参数:

aflag=
bflag=
while getopts ab: name
do
     case $name in
     a)      aflag=1;;
     b)      bflag=1
             bval="$OPTARG";;
     ?)     printf "Usage: %s: [-a] [-b value] args\n"  $0
            exit 2;;
     esac
done
if [ ! -z "$aflag" ]; then
   printf "Option -a specified\n"
fi
if [ ! -z "$bflag" ]; then
     printf 'Option -b "%s" specified\n' "$bval"
fi
shift $(($OPTIND - 1))
printf "Remaining arguments are: %s\n" "$*"

示例 2 为带选项的命令处理参数

以下 shell 程序片段为可以接受选项 -a-b 的命令处理参数。它还将处理选项 -o,该选项需要一个选项参数:

while getopts abo: c
do
      case $c in
     a | b)   FLAG=$c;;
     o)       OARG=$OPTARG;;
     \?)      echo $USAGE
        exit 2;;
     esac
done
shift `expr $OPTIND - 1`

示例 3 等效的代码表达式

此代码示例将下列各个输入视为等效的:

cmd -a -b -o "xxx z yy" filename
cmd -a -b -o "xxx z yy" -- filename
cmd -ab -o xxx,z,yy filename
cmd -ab -o "xxx z yy" filename
cmd -o xxx,z,yy -b -a filename

环境变量

有关影响 getopts 执行的以下环境变量的描述,请参见 environ(5)LANGLC_ALLLC_CTYPELC_MESSAGESNLSPATH

OPTIND

getopts 将该变量用作要处理的下一个参数的索引。

OPTARG

如果某个选项使用了参数,getopts 将使用该变量来存储参数。

退出状态

将返回以下退出值:

0

找到了一个选项,可能是也可能不是由 optstring 指定的。

>0

遇到选项的结尾,或发生错误。

ksh

ksh 返回以下退出值:

0

找到指定的选项。

1

遇到选项结尾。

2

已生成用法或信息性消息。

属性

有关下列属性的说明,请参见 attributes(5)

/usr/bin/getopts、sh、ksh88

属性类型
属性值
可用性
system/core-os
接口稳定性
Committed(已确定)
标准
请参见 standards(5)

ksh

属性类型
属性值
可用性
system/core-os
接口稳定性
Uncommitted(未确定)

另请参见

Intro(1)getoptcvt(1)ksh(1)ksh88(1)ps(1)sh(1)getopt(3C)attributes(5)environ(5)standards(5)

诊断

无论何时检测到错误,如果 optstring 操作数中的第一个字符不是冒号 (:),则都会向标准错误写入一条诊断消息,其中以未指定格式包含以下信息: