了解函数中的参数
我发现了这个函数:
findsit()
{
OPTIND=1
local case=""
local usage="findsit: find string in files.
Usage: fstr [-i] \"pattern\" [\"filename pattern\"] "
while getopts :it opt
do
case "$opt" in
i) case="-i " ;;
*) echo "$usage"; return;;
esac
done
shift $(( $OPTIND - 1 ))
if [ "$#" -lt 1 ]; then
echo "$usage"
return;
fi
find . -type f -name "${2:-*}" -print0 | \
xargs -0 egrep --color=always -sn ${case} "$1" 2>&- | more
}
我理解输出及其作用,但有一些术语我仍然不理解,并且很难找到参考,但相信它们在我的编程中学习会很有用。有人能快速解释一下吗?有些没有手册页。
local
getopts
case
shift
$#
${2:-*}
2>&-
谢谢。
I found this function:
findsit()
{
OPTIND=1
local case=""
local usage="findsit: find string in files.
Usage: fstr [-i] \"pattern\" [\"filename pattern\"] "
while getopts :it opt
do
case "$opt" in
i) case="-i " ;;
*) echo "$usage"; return;;
esac
done
shift $(( $OPTIND - 1 ))
if [ "$#" -lt 1 ]; then
echo "$usage"
return;
fi
find . -type f -name "${2:-*}" -print0 | \
xargs -0 egrep --color=always -sn ${case} "$1" 2>&- | more
}
I understand the output and what it does, but there are some terms I still don't understand and find it hard to find a reference, but believe they would be useful to learn in my programming. Can anyone quickly explain them? Some don't have man pages.
local
getopts
case
shift
$#
${2:-*}
2>&-
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
foo
的变量。您调用的函数也有变量foo
。假设该函数更改了foo
的值。试试这个程序:
请注意,即使在函数完成之后,
$foo
的值也已被函数更改。通过声明local foo="barfoo"
而不仅仅是foo="barfoo"
,我们可以防止这种情况发生。这两者或多或少是等效的:
并且
;;
结束case
选项。否则,它将下降到下一行并执行这些行。我将每个选项分为三行,但它们通常像shift 一样组合:命令行参数放在名为
$*
的变量中。当您说shift
时,它会获取$*
变量中的第一个值,并将其删除。getopts:Getopts 是一个相当复杂的命令。它用于解析
$@
变量中单字母选项的值(其中包含来自命令行的参数和参数)。通常,您在 while 循环中使用 getopts 并使用 case 语句来解析输出。格式为getopts变量
。var
是一次包含每个选项的变量。指定单字母参数以及哪些参数需要参数。解释它的最好方法是向您展示一个简单示例。$#:命令行上参数/参数的数量。
${var:-alternative}:这表示使用环境变量
$var
的值。但是,如果此环境变量未设置或为 null,请改用值alternative
。在此程序中,使用 ${2:-*} 代替。$2
表示由于shift
命令而将所有内容移出后,命令行参数/参数中剩下的第二个参数。2>&-:这会将标准错误移至标准输出。标准错误是放置错误消息的地方。通常,它们像标准输出一样放置在您的终端屏幕上。但是,如果将输出重定向到文件中,错误消息仍会打印到终端窗口。在这种情况下,将输出重定向到文件也会重定向任何错误消息。
foo
in your program. You call a function that also has a variablefoo
. Let's say the function changes the value offoo
.Try this program:
Notice that the value of
$foo
has been changed by the function even after the function has completed. By declaringlocal foo="barfoo"
instead of justfoo="barfoo"
, we could have prevented this from happening.These two are more or less equivelent:
and
The
;;
ends thecase
option. Otherwise, it'll drop down to the next one and execute those lines too. I have each option in three lines, but they're normally combined likeshift: The command line arguments are put in the variable called
$*
. When you sayshift
, it takes the first value in that$*
variable, and deletes it.getopts: Getopts is a rather complex command. It's used to parse the value of single letter options in the
$@
variable (which contains the parameters and arguments from the command line). Normally, you employgetopts
in a while loop and usecase
statement to parse the output. The format isgetopts <options> var
. Thevar
is the variable that will contain each option one at a time. The specify the single letter parameters and which ones require an argument. The best way to explain it is to show you a simple example.$#: The number of parameters/arguments on the command line.
${var:-alternative}: This says to use the value of the environment variable
$var
. However, if this environment variable is not set or is null, use the valuealternative
instead. In this program ${2:-*} is used instead. The$2
represents the second parameter of what's left in the command line parameters/arguments after everything has been shifted out due to theshift
command.2>&-: This moves Standard Error to Standard Output. Standard Error is where error messages are put. Normally, they're placed on your terminal screen just like Standard Output. However, if you redirect your output into a file, error messages are still printed to the terminal window. In this case, redirecting the output to a file will also redirect any error messages too.
这些是 bash 内置函数。您应该阅读 bash 手册页,或者对于 getopts,尝试 help getopts
一次一个(在 ipad 上打字真的很烦人,因此切换到笔记本电脑):
local
允许您定义局部变量(在范围内) )getopts 是一个 bash 内置函数,它实现 getopt 风格的参数处理(
-a
、-b
... 类型参数案例
是switch 语句的 bash 形式。语法是shift
将所有参数移位 1(以便第二个参数成为第一个,第三个成为第二个,...),类似于 perl 移位。如果您指定一个参数,它将移动多个索引(因此shift 2
将分配$3 -> $1, $4 -> $2, ...
)< code>$# 是传递给函数的参数数量
${2:-*}
是默认参数形式。基本上,它查看第二个参数($2
是第二个参数),如果未分配它,它将用*
替换它。2>&-
是输出重定向(在本例中,用于标准错误)Those are bash built-ins. You should read the bash man page or, for getopts, try help getopts
One at a time (it's really annoying to type on ipad hence switched to laptop):
local
lets you define local variables (within the scope of a function)getopts
is a bash builtin which implements getopt-style argument processing (the-a
,-b
... type arguments)case
is the bash form for a switch statement. The syntax isshift
shifts all of the arguments by 1 (so that the second argument becomes the first, third becomes second, ...) similar to perl shift. If you specify an argument, it will shift by that many indices (soshift 2
will assign$3 -> $1, $4 -> $2, ...
)$#
is the number of arguments passed to the function${2:-*}
is a default argument form. Basically, it looks at the second argument ($2
is the second arg) and if it is not assigned, it will replace it with*
.2>&-
is output redirection (in this case, for standard error)