lovevmwarer
写了一段脚本做个测试,但是没有成功,不知道什么地方出现问题
#!/bin/sh
var=1
first ()
{
echo "first run"
}
second ()
{
echo "second run"
}
third ()
{
echo "third run"
}
if [ ${var} -gt 0 ]
then
while getopts :setho option
do
case $option in
a) first;;
b) second;;
c) third;;
esac
done
fi
sh getopts.sh a
发现什么都没有显示,不知道什么地方出现问题。。。。。
lovevmwarer
[quote]原帖由 [i]waker[/i] 于 2008-6-30 15:35 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8703511&ptid=1184637][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
setho这几个选项你测试的时候用了哪一个? [/quote]
sh getopts.sh a
我用的是a,是不是这个有点问题,我对这个不是很明白,谢谢指点
walkerxk
[quote]原帖由 [i]lovevmwarer[/i] 于 2008-6-30 15:48 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8703662&ptid=1184637][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
sh getopts.sh a
我用的是a,是不是这个有点问题,我对这个不是很明白,谢谢指点 [/quote]
请问a是setho中的第几个?
getopts :setho option
这句话的意思是从参数中选出setho中的一个,然后赋值给option
getopts optstring name [args]
getopts is used by shell procedures to parse positional parameters. [color=Red]optstring contains the option
characters to be recognized[/color]; if a character is followed by a colon, the option is expected to have
an argument, which should be separated from it by white space. The colon and question mark char-
acters may not be used as option characters. Each time it is invoked, getopts places the next
option in the shell variable name, initializing name if it does not exist, and the index of the
next argument to be processed into the variable OPTIND. OPTIND is initialized to 1 each time the
shell or a shell script is invoked. When an option requires an argument, getopts places that
argument into the variable OPTARG. The shell does not reset OPTIND automatically; it must be man-
ually reset between multiple calls to getopts within the same shell invocation if a new set of
parameters is to be used.
When the end of options is encountered, getopts exits with a return value greater than zero.
OPTIND is set to the index of the first non-option argument, and name is set to ?.
getopts normally parses the positional parameters, but if more arguments are given in args,
getopts parses those instead.
getopts can report errors in two ways. If the first character of optstring is a colon, silent
error reporting is used. In normal operation diagnostic messages are printed when invalid options
or missing option arguments are encountered. If the variable OPTERR is set to 0, no error mes-
sages will be displayed, even if the first character of optstring is not a colon.
If an invalid option is seen, getopts places ? into name and, if not silent, prints an error mes-
sage and unsets OPTARG. If getopts is silent, the option character found is placed in OPTARG and
no diagnostic message is printed.
If a required argument is not found, and getopts is not silent, a question mark (?) is placed in
name, OPTARG is unset, and a diagnostic message is printed. If getopts is silent, then a colon
(:) is placed in name and OPTARG is set to the option character found.
getopts returns true if an option, specified or unspecified, is found. It returns false if the
end of options is encountered or an error occurs.