0

I want to create a new user in a specific group when I issue the below command.

sudo ./create_user.sh "test" "firstname lastname"

The create_user.sh script looks as below.

#!bin/bash
#sudo adduser --gecos "$2" --ingroup testgroup $1

However, the problem is, I have to avoid the password insertion (it should be simply test) with a here document, but I have no idea about the syntax.

I tried some solutions but none of them seemed to work.

This is how my Try to of the heredocument in the script looks like

#<< EOF
#test
#test
#EOF

my output looks like this

#adding user 'test' ...
#adding new user 'test' <1003> with group 'testgroup' ...
#Enter new UNIX password:
Newguy
  • 1
  • 2

1 Answers1

0

adduser calls the passwd utility to set a password. Like many programs that prompt for a password, passwd insists that its output comes from a terminal. So you cannot pass it input via a here-document — that creates a temporary file or a pipe, and passwd accepts neither.

You can pas adduser (at least the Debian version) the --disabled-password option if you don't want to be able to log in with a password (you'll still be able to log in with other methods such as SSH keys if you set them up). adduser doesn't have an option to pass the password hash (not the password itself), but useradd from the Linux shadow password utility suite does.

If you want to enter a password during the account creation, make sure that passwd reads from the terminal. If you obtain the password from some other source (e.g. a web form), call passwd from an expect script — example.

If this is an exercise in using a here document, the password is not where you should use it. I don't see any problem that a here document would solve in what you've posted.