[英]SHELL SCRIPT LINUX set password using user input
I would like to set the password for the new account as the input (string) that the user entered.我想将新帐户的密码设置为用户输入的输入(字符串)。 This does not seem to work.这似乎不起作用。 This is part of a larger script by the way, I only included the relevant parts to this issue.顺便说一句,这是一个更大的脚本的一部分,我只包含了这个问题的相关部分。
So if I run the code I get the following error code: /create-user.sh: line 36: user3:hej: command not found.The line here is different as it is part of a larger script.因此,如果我运行代码,我会收到以下错误代码:/create-user.sh: line 36: user3:hej: command not found。这里的行不同,因为它是更大脚本的一部分。 the "hej" above is what I entered as the input to password when I ran the code.上面的“hej”是我在运行代码时输入的密码。 I guess shell thinks "hej" is supposed to be a command, but it should be a string for the password.我猜 shell 认为“hej”应该是一个命令,但它应该是一个密码字符串。 To clarify, hej should be the password.澄清一下,hej 应该是密码。
#!/bin/bash
# Get the username (login).
read -p "Please enter a username: " username
# Get the password.
read -p "Please enter an initial password: " password
# Create the account.
useradd "$username"
# Set the password.
chpasswd | "$username":"$password"
$username:$password
should be the input to the chpasswd
command. $username:$password
应该是chpasswd
命令的输入。 You're piping the output of chpasswd
to a command formed from that.您正在将chpasswd
的 output 传送到由此形成的命令。 Since the username:password isn't the name of a command, you get an error.由于用户名:密码不是命令的名称,因此您会收到错误消息。
What you want is:你想要的是:
chpasswd <<< "$username:$password"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.