【发布时间】:2017-10-24 13:28:46
【问题描述】:
我正在编写一个检查配置文件的脚本。为了一次检查多行,我使用了 pcregrep。当我在命令行中使用它时,一切都很好。
当我把它放在一个函数中时,它并没有找到模式。
这是我的功能
function pcregrepF() {
echo pcregrep -M "$string" $path
if `pcregrep -M $string $path`; then
echo "$path --> $string_msg is configured OK"
else
echo "$path --> $string_msg is NOT configured correctly"
fi
}
echo pcregrep -M "$string" $path 只是一个控件,用于验证它是否需要 pcregrep 命令获取好的变量
当我使用函数执行文件时,控制台中有以下内容
/etc/yum.repos.d/nginx.repo --> 'NGINX repository' repository is NOT configured correctly
-
有趣的事情:当我复制粘贴控制台中显示的
echo pcregrep -M "$string" $path的结果时,即:pcregrep -M ".*[nginx]*\n.*name=nginx.*.repo*\n.*baseurl=http://nginx.org/packages/centos/.*.releasever/.*.basearch/*\n.*gpgcheck=0*\n.*priority=1*
它就像一个魅力
更新:实际上我正在尝试解析 CSV 文件中的正则表达式和路径,下面的行是列名和文件内容的示例:
function,string,string_msg,path,package,space,
pcregrepF,".*[nginx]*\\n.*name=nginx.*.repo*\\n.*baseurl=http://nginx.org/packages/centos/.*.releasever/.*.basearch/*\\n.*gpgcheck=0*\\n.*priority=1*\\n.*enabled=1",NGINX repository,/etc/yum.repos.d/nginx.repo,, ,
这是读取 CSV 文件并在第一列中调用一个或另一个函数的函数:
# Execute CSV - Read CSV file line by line and execute commands in function of parameters that are read
function executeCSV() {
file=/home/scripts/web.csv
while IFS="," read function string string_msg path package space
do
$function $string $string_msg $path $package
done < $file
}
executeCSV
我希望它可以帮助解决问题。
我错过了什么??????
提前致谢
【问题讨论】:
-
你真的想用反引号写
pcregrep吗?输出将作为命令执行,然后将其输出发送到if。此外,您在$string和$path周围缺少"-引号。 -
感谢双方,已经尝试了所有组合:有和没有反引号,有和没有反引号,以及这两种情况下所有可能的组合,没有任何变化