0

When i run this script on my machine with the given file i get this error, same when using groupadd command.

#!/bin/bash
while IFS="," read -r first last department
do
    department=`echo "${department,,}" | tr -d '\n'`
    echo "$department"
    echo "$(sudo /usr/sbin/addgroup $department)"
done < <(tail -n +2 EmployeeNames.csv)

EmployeeNames.csv

FirstName,LastName,Department
Iwan,Meza,IT
Bradley,Seymour,Executive
Phillip,Mcdowell,Executive
Abdullah,Davenport,HR
Danni,Bate,Administrative
Agata,Moore,IT
Chance,Knight,Administrative
Darcy,Solomon,Finance
Corinne,Hilton,Finance
Hayley,Miles,Executive
Charli,Reyes,Administrative
Iwan,Meza,IT
Kaira,Fry,Administrative
Veronica,Byrd,Executive
Mccauley,Holding,CallCentre
Rudi,Khan,CallCentre
Chance,Knight,Administrative
Christiana,Nunez,CallCentre
Abubakar,Mclaughlin,Finance
Giles,Durham,CallCentre
Madelaine,Busby,Administrative
Jareth,Blake,CallCentre
Mccauley,Holding,CallCentre
anukul,nukul,kath

I get this error (this is just one error all department returns same error.

kath
addgroup: To avoid problems, the username should consist only of
letters, digits, underscores, periods, at signs and dashes, and not start with
a dash (as defined by IEEE Std 1003.1-2001). For compatibility with Samba
machine accounts $ is also supported at the end of the username
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
moongdal
  • 21
  • 1
  • 3
  • Your file has dos line endings. What does `echo "$department some text"` shows? – KamilCuk Apr 08 '22 at 16:24
  • Please add output of this command to your question (no comment): `file EmployeeNames.csv` – Cyrus Apr 08 '22 at 16:31
  • The `tr -d '\n'` is innately unnecessary; if you don't use `-d` to override the record separator, `read` will never put a line feed in a variable. On the other hand, you **do** have `\r` characters (which are carriage returns, not line feeds). – Charles Duffy Apr 08 '22 at 16:37
  • Anyhow, `department=${department,,}; department=${department//$'\n/}` is much, *much* faster than starting up a pipeline (though as I said, it's `\r`s, not `\n`s, that you need to be removing). – Charles Duffy Apr 08 '22 at 16:39
  • Also, there's never a good reason to `echo "$(somecommand)"` instead of just running `somecommand` on its own. `sudo /usr/sbin/addgroup "$department"` will work fine (once you've fixed the carriage-return problem). – Charles Duffy Apr 08 '22 at 16:39
  • @Cyrus $ file EmployeeNames.csv EmployeeNames.csv: CSV text – moongdal Apr 08 '22 at 16:50
  • i did what @CharlesDuffy said by replacing \n with \r and the script worked fine. Thank's guys have a great day – moongdal Apr 08 '22 at 16:54
  • It is not acceptable here to edit your post to add the solution, or to put [SOLVED] in the post title. – Ken White Apr 08 '22 at 17:17
  • @moongdal, let me reiterate what Ken said above -- answers should never be edited into questions, even closed ones. See f/e https://meta.stackoverflow.com/questions/404545/is-it-acceptable-to-answer-a-closed-question-by-editing-the-answer-into-the-ques and other related discussion on [meta]. If you continue to revert edits bringing the question into compliance with rules, we'll need to pull in a moderator to lock it. – Charles Duffy Apr 08 '22 at 18:03

0 Answers0