Skip to content
Advertisement

Operators and user input not found / unexpected while checking if users and groups exist in /etc/group and /etc/passwd

I can’t understand what’s wrong with my code. I’m trying to make it so that if the passed in username and group are both not found in /etc/group and etc/passwd echo both not found.

If one but not the other is found echo one is found and finally if they are both found then echo both exist.

Here is my code:

JavaScript

Here is the output:

JavaScript

I’ve tested the grep and cut command in the command line and it works:

JavaScript

Advertisement

Answer

There is a missing ! in the shebang, the first line should be:

JavaScript

There are missing [ right after the elifs.

You could always use shellcheck command to parse and list all issues in a bash script:

JavaScript

Finally you could rely on grep command return status with if statements like the one below:

JavaScript

Give a try to this, which only parse each file once:

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