1
#!/bin/bash

counter=2

while [ $counter -lt 19 ]
do



        username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1 
        psswd= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f2 
        full_name= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f3 
        group= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f4 
        second_group= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f5 

        sudo useradd $username -m -g $group -s /bin/bash -c $full_name

        if [ second_group = LPGestionnaires ]
        then 
                usermod -a -G $second_group $user
        fi

        #echo "$username:$psswd" | chpasswd

        ((counter++))
done
echo Execution complete

The part where it says sudo useradd $username -m -g $group -s /bin/bash -c $full_name is the part that isn't working, my -g option isn't seeing the variable $group argument as an argument, when I execute my script it returns this: useradd: group '-s' does not exist

I'm pulling the data from a .csv file that is located correctly.

If anyone could help that would be great!

Thanks!

wjandrea
  • 14,504

2 Answers2

5

It seems you wanted to assign the result of the head ... command to the variable username here:

username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1 

That is incorrect syntax. Correct it like this:

username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)

And then do the same for the other variables too, which all have the same problem.

Also, change the sudo useradd command like this:

sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"

Variables used in command line arguments should usually be double-quoted to avoid word splitting.

janos
  • 4,968
0

The final code is this for anyone that is curious:

#!/bin/bash

counter=2

while [ $counter -lt 19 ]
do



        username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)
        psswd=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f2)
        full_name=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f3)
        group=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f4)
        second_group=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f5)

        sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"

        if [ "$second_group" = LPGestionnaires ]
        then 
                sudo usermod -a -G LPGestionnaires "$username"
        fi

        echo "$username:$psswd" | sudo chpasswd

        ((counter++))
done
echo Execution complete