Home > Software engineering >  How to get a meaningful output from grep?
How to get a meaningful output from grep?

Time:02-19

I am writing a bash script that you can run on a server where mediawikis of games are deployed. I want to check if the wikis use SMTP. So a part of my script greps for the content in the config file that proves that they use SMTP. The problem is, that I do that on multiple servers and I want to loop through multiple markets on a server. Not all servers share the same market names. In my script I have arrays that contain ALL market names. I want my script to account for the case that the grep cant find the file in which it is to look up if SMTP is used. How can I go about that? I was thinking about a extra command to ask if the file exists before grepping. But that didn't work out as you can

for i in "${markets[@]}"; do
    myPATH="/www/${game}_$i/${game}_$i/LocalSettings.php"
    grepOut="grep -q 'wgSMTP = array(' "$myPATH""
    if grep -q "wgSMTP = array(" "$myPATH"; then
        echo -e "The Market ${BLUE}$i${NC} ${GREEN}uses${NC} SMTP."
    else
        if [[ "$grepOut" == *"No such file or directory"* ]]; then
            if [[ "$market" == "all" ]]; then
                echo -e "All markets:"
            else
                echo -e "The Market ${BLUE}$i doesn't${NC} exist on this server."
            fi
        else
            echo -e "The Market ${BLUE}$i${NC} ${RED}doesn't${NC} use SMTP."
        fi
    fi
done

CodePudding user response:

for i in "${markets[@]}"; do
    path="/www/${game}_$i/${game}_$i/LocalSettings.php"

    if ! [[ -f "$path" ]]; then
        if [[ "$market" == "all" ]]; then
            echo -e "All markets:"
        else
            echo -e "The Market ${BLUE}$i doesn't${NC} exist on this server."
        fi

        continue
    fi

    if grep -q 'wgSMTP = array(' "$path"; then
        echo -e "The Market ${BLUE}$i${NC} ${GREEN}uses${NC} SMTP."
    else
        echo -e "The Market ${BLUE}$i${NC} ${RED}doesn't${NC} use SMTP."
    fi

done

Not sure if $market should be $i (or vice versa).

  • Related