了解函数中的参数

发布于 2024-12-03 22:46:45 字数 748 浏览 1 评论 0原文

我发现了这个函数:

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2024-12-10 22:46:45
  • local:局部变量。假设您的程序中有一个名为 foo 的变量。您调用的函数也有变量 foo。假设该函数更改了 foo 的值。

试试这个程序:

testme()
{
    foo="barfoo"
    echo "In function: $foo"
}

foo="bar"
echo "In program: $foo"
testme
echo "After function in program: $foo"

请注意,即使在函数完成之后,$foo 的值也已被函数更改。通过声明 local foo="barfoo" 而不仅仅是 foo="barfoo",我们可以防止这种情况发生。

  • case:case 语句是一种指定选项列表以及您想要对每个选项执行的操作的方法。它有点像 if/then/else 语句。

这两者或多或少是等效的:

if [[ "$foo" == "bar" ]]
then
   echo "He said 'bar'!"
elif [[ "$foo" == "foo" ]]
then
  echo "Don't repeat yourself!"
elif [[ "$foo" == "foobar" ]]
then
  echo "Shouldn't it be 'fubar'?"
else
   echo "You didn't put anything I understand"
fi

并且

case $foo in
    bar)
        echo "He said 'bar'!"
        ;;
    foo)
        echo "Don't repeat yourself!"
        ;;
    foobar)
        echo "Shouldn't it be 'fubar'?"
        ;;
    *)
        echo "You didn't put anything I understand"
        ;;
esac

;; 结束 case 选项。否则,它将下降到下一行并执行这些行。我将每个选项分为三行,但它们通常像

foobar) echo "Shouldn't it be 'fubar'?";;
  • shift 一样组合:命令行参数放在名为 $* 的变量中。当您说 shift 时,它会获取 $* 变量中的第一个值,并将其删除。

  • getopts:Getopts 是一个相当复杂的命令。它用于解析 $@ 变量中单字母选项的值(其中包含来自命令行的参数和参数)。通常,您在 while 循环中使用 getopts 并使用 case 语句来解析输出。格式为getopts变量var 是一次包含每个选项的变量。指定单字母参数以及哪些参数需要参数。解释它的最好方法是向您展示一个简单示例

  • $#:命令行上参数/参数的数量。

  • ${var:-alternative}:这表示使用环境变量 $var 的值。但是,如果此环境变量未设置或为 null,请改用值 alternative。在此程序中,使用 ${2:-*} 代替。 $2 表示由于 shift 命令而将所有内容移出后,命令行参数/参数中剩下的第二个参数。

  • 2>&-:这会将标准错误移至标准输出。标准错误是放置错误消息的地方。通常,它们像标准输出一样放置在您的终端屏幕上。但是,如果将输出重定向到文件中,错误消息仍会打印到终端窗口。在这种情况下,将输出重定向到文件也会重定向任何错误消息。

  • local: Local variable. Let's say you had a variable called foo in your program. You call a function that also has a variable foo. Let's say the function changes the value of foo.

Try this program:

testme()
{
    foo="barfoo"
    echo "In function: $foo"
}

foo="bar"
echo "In program: $foo"
testme
echo "After function in program: $foo"

Notice that the value of $foo has been changed by the function even after the function has completed. By declaring local foo="barfoo" instead of just foo="barfoo", we could have prevented this from happening.

  • case: A case statement is a way of specifying a list of options and what you want to do with each of those options. It is sort of like an if/then/else statement.

These two are more or less equivelent:

if [[ "$foo" == "bar" ]]
then
   echo "He said 'bar'!"
elif [[ "$foo" == "foo" ]]
then
  echo "Don't repeat yourself!"
elif [[ "$foo" == "foobar" ]]
then
  echo "Shouldn't it be 'fubar'?"
else
   echo "You didn't put anything I understand"
fi

and

case $foo in
    bar)
        echo "He said 'bar'!"
        ;;
    foo)
        echo "Don't repeat yourself!"
        ;;
    foobar)
        echo "Shouldn't it be 'fubar'?"
        ;;
    *)
        echo "You didn't put anything I understand"
        ;;
esac

The ;; ends the case 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 like

foobar) echo "Shouldn't it be 'fubar'?";;
  • shift: The command line arguments are put in the variable called $*. When you say shift, 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 employ getopts in a while loop and use case statement to parse the output. The format is getopts <options> var. The var 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 value alternative 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 the shift 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.

小霸王臭丫头 2024-12-10 22:46:45

这些是 bash 内置函数。您应该阅读 bash 手册页,或者对于 getopts,尝试 help getopts

一次一个(在 ipad 上打字真的很烦人,因此切换到笔记本电脑):

local 允许您定义局部变量(在范围内) )

getopts 是一个 bash 内置函数,它实现 getopt 风格的参数处理(-a-b... 类型参数

案例是switch 语句的 bash 形式。语法是

case: case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac

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 is

case: case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac

shift 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 (so shift 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文