Bashスクリプトにunix / linuxユーザーを追加する方法

Bashスクリプトにunix / linuxユーザーを追加する方法

これは私のテストbashスクリプトです。私はそれを動作させることはできません。 2 つのエラーが表示されます。

  1. Use of uninitialized value $answer in chop at /usr/sbin/adduser line 589.
  2. Use of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 590.

これは私のスクリプトです。

#!/bin/bash
sudo adduser myuser << ENDX
password
password
First Last




Y
ENDX
exit 0

出力は次のとおりです。

me@mycomputer$ ./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] me@mycomputer$

これはKubuntu 12.04 LTSにあります。

$ bash --version
bash --version
GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)

以下は、2つの関連行番号のシンボルを含むadduser(システムスクリプト - 私は変更しません)の行です。

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
}

ベストアンサー1

stdinの代わりにコマンドライン引数を使用し、パスワードとしてchpasswdを使用してください。

たとえば、

sudo adduser myuser --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password
echo "myuser:password" | sudo chpasswd

おすすめ記事