0

I'm writing a Bash script. My problem is I want to read from the text file line-by-line and split the line by a character. I want pure Bash code.

Let's say I have this in the text file:

Format: (name;username;code)

John Doe;johnDoe;534092
John Dor;johnDor;923845
Joan Doe;Joan;232423

I want to know how to split the string (line) by character ";".

I need to know this because of what I want to do next: I want to read this text file and for every name in the file make a new user (with the same home directory as name), the password should be the code, and default should be Bash.

I'm reading the file with while read line ....

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
JohnDoeTheOne
  • 141
  • 1
  • 2
  • 16

1 Answers1

10
while IFS=';' read -r fullName userName password; do
    useradd ... # $fullName, $userName, and $password are available
done < users.txt
 
tripleee
  • 175,061
  • 34
  • 275
  • 318
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • @JohnDoeTheOne I tried this verbatim at the command line and it works. Make sure you don't have any typos (like `$fullname` vs. `$fullName`). – John Kugelman Apr 03 '13 at 20:17