Search code examples
bashsedgreprhel

Is it possible to get the grep path output in a variable?


I'm having trouble in a script and i want to know if it's possible to store the path of the matching result of a grep ?

I'm on RHEL 7, the script is a check of the rsyslog.conf file which complete or add the correct value to a parameter (CIS rhel7 benchmark, part 4.2.1.3).

Full script so far :

#!/bin/bash

if grep "^\$FileCreateMode" /etc/rsyslog.conf /etc/rsyslog.d/*.conf
then
    read -p "Is $FileCreateMode superior or equal to 0640 ? [y/n]" rep
    if [ $rep == "y" ]
    then
        echo "No action needed"
    else
        read -p "Enter the new $FileCreateMode value (0640 recommanded)" rep2
        sed -i "/^\$FileCreateMode/ $rep2" 
        echo "$FileCreateMode new value is now $rep2"
    fi
else
    echo "$FileCreateMode doesn't exist in rsyslog conf files"
    read -p "What's the path of the file to modify ?(Press [ENTER] for default /etc/rsyslog.conf)" path
    if [ $path -z ] 
    then
        echo "$FileCreateMode 0640" >> /etc/rsyslog.conf
    else
        echo "$FileCreateMode 0640" >> $path
    fi
fi

So my problem is on the sed at the 11th line. Am i able to get the right path if my grep on 3rd line matched into a variable to reuse it on the 11th.

And i'm struggling with the same sed because i want him to replace the value after $FileCreateMode but it keep changing the $FileCreateMode string.

i've tried this syntax too but i still don't get the result i want

sed -i -e "s,^\($FileCreateMode[ ]*\).*,\1 0640 ,g" /etc/rsyslog.conf

Thanks in advance for any help you can bring, and have a good day :)

Edit :

As requested i'm simplifying here.

I want to grep $FileCreateMode in /etc/rsyslog.conf and /etc/rsyslog.d/*.conf and i'm trying to get the destination file (could be rsyslog.conf but it can be testpotato.conf in rsyslog.d) into a variable (like $var) to be able to use the path in my sed on the 11th line like

sed -i "/^\$FileCreateMode/ 0640" $var

And for the sed problem when i execute this command i would like to have something like

old : $FileCreateMode 0777
sed -i "/^\$FileCreateMode/ 0640" $var
new : $FileCreateMode 0640

But instead i get

old : $FileCreateMode 0777
sed -i "/^\$FileCreateMode/ 0640" $var
new : 0640 ($FileCreateMode is deleted)

hope i'm more understable, thanks again and feel free to ask for more details


Solution

  • Use $() to assign the results of grep into a variable, and then use a for loop to process files one by one:

    # Assign grep results to FILES
    FILES=$(grep -l '^$FileCreateMode' /etc/rsyslog.conf /etc/rsyslog.d/*.conf)
    
    # Check if FILES variable is not empty
    if [[ -n ${FILES} ]]; then
        # Loop through all the files
        for file in ${FILES}; do
            # ...
            sed -iE "s/^(\\\$FileCreateMode\s+)[[:digit:]]+/\1${rep2}/" ${file}
            # ...
        done
    else
        # OP's logic for when $FileCreateMode doesn't exist in any of the files
    

    sed fix:

    Notice that I've also updated your sed expression (above). You were very close, but you had to double escape the dollar sign: once for using it inside "", and once so that it isn't interpreted as END_OF_LINE in the regex.