0

I receive an error: "/bin/ums: 19: [: is_duplicate: unexpected operator" when running the following script:

#! /bin/sh group="" #utility functions is_duplicate () { return grep $1 /etc/passwd /etc/group } #script echo "Welcome to the user management system." echo "Please enter the name of the group you wish to create:" read group while [ is_duplicate "$group" -eq 0 ] do echo "That group name already exists." echo "Please enter a new name:" read group done 

Why?

Note: The script is a work in progress. In this segment I am trying to grep for the name of a group being input in the console. If the group exists in the group file (i.e. grep returns exit code 0) then continue asking for a name.

1

1 Answer 1

0

To test the success as indicated by the zero exit code no parentheses are needed.

while is_duplicate "$group" ; do ... done 

Similarly, return expects a number, not the string grep. You can call grep without return as the last thing in the function, its exit code will be returned.

But you should be more specific in what you're grepping from the files.

grep $1 /etc/passwd /etc/group 

will return true even for user names or shell paths.

At least, quote the $1 (and maybe validate it a bit, you can try what e.g. -v : does).

3
  • grep -wF "$1" would be a better match. Or even awk -v ug="$1" '$1==ug {exit 0} END {exit 1}'CommentedMay 26, 2022 at 15:35
  • Thanks for the insight - removing parenthesis seems to have worked. Why does including them cause the error?
    – Nick
    CommentedMay 26, 2022 at 15:42
  • The syntax of [ is explained in man bash. It should start with -z or similar, or with any word followed by -eq and similar. It can't start with two unknown words is_duplicate and the group name (unless the group name is -eq etc., but then only one value is expected, not two, -eq 0.)
    – choroba
    CommentedMay 26, 2022 at 15:51

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.