0

I'm trying to create a function that will do a grep on strings from an array that are sent as arguments but I can't get the quotation marks to work as I want.

LOG=/tmp/log.log
function grepCheck () {
    if grep $1 ; then
    STATE="true"
    else
    STATE="false"
    fi }

possibleErrors=("string1 string1 string1" "string2 string2 string2")

for checkError in ${possibleErrors[*]}
do
grepCheck "${possibleErrors[$checkError]} $LOG"
done

Output example:

+ LOG=/tmp/log.log
+ possibleErrors=("string1 string1 string1" "string2 string2 string2")
+ for checkError in '${possibleErrors[*]}'
+ grepCheck 'string1 string1 string1 /tmp/log.log'
+ grep string1 string1 string1 /tmp/log.log grep: string1: No such file or directory grep: string1: No such file or directory

How do I get the function to grep the string that it recieves?

2

3 Answers 3

2

What makes sense is for your function to accept the same arguments grep does.

LOG=/tmp/log.log
grepCheck () {
    # Edit: use "$@"; add -q option to avoid spurious output
    if grep -q "$@"; then
        STATE="true"
    else
        STATE="false"
    fi
}

possibleErrors=("string1 string1 string1" "string2 string2 string2")

for checkError in ${possibleErrors[*]}
do
    # Edit: fixed quotes; pass two quoted arguments
    grepCheck "${possibleErrors[$checkError]}" "$LOG"
done

If you simply care whether any of these strings matched, it would be simpler to pass them all to grep in one go.

regex=$(IFS='|'; echo "${possibleErrors[*]}")
grep -Eq "$regex" "$LOG"
Sign up to request clarification or add additional context in comments.

2 Comments

I think you want to use egrep in the second example with '|' ?
@Bitdiot Meh, I was trying to wiggle around that, but you're right, it's the easiest solution.
0

Your code looks a little funny. grep usually wants a regular expression and a file to look for the expression. However, in your example, grepCheck is given an error string to-find, and a log to look for it.

Here you have:

if grep $1 ; then

Because $1 is unquoted, the shell expands this to grep string1 string1 string1. So grep wants to look for string1 in the two files called string1 and string1 (basically the same two files.).

I think you want the following:

function grepCheck () {
    needle=$1
    haystack=$2
    if [[ -n $(grep "$needle" "$haystack") ]] ; then
        STATE="true"     # Error found
    else
        STATE="false"
    fi }

Then in your calling code:

grepCheck "${possibleErrors[$checkError]}" $LOG

Notice the change in quoting.

6 Comments

That's a Useless Use of [[. grep tells you perfectly well whether or not it succeeded just by itself, and the OP's code has this part exactly right.
Ah yes, thank you for correcting my bad habit. Let me change that. If you don't mind.
Hey triplee. I changed it back to the original. The reason is, and I just remembered, he may not want to see the results of the grep, but to set the flags.
Use grep -q for that. See my answer.
Wow, that's nice. I didn't know of the -q option. Thanks.
|
0

I believe you need something like:

... for checkError in seq 0 $((${#possibleErrors[*]} - 1)) ...

But this is also a useless use of an array:

for error in "string1 string1 string1" "string2 string2 string2"; do grepCheck "$error" "$LOG" done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.