我正在编写一个shell脚本来自动添加新用户并更新他们的密码.我不知道如何让passwd从shell脚本读取,而不是以交互方式提示我输入新密码.我的代码如下.
adduser $1 passwd $1 $2 $2
我正在编写一个shell脚本来自动添加新用户并更新他们的密码.我不知道如何让passwd从shell脚本读取,而不是以交互方式提示我输入新密码.我的代码如下.
adduser $1 passwd $1 $2 $2
从"man 1 passwd
"开始:
--stdin
This option is used to indicate that passwd should read the new
password from standard input, which can be a pipe.
所以在你的情况下
adduser "$1"
echo "$2" | passwd "$1" --stdin
[Update] comments 中提出了几个问题:
passwd
命令可能没有--stdin
选项:使用chpasswd
如果使用bash以外的shell,"echo"可能不是内置命令,
在这种情况下,您应该使用另一种脚本语言.下面是一个Perl示例:
#!/usr/bin/perl -w
open my $pipe, '|chpasswd' or die "can't open pipe: $!";
print {$pipe} "$username:$password";
close $pipe