0

I need to return a true or false if I find a value within a file. I parse the file and if the value is located once which is enough, I want to break and return a true else return false. On top of it I need to pass the file into this check function. I'm trying to only use bash.

is_file_contains_VAR(){
    VARFILENAME=$1

    while read LINE
    do
        if echo "$LINE" | grep -q "$VAR"; then
            break
            return 0   
        else
            return 1
        fi
    done < $VARFILENAME
}
5
  • 1
    First thing you might want to do here is ask yourself what a statement immediately after a break might do.... Commented Feb 23, 2014 at 23:41
  • A break should get me out of the while at least I'm hoping. I don't want it to continue through the whole file. Commented Feb 23, 2014 at 23:43
  • Right so the return 0 won't be executed in that case. Commented Feb 23, 2014 at 23:44
  • Would it make more sense to have the return 0 after the while? so after the last line done < $VARFILENAME Commented Feb 23, 2014 at 23:45
  • Yes but that is still only the first problem. Your code is always returning a value the first time through the loop. You need to wait until you have gone through the whole file without seeing VAR in order to return 1. Commented Feb 23, 2014 at 23:48

1 Answer 1

4

grep -q already does what you want: it will abort as soon as it finds the string in question. So you can just write:

function is_file_contains_VAR () {
    grep -q -e "$VAR" "$1"
}

(Note: the -e is in case "$VAR" starts with a hyphen.)

But for educational purposes . . . to write this as a loop, what you would want is to return 0 as soon as there's a match, and only return 1 at the very end, if you never find a match:

function is_file_contains_VAR () {
    local VARFILENAME="$1"
    local LINE
    while IFS= read -r LINE ; do
        if grep -q -e "$VAR" <<< "$LINE" ; then
             return 0
        fi
    done < "$VARFILENAME"
    return 1
}
Sign up to request clarification or add additional context in comments.

3 Comments

Also, if using the bash loop shown, use bash's built-in regular expression support instead of forking a grep process: if [[ "$LINE" =~ $VAR ]]; then. You would need to make sure that VAR conforms to the regular expressions understood by bash.
what do you mean by "forking" a grep? Does this open up a new thread is what you mean? Is grep natively supported in bash, meaning I can use it on any machine with bash?
@MAXGEN: Not "forking a grep", but "forking a grep process"; that is, using fork() to create a new process, which will then be used to run grep. (If you're not familiar with forking and processes, then please Google them; I can't/won't explain them in a short comment.) Re: "Is grep natively supported in bash, meaning I can use it on any machine with bash?": No. They are separate programs. If you have bash, you'll usually have grep, but there are plenty of exceptions.

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.