Frustrating Groups loop

I'm trying to list a bunch of users' groups and filter them out to be space delimited like this:

3610 14
25 2810

Using the "id" command they would look like this:

uid=39693(user1) gid=14(dev) groups=3610(a_dev),14(dev)
uid-39471(user2) gid=25(testing) groups=2810(prod),25(testing)

I want the script to echo a blank line if the "id" command returns no such user. However, my condition doesn't appear to like my syntax and I've viewed several other links about syntax inside the double brackets and the script will not accept my condition!

while read i
do
USER_HAS_GROUPS="`id $i`"
USER_LDAP_GROUPS="`id $i | cut -d\= -f4 | sed 's/([^()]*)//g;s/,/ /g'`"
if [[ "$USER_HAS_GROUPS" == *such* ]]
then
echo ""
else
echo $USER_LDAP_GROUPS
fi
done < txt1

I've tried variants such as without the quotes, and quotes around *such*, single brackets, single equals sign, etc. Doesn't that condition mean "if $USER_HAS_GROUPS contains the string "such" anywhere then the condition is true" ?

The "no such user" message is probably being output to stderr to capture it you will need to redirect stderr to stdout like this:

USER_HAS_GROUPS="`id $i 2>&1`"

Here is another thought, why not examine the return value from the id command something along the lines of this:

while read i
  if id $i > /dev/null 2>&1
  then
    USER_LDAP_GROUPS="`id $i | cut -d\= -f4 | sed 's/([^()]*)//g;s/,/ /g'`"
    echo $USER_LDAP_GROUPS
  else
     echo "Invalid user $i"
  fi
done < txt1

Not only does that kludge not work for me but I don't understand why...it keeps saying "-bash: syntax error near unexpected token `done'". I believe the syntax is correct...there's a while followed by a done, I even put in a "do" just in case. The if statement is properly enclosed with fi...not sure why this is happening but I can't verify your suggestion.

while read i
do
  if id $i > /dev/null 2>&1
  then
    USER_LDAP_GROUPS="`id $i | cut -d\= -f4 | sed 's/([^()]*)//g;s/,/ /g'`"
    echo $USER_LDAP_GROUPS
  else
     echo "Invalid user $i"
  fi
done < txt1

simple syntax error - missing "do"

1 Like

I don't understand ! I inserted the "do" before and it still returned an error!

while read i; do; if id $i > /dev/null 2>$1; then USER_LDAP_GROUPS="`id $i | cut -d\= -f4 | sed 's/([^()]*)//g;s/,/ /g'`"; echo $USER_LDAP_GROUPS; else echo "";fi; done < txt1
-bash: syntax error near unexpected token `;'

Jim yours works - thanks as always. I'm sure it's a syntax error on my part I'm just not readily seeing it. I tried several variations and still got an error.

Still returned what error?

-bash: syntax error near unexpected token `;'

Remove the ; from after the 'do'.

Or, better yet, stop squashing all your code onto one line so you can actually read it later.

I typically try to do it in the shell before I put it all into a script. In any event I have everything I need and this script is humming away nicely. Appreciate the help as always.

You don't have to squash it all onto one line when you type it into the shell. It will wait for you.

Correct, but then I press up to re-run it and it's all semi-colon'd.

@MainDotC
Yet again.
Please post what Operating System you are have and what Shell you prefer.

Please also post a description of the problem and what output you require. I cannot imagine that trying to parse the output from the id command is a sensible option.

@methyl - the issue is resolved, I was simply replying to jim's response...

Glad all is working now.