初始实验脚本如下:

#!/bin/bash

tmp_license="GPL-2"
concluded_license="LGPL-2.1"

echo "1111111111111111111111111111111111111"
echo $concluded_license | grep "$tmp_license"

echo "2222222222222222222222222222222222222"
`echo $concluded_license | grep "$tmp_license"`

运行以上脚本会出现如下结果:

./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
LGPL-2.1
2222222222222222222222222222222222222
./test_subscript/full_match_test.sh: 行 11: LGPL-2.1:未找到命令

此时修改脚本,加入ret=,修改后的脚本如下:

#!/bin/bash

tmp_license="GPL-2"
concluded_license="LGPL-2.1"

echo "1111111111111111111111111111111111111"
echo $concluded_license | grep "$tmp_license"

echo "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep "$tmp_license"`
echo "$ret"

运行修改后的脚本结果如下:

./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
LGPL-2.1
2222222222222222222222222222222222222
LGPL-2.1

可见,在shell中不能直接调用``,必须有变量来接收才可以。

另外可以看到,"LGPL-2.1"中是包含"GPL-2"的,如果想要收集所有的协议,则会产生问题,正常应该是"LGPL-2.1"和"GPL-2",但是按照以上的方法,"GPL-2"会“湮灭”在"LGPL-2,1"中,从而导致只有"LGPL-2.1"一个协议被收集到。

如何解决?继续修改脚本,加入Fx选项实现完全匹配,已解决这一问题。脚本如下:

#!/bin/bash

tmp_license="GPL-2"
concluded_license="LGPL-2.1"

echo "1111111111111111111111111111111111111"
echo $concluded_license | grep -Fx "$tmp_license"

echo "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep -Fx "$tmp_license"`
echo "$ret"

脚本运行结果如下:

$ ./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
2222222222222222222222222222222222222

这里有一个“小手段”要注意:grep本来是用于在文件中查找字符串。那么如何将其用在字符串中查找字符串?使用echo <string1> | grep <string2>的方式即可实现。

可以看到,加入-Fx选项后,会进行全字匹配,而不会进行误判断了。

完整的实验脚本如下:

#!/bin/bash

tmp_license="GPL-2"
concluded_license="LGPL-2.1"

echo "1111111111111111111111111111111111111"
if [ `echo $concluded_license | grep "$tmp_license"` ]; then
	echo "exist"
else
	echo "not exist"
fi

echo "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep -Fx "$tmp_license"`
echo "$ret"

在此基础上在进行进一步实验,这次将concluded_license进行修改,脚本如下:

#!/bin/bash

tmp_license="GPL-2"
concluded_license="LGPL-2.1 or GPL-2"

echo "1111111111111111111111111111111111111"
if [ `echo $concluded_license | grep "$tmp_license"` ]; then
	echo "exist"
else
	echo "not exist"
fi

echo "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep -Fx "$tmp_license"`
echo "$ret"

此时运行脚本,会出现以下错误:

$ ./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
./test_subscript/full_match_test.sh: 第 27 行: [: or:需要二元表达式
not exist
2222222222222222222222222222222222222

解决方法是先用一个变量接收一下,也就是说不直接将echo带入到if中,再进行判断。修改后的脚本如下所示:

#!/bin/bash

tmp_license="GPL-2"
concluded_license="LGPL-2.1 or GPL-2"

echo "1111111111111111111111111111111111111"
ret=`echo "$concluded_license" | grep "$tmp_license"`
#echo "ret: $ret"
if [ -n "$ret" ]; then
#if [ `echo $concluded_license | grep "$tmp_license"` ]; then
	echo "exist"
else
	echo "not exist"
fi

echo "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep -Fx "$tmp_license"`
echo "$ret"

运行脚本,结果如下所示:

$ ./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
not exist
2222222222222222222222222222222222222

虽然脚本错误问题解决了,但是逻辑上出现了新问题。这次"LGPL-2.1 or GPL-2中"明显已经包含了"GPL-2",应该能够搜索到,但是反倒找不到了,为什么呢?原因是-Fx实现了完全匹配,条件太强了,必须完全相同,差一点都不行。我们只需要精确匹配就可以了,将-Fx参数改为-w,脚本如下:

#!/bin/bash

tmp_license="GPL-2"
concluded_license="LGPL-2.1 or GPL-2"

echo "1111111111111111111111111111111111111"
ret=`echo "$concluded_license" | grep -w "$tmp_license"`
#echo "ret: $ret"
if [ -n "$ret" ]; then
#if [ `echo $concluded_license | grep "$tmp_license"` ]; then
	echo "exist"
else
	echo "not exist"
fi

echo "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep -Fx "$tmp_license"`
echo "$ret"

运行结果如下:

$ ./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
exist
2222222222222222222222222222222222222

此时还不算完,还要确保-w参数能够在concluded_license只为"LGPL-2.1"时,不会“湮灭"GPL",修改脚本,恢复concluded_license为老的值,修改后脚本如下所示:

#!/bin/bash

tmp_license="GPL-2"
concluded_license="LGPL-2.1"

echo "1111111111111111111111111111111111111"
ret=`echo "$concluded_license" | grep -w "$tmp_license"`
#echo "ret: $ret"
if [ -n "$ret" ]; then
#if [ `echo $concluded_license | grep "$tmp_license"` ]; then
	echo "exist"
else
	echo "not exist"
fi

echo "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep -Fx "$tmp_license"`
echo "$ret"

脚本运行结果如下:

$ ./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
not exist
2222222222222222222222222222222222222

可以看到,使用-w参数进行grep,不会发生“湮灭”的情况,同时也不会过度严格,将真正包含的文本认为不存在。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐