linux – 编写脚本以使用预定义的密码创建多个用户

所以我想创建一个脚本,用于运行users.txt中的用户

useradd -m -s /bin/false users_in_the_users.txt

并从passwords.txt填写密码两次(以确认密码)

这是脚本

#!/bin/bash

# Assign file descriptors to users and passwords files
exec 3< users.txt
exec 4< passwords.txt
exec 5< passwords.txt

# Read user and password
while read iuser <&3 && read ipasswd <&4 ; do
  # Just print this for debugging
  printf "\tCreating user: %s with password: %s\n" $iuser $ipasswd
  # Create the user with adduser (you can add whichever option you like)
  useradd -m -s /bin/false $iuser
  # Assign the password to the user, passwd must read it from stdin
  passwd $iuser
done

问题是,它没有填写密码.还有一件事,我希望脚本能够填写两次密码.

有什么建议?

解决方法:

您必须在stdin上提供密码.更换:

passwd $iuser

有:

passwd "$iuser" <<<"$ipasswd
$ipasswd"

或者,如mklement0所示:

passwd "$iuser" <<<"$ipasswd"$'\n'"$ipasswd"

咒语<<<创建一个here-string. <<<<<<<<<作为标准提供给<<<<<<<<在这种情况下,我们提供passwd命令所需的密码的两个副本. (该脚本从纯文本文件中读取这些密码.我将假设您的情况是一些特殊情况,而这种情况并不像通常那样危险.)

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...