4

I am using Debian. I am learning Bash scripting. I am creating a script that creates new user and sets password the problem is I get passwd: unrecognized option '--stdin' error

That is my script:

#!/bin/bash
read -p "Please Enter Your Real Name: " REAL_NAME 
read -p "Please Enter Your User Name: " USER_NAME 
useradd -c "${COMMENT}" -m ${USER_NAME} 
read -p "Please Enter Your Password: " PASSWORD
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
passwd -e ${USER_NAME}
Cyrus
  • 84,225
  • 14
  • 89
  • 153
hosam.shafik
  • 119
  • 2
  • 6

2 Answers2

4

As tested on Debian 10 @ Docker image

echo -e "badpass\nbadpass" | passwd urname

This will work in /bin/bash, not in /bin/sh


echo "urname:badpass" | chpasswd

This will work in /bin/sh and /bin/bash

eby mohan
  • 41
  • 2
3

There is no --stdin option, and you need to protect your variable with quotes.

This is a working version:

#!/bin/bash
read -p "Please Enter Your Real Name: " REAL_NAME 
read -p "Please Enter Your User Name: " USER_NAME 
useradd -c "${COMMENT}" -m ${USER_NAME} 
read -p "Please Enter Your Password: " PASSWORD
echo -e "$PASSWORD\n$PASSWORD" |passwd "$USER_NAME"
passwd -e ${USER_NAME}
Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44