我正在做一个检查配置文件的脚本。为了一次检查多行,我使用了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 correctlyecho pcregrep -M "$string" $path结果时,即:
pcregrep -M -M--它的工作原理就像一个魅力
UPDATE:实际上,我试图解析CSV文件中的regex和路径,下面的行是,列名和文件内容的示例:
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我希望它能帮助解决这个问题。
我错过了什么?
预先感谢
发布于 2017-10-24 13:38:37
您正在尝试在脚本中执行pcregrep的输出;删除反引号。
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
}发布于 2017-10-25 08:26:30
错误发现
实际上,当读取CSV文件时,进程会在每个参数周围添加“”。我的csv是这样的:
function,string,string_msg,path,package,space,
pcregrepF,"something to search",message to show,path to the file,, ,当读取第二个参数时,它会在它周围添加‘’,所以最后的字符串将是'"something to search"',显然没有任何东西会因为双引号而变差。
我是不是以错误的方式读取CSV文件?
是否有任何方法可以避免从带有bash??的csv读入字符时添加字符?
谢谢!
https://stackoverflow.com/questions/46911832
复制相似问题