0

I'm doing an script that check configuration files. In order to check multiple lines at once I'm using pcregrep. When i use it in command line everything works great.

When I put it inside a function it does not fidn the pattern.

This is my function

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
}

The echo pcregrep -M "$string" $path it's just a control to verify that it takes the pcregrep command take the good variables

when I execute the file with the function I have the following in the console

/etc/yum.repos.d/nginx.repo --> 'NGINX repository' repository is NOT configured correctly
  • Funny thing : When I copy-paste the result of echo pcregrep -M "$string" $path shown in the console, that is :

    pcregrep -M ".*[nginx]*\n.*name=nginx.*.repo*\n.*baseurl=http://nginx.org/packages/centos/.*.releasever/.*.basearch/*\n.*gpgcheck=0*\n.*priority=1*
    

It works like a charm

UPDATE : Actually I'm trying to parse the regex and paths from a CSV file, the lines below are, the columns names and an exemple of the content of the file :

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,, ,

This the function that reads the CSV file and in fonction of the first column it calls one function or another :

# 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

I hope that it can help to troobleshoot the problem.

What I'm missing ???????

Thank's in advance

Abel
  • 151
  • 4
  • Do you really want to write `pcregrep` in backticks? The output will be executed as a command whose output is then sent to `if`. Also you are missing `"`-quotes around `$string` and `$path`. – Socowi Oct 24 '17 at 13:39
  • Thank's to both, already tried all combinations : with and without backquoutes and with and without backticks, and all the combinations possibles with this two situations, NOTHING CHANGE – Abel Oct 24 '17 at 13:47

2 Answers2

0

You are trying to execute the output of pcregrep in your script; drop the backquotes.

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
}
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thank's to both, already tried all combinations : with and without backquoutes and with and without backticks, and all the combinations possibles with this two situations, NOTHING CHANGE – Abel Oct 24 '17 at 13:46
  • @Abel This is correct. The problem is that you're calling the function wrong: according to your post, your regex has literal double quotes embedded in it. It shouldn't have. Consider updating the question with the data you're trying to match against and the actual invocation of the function. – that other guy Oct 24 '17 at 15:41
0

ERROR FOUND

Actually when reading the CSV file, the process adds ' ' around every parameter. My csv was like it follows :

function,string,string_msg,path,package,space,

pcregrepF,"something to search",message to show,path to the file,, ,

When readin the second parameter, it add ' ' around it, so the final string would be '"something to search"' and obviosly nothing macthes because of the double quotes.

Am I readin in a wrong way the CSV file ????

Is there any way to evitate this addition of characters when readin from a csv with bash ???

Thank you !

Abel
  • 151
  • 4