About adding users from a text file using bash


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers About adding users from a text file using bash
# 1  
Old 09-08-2015
Debian About adding users from a text file using bash

hello,

I try to add users from a text file with this form:

username:groupename:homedir


first i extract data which is separated by ":"
then i use useradd to add users/groups.

but,,, my code doesn't works :


Code:
#!/bin/bash

echo "give me a text file: "
read dir


# control if the file is valid
if [ -f $dir ]
then
       for user_dets in $(dir)      #loop and extract data
       do
                    user= echo "$user_dets" | awk -F\: '{print $1}'
                    group= echo "$user_dets" | awk -F\: '{print $2}'
                     home= echo "$user_dets" | awk -F\: '{print $3}'
                 
                     useradd -g "$group" -d "$home"  $user
 
        done
else
        echo "file is not valid"
fi

I found this code which probably do the same as awk 'also doesn't works':

Code:
user = $(echo $ligne | cut -d':' -f1)

Can someone help me please?
# 2  
Old 09-08-2015
Maybe. You should post WHAT doesn't work. I guess you're using a recent version of bash?
You read in a file name, but never read FROM that file. I bet you get a "command not found" error for the $(dir), and user_dets are never assigned to.
Try e.g.
Code:
while IFS=":" read user group home
  do useradd ...
  done < $dir

and report back.
# 3  
Old 09-08-2015
How about this?

Code:
# control if the file is valid
if [ -f $dir ]
then
  awk -F: '{ printf("useradd -g %s -d %s %s\n", $2, $3, $1) }' $dir > tmp.shl
  chmod +x tmp.shl
  ./tmp.shl
else
  echo "file is not valid"
fi

This is a modification of the last lines of your code, be sure to put back the ones I left out.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to select text and apply it to a selected file in bash

In the bash below I am asking the user for a panel and reading that into bed. Then asking the user for a file and reading that into file1.Is the grep in bold the correct way to apply the selected panel to the file? I am getting a syntax error. Thank you :) ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Script for adding users to file permissions

I need a script to add the following two users ids to the permissions for various files: IIS_WPG and IUSR_CowGirl. I am fairly familiar with scripting but haven't been able to figure out how to do this via a script. Manually doing it is slow. I don't want to create users but only add them to a... (2 Replies)
Discussion started by: Stu Loventhal
2 Replies

3. Windows & DOS: Issues & Discussions

Script for adding users to file permissions

I need a script to add the following two users ids to the permissions for various files: IIS_WPG and IUSR_CowGirl. I am fairly familiar with scripting but haven't been able to figure out how to do this via a script. Manually doing it is slow. I don't want to create users but only add them to a... (2 Replies)
Discussion started by: Stu Loventhal
2 Replies

4. Shell Programming and Scripting

adding a line to a text file

I have a tab delimited text file, id name distance 1 3325167 0.334561754018 2 3290488 0.389444269458 3 3288794 0.392312701782 4 3347602 0.392532202097 5 3295355 0.394394169485 I need to add a line after the header line. The first and third field of... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

5. Shell Programming and Scripting

Adding text into file

Hello, I after some assistance please. Below is a file I need to read and for each line write an output. My input file looks like this 2 20008 2003-08-26 2 20032 2003-08-26 2 20041 2003-08-26 2 20037 2003-08-26 2 20050 2003-08-26 6 ... (6 Replies)
Discussion started by: giles.cardew
6 Replies

6. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

7. Shell Programming and Scripting

Adding Text To Middle Of File

Hi I am trying to write a bash script to add a line of text to the middle of a file. The way I worked it out was to calculate the number of lines and divide by 2. The file I am using is called newfile and it just contains lines of data. The new file I have created I have called midfile and... (7 Replies)
Discussion started by: BundBash
7 Replies

8. Shell Programming and Scripting

adding text to a file

Hi All I am making a script in which.I have a problem i try to discribe. File1 content need to be append in file 2 but such as if content already exist then dont add. File1 IS Like this myname yourname hername hisname File2 %AddNameHere myname yourname hername hisname (3 Replies)
Discussion started by: aliahsan81
3 Replies

9. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

10. Shell Programming and Scripting

adding text to a file between lines

Suppose content of my first file: first line second line third line How can i insert text between "first line" & "second Iline" Any help?????/ (7 Replies)
Discussion started by: bishweshwar
7 Replies
Login or Register to Ask a Question