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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.