geret13
geret13

Reputation: 3

Problems with groupadd

I'm playing around with linux a little bit, but hit a problem:

# main.sh
#!/bin/bash

while read line
do
    line=(${line//:/ })
    groupadd ${line[0]}
done <"config/groups.config"

# config/groups.config
Directie

# output
' is not a valid group name

While when I do this it works:

#!/bin/bash
groupadd Directie

Thank you for helping!
- Gerben van der Meer

Upvotes: 0

Views: 1718

Answers (1)

chepner
chepner

Reputation: 532303

config/groups.config has DOS line endings, which you'll need to remove. The actual value in ${lines[0]} is Directie\r, which accounts for the odd looking error message, which is actually something like

groupadd: 'Directie\r' is not a valid group name.

The \r causes your terminal to move the cursor back to the beginning of the line, which results in everything prior to the closing single quote being overwritten by the rest of the error message.

You can also simply strip the carriage return after reading each line.

while read line
do
    line=${line#$'\r'}
    line=(${line//:/ })

Upvotes: 1

Related Questions