1

I'm trying to create different users from a csv file in bash. This my code so far:

  #create arrays usernames and password 
  groups=(`cut -d: -f 1 "$filein"`) 
  usernames=(`cut -d: -f 3  "$filein" | tr [A-Z] [a-z] |awk '{print substr($1.$2}'`) 
  password=(`cut -d: -f 4 "$filein"`) 
  #creates the user accounts, adds them to groups, and sets passwords 
  #then once account is created 
  #checks if the group exists, if not then creates it 
  for group in ${groups[*]} 
  do 
  grep -q "^$group" /etc/group ; let x=$? 
  if [ $x -eq 1 ] 
  then 
  groupadd "$group" 
  fi 
  done 
  #creates the user accounts, adds them to groups, and sets passwords 
  #then once account is created 
  x=0 
  created=0 
  for user in ${usernames[*]} 
  do 
  useradd -n -c ${usernames[$x]} -g "${groups[$x]}" $user 2> /dev/null 
  chpasswd  "$user" > /dev/null 
  if [ $? -eq 0 ] 
  then 
  let created=$created+1 
  fi

For some reason, when I run the file, this is the error I get, even went let me add any users groupadd: 'Q123456,Bob,Bob,QwertY1' is not a valid group name groupadd: 'Q236578,Jane,Jane,AzertY2' is not a valid group name Any help will be appreciated. Thank you

dnoj
  • 19
  • 5
  • This might help: [How to debug a bash script?](http://unix.stackexchange.com/q/155551/74329) – Cyrus Apr 08 '20 at 16:55

1 Answers1

0

Your delimiter in cut command is " : " instead of " , "

From cut manpage:

-d, --delimiter=DELIM

use DELIM instead of TAB for field delimiter

Community
  • 1
  • 1
  • do you mean usernames=(`-d,DELIM: -f 3 "$filein" | tr [A-Z] [a-z] |awk '{print substr($1.$2}'`) ?? – smart kinder Apr 08 '20 at 17:38
  • What I mean is: > cut -d , Every time you use cut in this script I.e. cut -d , -f 1 "$filein" – Krzysztof Czyż Apr 08 '20 at 17:45
  • to be honest i new to bash script i dont really fully understand you will look like this ?? usernames=cut -d( -f 3 "$filein" | tr [A-Z] [a-z] |awk '{print substr($1.$2}) – smart kinder Apr 08 '20 at 18:05
  • Those 3 lines should fix the issue groups=(`cut -d , -f 1 "$filein"`) usernames=(`cut -d , -f 3 "$filein" | tr [A-Z] [a-z] |awk '{print substr($1.$2}'`) password=(`cut -d , -f 4 "$filein"`) Explanation: -d switch within cut command is the character on witch to split the fields on. In your example (CSV) your delimiter is , (comma). – Krzysztof Czyż Apr 08 '20 at 18:12
  • ok thats make more seance now , i going to try it now thanks and thank you for patient – smart kinder Apr 08 '20 at 18:14
  • If it works i would kindly appreciate if you approved the answer :) – Krzysztof Czyż Apr 08 '20 at 18:26
  • not yet my linux mint get corrupted i need to install fresh one i will let you know if working or not i really appreciated bro cheers – smart kinder Apr 08 '20 at 21:54
  • no bro didint work test.sh: line 11: syntax error near unexpected token `|' test.sh: line 11: ` usernames=(cut -d , -f 2 "$filein" | tr [A-Z] [a-z] |awk '{print substr($1.$2}')' test.sh: line 36: syntax error: unexpected end of file – smart kinder Apr 09 '20 at 01:45
  • Cloud you explain what are you trying to achieve with " awk '{print substr($1.$2} " ? Also what do fields 2 and 3 stand for. Code suggests that field 3 is used for username ? – Krzysztof Czyż Apr 09 '20 at 09:56
  • " awk '{print substr($1.$2} " is to separate first name and surname with a comma and "f 2" and "f 3 "is to read column 2 and 3 i dont really need them this is my csv file look like 'Q123456,Bob,Bob,QwertY1' so i want to ready "bob.bob " as username an "QwertY1 " will be the password the group is private but its not necessary if you have a different solution i will appropriated – smart kinder Apr 09 '20 at 11:02
  • This line should work then usernames=(`cut -d , -f 2,3 "$1" | tr [A-Z] [a-z] | sed -r s/,/./`) It takes fileds 2 and 3 and converts ' , ' into ' . ' Result beeing bob.bob – Krzysztof Czyż Apr 09 '20 at 11:23
  • no that still give me an error " test.sh: line 10: syntax error near unexpected token `|' test.sh: line 10: ` usernames=(cut -d , -f 2,3 "$1" | tr [A-Z] [a-z] | sed -r s/,/./)' " – smart kinder Apr 09 '20 at 11:29
  • for some reason when i add this quote ' create 1 account but still give me error " Welcome! Your account has been created. Your username is cut -d , -f 2,3 "$1" | tr [A-Z] [a-z] | sed -r s/,/./ and password is "cut" without the quotes. Complete. 1 accounts have been created. " – smart kinder Apr 09 '20 at 11:33
  • You are probably missing the ' ` ' characters around that cut -d , -f 2,3 "$1" | tr [A-Z] [a-z] | sed -r s/,/./ statement. StackOverflow uses them as code blocks so you have to enter them manually – Krzysztof Czyż Apr 09 '20 at 12:06