Skip to content
Advertisement

BASH syntax error at line 1: `then’ unmatched

JavaScript

Hi, I am having issues with this particular loop and couldn’t find a solution to it, any ideas why?

JavaScript

Advertisement

Answer

If you run the code through shellcheck and correct the errors which it shows (except for one problematic warning), the code will become:

JavaScript

The code still seems to have issues, one of which is that it looks for duplicate user names but the print out claims that it found duplicate group names.

I would suggest replacing the above with:

JavaScript

How the awk code works

In the output of getent passwd, the user name will be in field 1 and the user ID will be in field 3.

  • c[$1]++; uids[$1]=uids[$1]" "$3

    This counts the number of times that user name $1 occurs and saves the count in array c. It also saves the user ID, $3, associated with each name in array uids.

  • END{for (name in c) if (c[name]>1) printf "Duplicate User Name (%s):%sn",name, uids[name]}

    After we have finished processing getent’s output, this looks for user names for which the count is greater than 1 and prints the info.

Multiline version of suggested code

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement