0

With the following script I'm trying to create users,groups and password from a txt/cvs file.

Here is the script:

#!/bin/bash
set -xv
filein="file.csv"
IFS=$'\n'
if [ ! -f "$filein" ]
then
    echo "Cannot find file $filein"
else
    groups=$(cut -d: -f 6 "$filein" | sed 's/ //')
    fullnames=$(cut -d: -f 1 "$filein")
    usernames=$(cut -d: -f 1 "$filein" | tr '[:upper:]' '[:lower:]' | awk '{print substr($1,1,1) $2}')
fi

for group in ${groups[*]}
do
    grep -q "^$group" /etc/group ; (( x=$? ))
    if [ $x -eq 1 ]
    then
        groupadd "$group"
    fi
done

x=0
created=0
for user in ${usernames[*]}
do
  useradd -n -c ${fullnames[$x]} -g "${groups[$x]}" $user 2> /dev/null
  if [ $? -eq 0 ]
  then
      (( created=created+1 ))
  fi
done

echo "${usernames[$x]}" | passwd --stdin "$user" > /dev/null
echo "Complete. $created accounts have been created."

This morning I used shellchek on my script and I modify some of the code but I still get 2 problems.

One of the problem is that I'm getting an error from shellcheck saying:

In test.sh line 29:
useradd -n -c ${fullnames[$x]} -g "${groups[$x]}" $user 2> /dev/null
              ^-- SC2086: Double quote to prevent globbing and word splitting.
                                                  ^-- SC2086: Double quote to prevent globbing and word splitting.

The other problem I'm getting is when I'm trying to create passwords for the usernames( same as the username).

echo "${usernames[$x]}" | passwd --stdin "$user" > /dev/null

Ubuntu doesn't find the command --stdin and It's been a while that Im looking for a solution but without result.

I'm just starting creating shell scripts, If you guys can please help me running this script would be amazing.

Thanks in advance.

  • 1
    Shellcheck is telling you to do the same thing for fullnames and user that you did for groups i.e. "${fullnames[$x]}" and "$user". As for the other error, AFAIK passwd can only be used interactively - however you may be able to do what you want using chpasswd – steeldriver May 25 '18 at 0:24
  • Got it Thanks a lot, how it would be using chpassw? I couldn't understand how to canalize it reading my usernames – Elio Basciani May 25 '18 at 0:33
  • You just supply colon-separated username:password pairs to its standard input - see for example How to set user passwords using passwd without a prompt? – steeldriver May 25 '18 at 0:43
  • Do I have to write manually the pairs in my file.cvs? – Elio Basciani May 25 '18 at 1:46
  • No, if you already have the values in shell variables you can use "$user:$password" for example. However I can't follow your code logic (what is $x for example?). I suspect you are hugely over-complicating things - likely you can read all the information you need line-by-line (i.e. user-by-user) direct from the file in a single while loop. You could also set the password directly in the useradd command provided you encrypt it. – steeldriver May 25 '18 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.