理解bash脚本中的grep模式

时间:2022-03-20 01:47:48

What does it mean, in particular symbol * after end?

它是什么意思,尤其是结束后的符号* ?

LOG=/root/$(basename $0).log
_notlogged(){
 if ! grep -q "# $*" $LOG; then
  echo "no"
 fi
 echo "yes"
}
_notlogged

Therefore, are grep regex rules special?

因此,grep regex规则是否特殊?

1 个解决方案

#1


2  

The double quoted string "# $*" is subject to bash's parameter expansion before it get's passed to grep.

在传递给grep之前,双引号字符串“# $*”会受到bash的参数扩展。

$* is a special variable in bash, it contains all positional parameters. Meaning the arguments to _notlogged.

$*是bash中的一个特殊变量,它包含所有位置参数。表示参数为_notlog。

If you call it like

如果你这样称呼它。

_lotlogged foo

the expression passed to grep would be

传递给grep的表达式将是

grep -q "# foo"

Check the bash manual for further information.

查看bash手册了解更多信息。

#1


2  

The double quoted string "# $*" is subject to bash's parameter expansion before it get's passed to grep.

在传递给grep之前,双引号字符串“# $*”会受到bash的参数扩展。

$* is a special variable in bash, it contains all positional parameters. Meaning the arguments to _notlogged.

$*是bash中的一个特殊变量,它包含所有位置参数。表示参数为_notlog。

If you call it like

如果你这样称呼它。

_lotlogged foo

the expression passed to grep would be

传递给grep的表达式将是

grep -q "# foo"

Check the bash manual for further information.

查看bash手册了解更多信息。