LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2013, 04:56 AM   #1
Mike0
LQ Newbie
 
Registered: Mar 2013
Posts: 3

Rep: Reputation: Disabled
BASH script for making users from text file


Hello world!

I have the following code written in BASH. It reads from a .txt file and makes 6 users with their home directories and copies some files and folders to the home directories.

The password is read from the third field in the .txt line, separated by comma(,). It is a 8 digit number.

The script makes the users, but I cant login, it says wrong password.

Also, the users home folders get with them a bonus folder named "Examples" and three config files. I need to use the correct -argument in the useradd command for not having these "bonus" folders, I want the home folder to be empty, only with the manualy added "gradiva" folder which I added with mkdir.

Thanks for any help!

#!/bin/bash

FILE=/home/administrator/seznam.txt
LOG=/home/administrator/postopek.log

while IFS=, read USER USERNAME PASSWORD

do

sudo useradd -p $PASSWORD -c "$USER" -m -U -s /bin/bash $USERNAME

echo "Ustvaril sem uporabnika $USER z uporabniškim imenom $USERNAME in vpisno številko $PASSWORD">>$LOG;

sudo mkdir /home/$USERNAME/gradivo

sudo cp -r /home/administrator/vaje/* /home/$USERNAME/gradivo

sudo chown -R $USERNAME:$USERNAME /home/$USERNAME/gradivo

done < $FILE
 
Old 03-31-2013, 05:25 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
I think the useradd -p expects the password to be encrypted.

man useradd:
Code:
 -p, --password PASSWORD
           The encrypted password, as returned by crypt(3). The default is to disable the
           password.

           Note: This option is not recommended because the password (or encrypted password) will
           be visible by users listing the processes.

           You should make sure the password respects the system's password policy.
So you should encrypt the passwords before passing them to useradd.
Alternatively, you may use the chpasswd utility to set the passwords after creating the user accounts.


edit:

Quote:
Originally Posted by Mike0
Also, the users home folders get with them a bonus folder named "Examples" and three config files. I need to use the correct -argument in the useradd command for not having these "bonus" folders, I want the home folder to be empty, only with the manualy added "gradiva" folder which I added with mkdir.
man useradd:
Code:
       -m, --create-home
           Create the user's home directory if it does not exist. The files and directories
           contained in the skeleton directory (which can be defined with the -k option) will be
           copied to the home directory.
[...]
       -k, --skel SKEL_DIR
           The skeleton directory, which contains files and directories to be copied in the
           user's home directory, when the home directory is created by useradd.

           This option is only valid if the -m (or --create-home) option is specified.

           If this option is not set, the skeleton directory is defined by the SKEL variable in
           /etc/default/useradd or, by default, /etc/skel.

Last edited by millgates; 03-31-2013 at 05:31 AM.
 
Old 04-05-2013, 12:21 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Please place your command arguments with variables inside double quotes as well:
Code:
"$PASSWORD"
"/bin/bash $USERNAME"
"$LOG"
"/home/$USERNAME/gradivo"
"$USERNAME:$USERNAME"
"/home/$USERNAME/gradivo"
"$FILE"
As that would be safer and portable with values containing spaces especially with unrestrictive directories.
 
Old 04-05-2013, 02:31 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Another option is the newusers tool http://linux.die.net/man/8/newusers
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read a text file using a bash script Jeroen1000 Programming 8 09-30-2009 06:53 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
making a bash script file Berticus Linux - General 1 10-15-2005 11:14 PM
Change Text-File through bash script arkus Programming 2 12-17-2004 08:48 PM
Need help reading text file in bash script scilec Programming 3 11-25-2004 06:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration