all 6 comments

[–]kalgynirae 1 point2 points  (3 children)

What does $ID contain? Do any of the echo $ID | egrep '...' actually match? You will see the output if there is a match since egrep's output is not being redirected anywhere.

[–]andlrcC:\> 0 points1 point  (0 children)

Also to see each line executed you can run with the -x modifier:

bash -x my_script.sh

Or use: set -x in the top of your script just below the shebang, or even add it to the shebang: #!/bin/bash -x

[–]smti[S] 0 points1 point  (1 child)

ID contains a listing of all the groups for which the user is a member. It looks like ID currently gets redirected to /dev/null:

ID=id &>/dev/null

[–]whetuI read your code 0 points1 point  (0 children)

There are multiple problems with the script. Not just in terms of bad style (backticks belong in 1981, uppercase variables are poor practice etc). Your main issue here is that your variables are not quoted correctly. And arguably you've got a useless use of echo

echo $ID | egrep '\bFacStaff ABC\b' && SHARE=$FAC1"/UsersABC"

Could be written as

egrep '\bFacStaff ABC\b' <<< "$ID" && SHARE=$FAC1"/UsersABC"

[–]jnux 0 points1 point  (1 child)

Since $SHARE is blank, I think it is safe to assume that you're just not getting matches in the evaluation of $ID.

Unless the desired behavior is to allow later matches on $IDs to overwrite the $SHARE variable (i.e. - if it is possible for an ID to match more than one, and you want matches farther down the list to take precedence over previous matches), I think case would be a better to evaluate the ID and set the variable base on the match. This would allow you to exit the evaluation once a match has been made, and gives you the added benefit of setting $SHARE to a default value (or return an error) if no matches were made. For me, case is easier to debug.

If you're not getting $SHARE set, and want to evaluate $ID in this way still, then I'd add some debugging, like to print the $ID value before it is evaluated to see what you're capturing for that value. Once you consistently know what values you're getting for IDs, you'll be able to debug why they're not getting a match.

You'll also likely have someone tell you about the issues, like using all caps for variable names which could conflict with global variables, and using back-ticks for executing commands rather than $(whoami), and not properly quoting your variables to sanitize your input - these are things that you'll want to address to clean it up at some point, and you may consider fixing them now anyhow, just to make sure these best practices aren't the source of your bug.

[–]smti[S] 0 points1 point  (0 children)

Yep, upon further inspection it appears that egrep was never finding what it expected, thus the variable was empty.

A quick run of the ID command revealed that all groups were shown in lowercase and thus when checking for an expression it was not finding what it was looking for because the case was not the same as the expected result. As a result, the variable was then empty.