LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I'm trying to write a script to add users non-interactively (https://www.linuxquestions.org/questions/linux-newbie-8/im-trying-to-write-a-script-to-add-users-non-interactively-4175560536/)

linuxman94 12-03-2015 10:56 AM

I'm trying to write a script to add users non-interactively
 
The script should read the names of the users (each on a line by themselves) from STDIN

It should set the password (to the user’s name), ensure directory creation, and lock the account after creation.

Your script should check to see if the user already exists and NOT attempt to add the user if the user exists by using `cat /etc/passwd | grep $username` to check and see if empty

should run like: cat /root/users | ./addusers.bsh

I've been stuck on this for a while, can anyone help me out?

berndbausch 12-03-2015 11:45 AM

Quote:

Originally Posted by linuxman94 (Post 5459225)
The script should read the names of the users (each on a line by themselves) from STDIN

Use read in a while loop.

Quote:

Originally Posted by linuxman94 (Post 5459225)
It should set the password (to the users name), ensure directory creation, and lock the account after creation.

Use passwd and mkdir.

Quote:

Originally Posted by linuxman94 (Post 5459225)
Your script should check to see if the user already exists and NOT attempt to add the user if the user exists by using `cat /etc/passwd | grep $username` to check and see if empty

That is known as cat abuse. My script never does that.

Quote:

Originally Posted by linuxman94 (Post 5459225)
should run like: cat /root/users | ./addusers.bsh
I've been stuck on this for a while, can anyone help me out?

More cat abuse.

Anyway, show us what you have tried so far. I doubt anybody here will write a script for you from scratch for free.

suicidaleggroll 12-03-2015 12:02 PM

Step 1) Ignore the script part of this assignment. How do you create a user, create its home directory, set its password, and lock it on the command line? Once you know what commands you need, stick them in a file and call it a script. That's all a script is, a sequence of standard commands you type on the command line. Once you know the commands, creating a script is nothing more than copying and pasting them into a text file.

Step 2) Add input parsing. How do you read a file to get the user names? Add it to your script and wrap your above commands inside a loop over these user names.

Step 3) Add error checking. How do you check if a user already exists? Add it to your script.

If you run into a problem, ask, but it needs to be a specific problem, not the entire assignment verbatim.


All times are GMT -5. The time now is 11:48 AM.