0

我想将新帐户的密码设置为用户输入的输入(字符串)。这似乎不起作用。顺便说一句,这是一个更大的脚本的一部分,我只包含了这个问题的相关部分。

因此,如果我运行代码,我会收到以下错误代码:/create-user.sh: line 36: user3:hej: command not found。这里的行不同,因为它是更大脚本的一部分。上面的“hej”是我在运行代码时输入的密码。我猜shell认为“hej”应该是一个命令,但它应该是一个密码字符串。澄清一下,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"
4

1 回答 1

0

$username:$password应该是chpasswd命令的输入。您正在将输出chpasswd传递给由此形成的命令。由于用户名:密码不是命令的名称,因此您会收到错误消息。

你想要的是:

chpasswd <<< "$username:$password"
于 2021-03-05T21:18:14.233 回答