Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
}
share|improve this question
1  
First thing you might want to do here is ask yourself what a statement immediately after a break might do.... –  Ray Toal Feb 23 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. –  MAXGEN Feb 23 at 23:43
    
Right so the return 0 won't be executed in that case. –  Ray Toal Feb 23 at 23:44
    
Would it make more sense to have the return 0 after the while? so after the last line done < $VARFILENAME –  MAXGEN Feb 23 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. –  Ray Toal Feb 23 at 23:48

1 Answer 1

up vote 3 down vote accepted

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
}
share|improve this answer
    
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. –  chepner Feb 24 at 13:47
    
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 Feb 24 at 15:10
    
@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. –  ruakh Feb 24 at 16:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.