1

My problem is to add a username to a file, I really stuck to proceed, please help.
Problem: I am having a file called usrgrp.dat. The format of this file is like:

ADMIN:srikanth,admin  
DEV:dev1  
TEST:test1

I am trying to write a shell script which should give me the output like:

Enter group name: DEV  
Enter the username: dev2

My expected output is:

User added to Group DEV

If I see the contents of usrgrp.dat, it should now look like:

DEV:dev1,dev2
TEST:test1

And it should give me error saying user already present if I am trying to add already existing user in that group. I am trying this out with the following script:

#!/bin/sh
dispgrp()
{
        groupf="/home/srikanth/scm/auths/group.dat"
        for gname in `cat $groupf | cut -f1 -d:`
        do
                echo $gname
        done
        echo "Enter the group name:"
        read grname
        for gname in `cat $groupf | cut -f1 -d:`
        do
                if [ "$grname" = "$gname" ]
                then
                        echo "Enter the username to be added"
                        read uname
                        for grname in `cat $groupf`
                        do

                                $gname="$gname:$uname"
                                exit 1
                        done
                fi
        done
}
echo "Group display"
dispgrp

I am stuck and need your valuable help.

2 Answers 2

1
#!/bin/sh
dispgrp()
{
        groupf="/home/srikanth/scm/auths/group.dat"
        tmpfile="/path/to/tmpfile"
        # you may want to pipe this to more or less if the list may be long
        cat "$groupf" | cut -f1 -d:
        echo "Enter the group name:"
        read grname
        if grep "$grname" "$groupf" >/dev/null 2>&1
        then
            echo "Enter the username to be added"
            read uname
            if ! grep "^$grname:.*\<$uname\>" "$groupf" >/dev/null 2>&1
            then
                sed "/^$grname:/s/\$/,$uname/" "$groupf" > "$tmpfile" && mv "$tmpfile" "$groupf"
            else
                echo "User $uname already exists in group $grname"
                return 1
            fi
        else
            echo "Group not found"
            return 1
        fi
}
echo "Group display"
dispgrp

You don't need to use loops when the loops are done for you (e.g. cat, sed and grep).

Don't use for to iterate over the output of cat.

Don't use exit to return from a function. Use return.

A non-zero exit or return code signifies an error or failure. Use 0 for normal, successful return. This is the implicit action if you don't specify one.

Learn to use sed and grep.

Since your shebang says #!/bin/sh, the changes I made above are based on the Bourne shell and assume POSIX utilities (not GNU versions).

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your valuable suggestion, i had tried both yours and glenn's solution. When I tried to execute script as suggested by you, it hangs after accepting a username.
@Srikanth: Sorry, I had a couple of errors that are now fixed.
Thank you for your valuable help. It got fixed.
1

Something like (assume your shell is bash):

adduser() {
    local grp="$1"
    local user="$2"
    local gfile="$3"

    if ! grep -q "^$grp:" "$gfile"; then
        echo "no such group: $grp"
        return 1
    fi

    if grep -q "^$grp:.*\\<$user\\>" "$gfile"; then
        echo "User $user already in group $grp"
    else
        sed -i "/^$grp:/s/\$/,$user/" "$gfile"
        echo "User $user added to group $grp"
    fi 
}

read -p "Enter the group name: " grp
read -p "Enter the username to be added: " user
adduser "$grp" "$user" /home/srikanth/scm/auths/group.dat

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.