Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a dat file with names. These are the names of users I must add, without groups or passwords. Ofcourse i have to remove all spaces. I have to add them with a bash script. Can anyone help me write the script?

File:

Marianne        Earman
Justina Dragaj
Mandy   Mcdonnell
Conrad  Lanfear
Cyril   Behen
Shelley Groden
Rosalind        Krenzke
Davis   Brevard
Winnie  Reich
Trudy   Worlds
Deshawn Inafuku
Claudio Loose
Sal     Pindell
Cristina        Sharper
Tricia  Kruss

My script:

#!/bin/bash
#users.dat | sed "s/\t\t*/ /g"
while read p; do
  #var=$(echo $p | sed "s/\t\t*/ /g")
  var= echo $p | sed '/^$/d;s/[[:blank:]]//g'
  #sudo useradd $var
  sudo useradd $var
 done < users.dat
share|improve this question
    
You are just specifying "write the script". But How? Where is the dat file? What is the output you are expecting? –  Ramesh Mar 31 at 17:13
    
The file is saved on my computer (/home/tjasa/Documents/). I have to add names in that file as users. Now i heve something like that: #!/bin/bash if [$# -ne 0 ] then echo "usage: $0 < file' exit 1 fi first=cut -f 1 -d ',' user_list usern=$first| tr 'A-Z' 'a-z' while read line; do echo "adding $usern\n" done < users.dat –  user64047 Mar 31 at 17:24
2  
Welcome to Unix & Linux Stack Exchange! Please edit your question to add extra info, it is hard to read and easy to miss in the comments. Make sure you include an example if this "dat" file and show what you have tried so far. –  terdon Mar 31 at 17:33

2 Answers 2

This can be done in a single pipeline - just use xargs to run useradd for every line in its input:

sed '/^[[:space:]]*$/d; s/[[:space:]]//g' users.dat |
  xargs -I{} echo {}

Replace echo with useradd when you are sure it is doing the right thing.

Note also that if you are writing this for a Debian based system (eg Ubuntu, Linux Mint), you should consider using adduser instead of useradd. For more details see - What does adduser do that useradd doesn't?

share|improve this answer
    
Nice one! But as far as I understand it, you don't need to specify -d when also using -I since the latter one implies '\n' being the input separator. –  Andreas Wiese Mar 31 at 19:39
    
@AndreasWiese, that's right, thankyou. –  Graeme Mar 31 at 19:46

You need to store the output of the echo in the var variable by appending $ before executing the command. By that way, var will now have the previous command output. Try the below script. It should work fine.

#!/bin/bash
while read p; do
var=$(echo $p | sed '/^$/d;s/[[:blank:]]//g')
  echo $var
  #sudo useradd $var
 done < users.dat 
share|improve this answer
    
You had the output of the echo statement assigned correctly to var variable, but you have commented it out in your original script. –  Ramesh Mar 31 at 19:07
    
This will still attempt an adduser (or echo) with empty lines. Deleting them with sed makes no difference. One thing you could do is check to see if var is an empty string (and drop the unnecessary /^$/d; part in sed) before doing the last part. –  Graeme Mar 31 at 19:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.