微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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命令所需的密码的两个副本. (该脚本从纯文本文件中读取这些密码.我将假设您的情况是一些特殊情况,而这种情况并不像通常那样危险.)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐