Add user based on file

The script it should add all the users from this file "users.txt" All users should have the login shell as /sbin/nologin. When this script is called with any other argument, it should print the message as �Input File Not Found�. When this script is run without any argument, it should display �Usage: /root/newusers users.txt�

#!/bin/bash
if [ $# -eq 0 ]
then
echo "$(basename $0) <filename>"
fi

case $1 in
        userlist)
        if [ -f /home/ar/users.txt ]
        then
        for user in `cat /home/ar/users.txt|awk -F: '{print $1}'`
        do
        useradd -s /bin/false $user
        done
        echo "file is tehre"
        else
        echo "Input file is no there"
        fi
        ;;
esac


#cat users.txt
proxysync:x:1008:1008:Proxy Sync User:/home/proxysync:/bin/bash
gluster:x:992:988:GlusterFS daemons:/var/run/gluster:/sbin/nologin

The script works, and it created the user, but it doenst throw this "nput File Not Found� if theres no argument.

In the description you say the shell is to be /sbin/nologin but the script sets it to /bin/false. The usage message says <filename>, however the script expects userlist.

Use *) case pattern to trap everything else. Also exit with non-zero status when printing the usage string:

#!/bin/bash
if [ $# -eq 0 ]
then
   echo "$(basename $0) <filename>"
   exit 1
fi

case $1 in
    userlist)
        if [ -f /home/ar/users.txt ]
        then
           for user in `cat /home/ar/users.txt|awk -F: '{print $1}'`
           do
              useradd -s /bin/false $user
           done
           echo "file is tehre"
        else
           echo "Input file is no there"; exit 2
        fi
    ;;
    *) echo "Input File Not Found" ; exit 3;;
esac
$ ./myuseradd
myuseradd <filename>
$ echo $?
1

$ ./myuseradd userlist
Input file is no there
$ echo $?
2

$ ./myuseradd rubbish
Input File Not Found
$ echo $?
3
Moderator comments were removed during original forum migration.