使用grep从另一个linux脚本检测linux bash脚本中的注释行

时间:2022-05-30 04:25:39

I have a script in linux (script A) that usually contains the following command:

我在linux中有一个脚本(脚本a),它通常包含以下命令:

/usr/bin/do-some-test

I am creating a bash script now (script B), to always check if (script A) includes the above command.

我现在正在创建一个bash脚本(脚本B),以始终检查(脚本a)是否包含上述命令。

so i used this:

所以我用这个:

if grep /usr/bin/do-some-test "script A"; then
    echo "All good"
else
    echo "Script A file is not updated!"

the problem is, that if i comment the command in (scriptA), like this:

问题是,如果我在(scriptA)中注释命令,就像这样:

#/usr/bin/do-some-test

the result of the grep search is still TRUE.

grep搜索的结果仍然是正确的。

how can i search for #/usr/bin/do-some-test but make sure it doesn't have the # in the beginning?

我怎样才能找到#/usr/bin/do- sometest,但是要确保开头没有# ?

2 个解决方案

#1


3  

You can use anchors in your regex:

你可以在你的regex中使用锚:

if grep -Eq '^ */usr/bin/do-some-test' "script A"; then
   echo "All good"
else
   echo "Script A file is not updated!"
fi

#2


0  

In order to catch preceding tabs one should write the funny-looking

为了捕获前面的制表符,应该编写看起来很有趣的制表符

grep -E '^['$'\t'' ]*command' 

which is constructed from two single-quoted strings with a $'\t' in the middle (and contains a space before the closing bracket), or better use a Posix character class whose intent is clear:

它是由两个单引号的字符串构成的,中间有一个$'\t'(在结束括号前包含一个空格),或者最好使用一个Posix字符类,其目的是明确的:

grep -E '^[[:space:]]*command'

#1


3  

You can use anchors in your regex:

你可以在你的regex中使用锚:

if grep -Eq '^ */usr/bin/do-some-test' "script A"; then
   echo "All good"
else
   echo "Script A file is not updated!"
fi

#2


0  

In order to catch preceding tabs one should write the funny-looking

为了捕获前面的制表符,应该编写看起来很有趣的制表符

grep -E '^['$'\t'' ]*command' 

which is constructed from two single-quoted strings with a $'\t' in the middle (and contains a space before the closing bracket), or better use a Posix character class whose intent is clear:

它是由两个单引号的字符串构成的,中间有一个$'\t'(在结束括号前包含一个空格),或者最好使用一个Posix字符类,其目的是明确的:

grep -E '^[[:space:]]*command'