0

This is the code I have to create user accounts from a text file. The usernames are being from the text file, and i want to set the password to unix lab. This script does not however seem to work.

#!/bin/sh
for i in `cat unix_tuesday.txt`
password = "unixlab"
do
echo "adduser  $i"
adduser $i 
echo -e "$password" | passwd $password

done
user3078335
  • 781
  • 4
  • 13
  • 24

3 Answers3

1

There are a couple of issues :

  • your for loop is broken (the do is wrongly placed)
  • passwd requires a special option for stdin input

This should do what you requested :

#!/usr/bin/env bash
for i in $(cat unix_tuesday.txt); do
   password=unixlab
   adduser $i 
   echo $password | passwd $i --stdin
done

Also take into account that there is a difference between adduser and useradd.

kvantour
  • 25,269
  • 4
  • 47
  • 72
0

Running this through ShellCheck reports:

Line 2:
for i in `cat unix_tuesday.txt`
^-- SC1073: Couldn't parse this for loop. Fix to allow more checks.

Line 3:
password = "unixlab"
^-- SC1058: Expected 'do'.
^-- SC1072: Expected 'do'. Fix any mentioned problems and try again.

The password assignment is in between the for and the do. Moving it to the beginning yields a new set of errors:

Line 2:
password = "unixlab"
         ^-- SC1068: Don't put spaces around the = in assignments.

Line 3:
for i in `cat unix_tuesday.txt`
         ^-- SC2013: To read lines rather than words, pipe/redirect to a 'while read' loop.
         ^-- SC2006: Use $(..) instead of legacy `..`.

Line 6:
adduser $i
        ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 7:
echo -e "$password" | passwd $password
     ^-- SC2039: In POSIX sh, echo flags are undefined.
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

man adduser

-p, --password PASSWORD The encrypted password, as returned by crypt(3). The default is to disable the password.

Note: This option is not recommended because the password (or encrypted password) will be visible by users listing the processes.

pawel7318
  • 3,383
  • 2
  • 28
  • 44