Add user based on file


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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”

Code:
#!/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.
# 2  
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:

Code:
#!/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


Code:
$ ./myuseradd
myuseradd <filename>
$ echo $?
1

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

$ ./myuseradd rubbish
Input File Not Found
$ echo $?
3


Last edited by Chubler_XL; 09-24-2019 at 11:27 PM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #286
Difficulty: Easy
AIX Version 1 was introduced in 1986 for the IBM RT PC workstation, was based on HP-UX.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Deletion of list of user based on a text file In LDAP UNIX server

Dear All, It would be really nice, if you could help me to write a script for deletion of list of user( more than 15000 users) stored in a file and sorted by email address( i need deletion of only a particular type of mail address). Is the any script to write and take the file as input and... (3 Replies)
Discussion started by: Chand
3 Replies

2. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

4. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

5. Shell Programming and Scripting

Search on date range of file based on user input

Hello I would like to ask for help with a script to search a directory that contains many log files and based on a users input after being prompted, they enter a date range down to the hour which searches the files that contain that range. I dont know how to go about this. I am hoping that the... (5 Replies)
Discussion started by: lostincashe
5 Replies

6. Solaris

cannot add PATH to user crontab file

hi All, here is the problem: I'm not able to specify a PATH inside the user crontab file (/var/spool/cron/crontabs). The only syntax it accepts is the usual "* * * * * file" I'm not able to add PATH, or HOME, or MAILTO, or anything else. when I try to save the crontab, I have the error: ... (1 Reply)
Discussion started by: joe_x
1 Replies

7. Shell Programming and Scripting

Append file based upon user input-- solved

Ok, I have a script with a commandline option that allows the user to add a custom function to the script file. I have tried everything in my limited knowledge of sed to get this to work and keep coming up short. I need sed to search for a line starting with a pattern, I've got that part so far,... (0 Replies)
Discussion started by: DC Slick
0 Replies

8. Programming

how to add user defined tag in a mp3 file?

Hai all, Can anyone explain me about how to add an user defined tag in an mp3 file using MP3::Tag module in perl? (0 Replies)
Discussion started by: thillai_selvan
0 Replies

9. Shell Programming and Scripting

Update LDIF User info based on Test User Certs ID's

Hi I need help.......... I have an Sun One Directory server LDIF file with 5000 user entries, I need to change the data to match Test ID's, so I can run a perf test. I'm way out of my league as I have not done any scripting for 10 years. There are four entries for each user in the file... (3 Replies)
Discussion started by: Macdaddy99
3 Replies

10. Shell Programming and Scripting

add lines automatically based on a field on another file

hello I have a number of lines that need to be added at the end of a file each time I add a field in another file (let's name it file2) file2 has this format: filed1:field2:path1:path2:path3:path... Whenever I add a path field, I should add to file1 these lines: <Location path1>... (0 Replies)
Discussion started by: melanie_pfefer
0 Replies

Featured Tech Videos