LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script to add password to new user and run automatically. (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-to-add-password-to-new-user-and-run-automatically-4175452434/)

nevr2l899 03-02-2013 05:05 PM

Shell script to add password to new user and run automatically.
 
I am not worried about security because this is just a basic beginners script that I am running. Using the simple and traditional commands to create a user and add to a group, then prompt for a password. I would like to be able to echo the password into the script so that it does it automatically when the script is run. So.....

useradd -N -g (group) (user)
passwd (user)
#This is where I would like to try to echo the password directly into the script to run automatically#

Simplicity is important because Im a beginner. Thank you!

273 03-02-2013 05:16 PM

Perhaps chpasswd would be easier to use?
http://linux.die.net/man/8/chpasswd

nevr2l899 03-02-2013 05:27 PM

I tried to add chpasswd (user)(password) after the line passwd (user) and I got a cursor that when clicked said chpasswd: Line 1 empty. Remember, I am totally new and just learning syntax and how to use options correctly. I guess I thought that there would be some kind of echo command to append to or make the passwd command read.

273 03-02-2013 05:30 PM

You use chpasswd instead of passwd so you would use something like:
Code:

chpasswd newuser:newpassword
instead of your passwd line.

nevr2l899 03-02-2013 05:41 PM

That is what I did and it didnt do anything, just gave me a solid cursor and when I hit any button I got the error

chpasswd: Line 1: missing new password

Then I hit it again and it said:

chpasswd: Line 2: missing new password

and so on.... :(

Thanks for your help, Im still googling and reading everything I can find to try to find a solution.

273 03-02-2013 05:48 PM

Oh, sorry, you mean you want to run your script like:
Code:

./myscript username password
and have it create the user and change the password?
In that case (for the password change at least) you would do something like:
Code:

#!/bin/bash
chpasswd $1:$2


nevr2l899 03-02-2013 06:26 PM

Well its a fully functioning script adding users and groups and directories and also giving permissions and all of that. The password part is just something Im trying to do so that when the script is run you dont have to type the new passwords for the users. I want it all automated. Im sorry if you dont understand what I am trying to do but I dont know how else to explain it with my limited Linux vocabulary. The chpasswd thing just isnt working when I run the script. Thank you for trying to help.

273 03-02-2013 06:28 PM

How about posting the script you are trying to run rather than just explaining what you're not doing? That way others can help.

nevr2l899 03-02-2013 06:40 PM

I thought I had, atleast the only part that is involved in this question.

useradd -N -g Sales public #This is creating the user public and adding it to Sales group
passwd public #This is prompting the script to ask for new password
????? #Trying to find code that automatically echos or inserts password so script doesn't ask for it.


This is the best I can do since I am new on these forums and still dont know how to copy and paste my vi screen.

273 03-02-2013 06:58 PM

So are sale sand public in the script? If so then just using chpasswd public:password ought to do it.
The use of $1 and $2 I mentioned are so that you call the script with, for example, the username and password so that you don't have to modify it every time you want to create a new user.
I am confused how chpasswd username:password is is failing.

chrism01 03-03-2013 08:06 PM

chpasswd should work, but here's an alternative (given you're not worried about security)
Code:

for USER in john alex dave bryan sarah
do
    useradd $USER
    echo password | passwd --stdin $USER
done

You could also look at http://linux.die.net/man/8/newusers, which is designed to bulk create users.

nevr2l899 03-04-2013 06:33 AM

Thank you but I had to submit my assignment already. The only question I have is, is does this have to be done inside of a function? That is the only way it can read do/while and if/else statements right? I will take note of this and make a new script to try it out. Again, thank you for responding.

chrism01 03-04-2013 06:45 AM

You could put it in a fn, but its not a requirement.


All times are GMT -5. The time now is 07:26 PM.