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
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.
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.
@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.