0

I wrote this function in BASH, and it returns an unexpected value:

checkIfUserFound()
{
   isUserFound=$( cat user_uid.txt | grep $ADMIN_TO_UPDATE -B 1 | grep "uid" )
   return $isUserFound
}

the user_uid.txt file is empty (I enter invalid admin).

but for some reason, it returns "1":

checkIfUserFound
isUserFound=$?

if [ "$isUserFound" -eq "0" ];
then
    echo "Searching..."
else
    echo "User found..."
fi

This prints "User found..."

Does anyone know how come that is the returning value ? Shouldn't I get "0" when returning from the function ?

hidefromkgb
  • 5,834
  • 1
  • 13
  • 44
MathEnthusiast
  • 101
  • 1
  • 10

2 Answers2

1

The argument to return needs to be a number (a small integer -- the range is 0 through 255). But grep already sets its return code to indicate whether it found a match, and functions already return the return code of the last command in the function; so all you really need is

checkIfUserFound()
{
    grep "$ADMIN_TO_UPDATE" -B 1 user_uid.txt |
    grep -q "uid"
}

(Notice also how we get rid of the useless use of cat).

The script could probably usefully be refactored to Awk. Perhaps I am guessing correctly what you want:

checkIfUserFound()
{
    awk -v user="$ADMIN_TO_UPDATE" '/uid/ { p=1; next }
    p { if ($0 ~ user) found=1; p=0 }
    END { exit 1-found }' user_uid.txt
}

Finally, the code which calls this function should check whether it succeeded, not whether it printed 0.

if ! checkIfUserFound
then
    echo "Searching..."
else
    echo "User found..."
fi

Notice perhaps that [ is not part of the if command's syntax (though [ is one of the commands whose result code if is often used to check).

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

The main problem is that you don't check for failure of any of the commands in the pipe.

The failure (exit code) of the last command in the pipe will be the exit value of the whole pipe. That exit code is available in the $? variable.

And since you want to return the success/failure of the pipe from the function, that's the value you need to return:

return $?

If the pipe doesn't fail then the result of the pipe will be the output on stdout, which will be put into the $isUserFound variable. Since it will not be a valid exit code (which must be numeric) then it can't be returned.


Since you don't need the $isUserFound variable, and you want to return the result of the pipe, your function could be simplified as

checkIfUserFound() {
   cat user_uid.txt | grep $ADMIN_TO_UPDATE -B 1 | grep "uid" 2>&1 >/dev/null
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • As per my answer just now, `return $?` is really superfluous; a function already returns the result code from the latest command. – tripleee Jan 29 '19 at 11:20
  • How can I return what grep finds? I want to know if it returned empty string or not – MathEnthusiast Jan 29 '19 at 11:21
  • 1
    `grep` sets its return code to 0 if it finds a match, 1 otherwise (or more generally nonzero in case of a failure) – tripleee Jan 29 '19 at 11:21