Here’s my test bash script. I’m not able to get it to work. I’m seeing two errors:
Use of uninitialized value $answer in chop at /usr/sbin/adduser line 589.Use of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 590.
Here’s my script:
#!/bin/bash sudo adduser myuser << ENDX password password First Last Y ENDX exit 0
Here is the output:
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="026f67426f7b616d6f7277766770">[email protected]</a>$ ./adduser.sh
Adding user `myuser' ...
Adding new group `myuser' (1001) ...
Adding new user `myuser' (1001) with group `myuser' ...
Creating home directory `/home/myuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
Changing the user information for myuser
Enter the new value, or press ENTER for the default
Full Name []: Room Number []: Work Phone []: Home Phone []: Other []: Use of uninitialized value $answer in chop at /usr/sbin/adduser line 589.
Use of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 590.
Is the information correct? [Y/n] <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="325f57725f4b515d5f4247465740">[email protected]</a>$
This is on Kubuntu 12.04 LTS
$ bash --version bash --version GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
Here are the lines from adduser (system script – unmodified by me) with notations of the two relevant line numbers:
for (;;) {
my $chfn = &which('chfn');
&systemcall($chfn, $new_name);
# Translators: [y/N] has to be replaced by values defined in your
# locale. You can see by running "locale yesexpr" which regular
# expression will be checked to find positive answer.
print (gtx("Is the information correct? [Y/n] "));
chop (my $answer=<STDIN>); <-- LINE 589
last if ($answer !~ m/$noexpr/o); <-- LINE 590
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Just use the command line parameters instead of stdin, and use chpasswd for the password.
For example:
sudo adduser myuser --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password echo "myuser:password" | sudo chpasswd
Method 2
adduser calls the external program chfn to read the full name and other user information. chfn reads a bufferful of input, including not only what it needs but that last Y line. When adduser asks for confirmation afterward, the Y line has been read (but ignored) by chfn, so adduser sees an end of file on its input. The variable $answer is expected to contain a line of input, but because there was no input to read, it is undefined.
Since you’re writing a script, pass the data (except the password) on the command line.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0