这是我的测试 bash 脚本。我无法让它工作。我看到两个错误:
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.
这是我的脚本:
#!/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)
以下是 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
答案2
adduser
调用外部程序chfn
读取全名和其他用户信息。chfn
读取缓冲区的输入,不仅包括它需要的内容,还包括最后一行Y
。当adduser
随后要求确认时,该Y
行已被 读取(但被忽略)chfn
,因此adduser
在其输入上看到文件结尾。该变量$answer
应包含一行输入,但由于没有要读取的输入,因此它是未定义的。
既然你在写剧本,在命令行上传递数据(密码除外)。