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?


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:

#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
  • You are just specifying "write the script". But How? Where is the dat file? What is the output you are expecting? – Ramesh Mar 31 '14 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 '14 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 '14 at 17:33

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?

  • 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 '14 at 19:39
  • @AndreasWiese, that's right, thankyou. – Graeme Mar 31 '14 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.

while read p; do
var=$(echo $p | sed '/^$/d;s/[[:blank:]]//g')
  echo $var
  #sudo useradd $var
 done < users.dat 
  • 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 '14 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 '14 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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