0

Hey Iam trying to create new users from textfile and set passwwords for them. I wrote this script

 #! /bin/bash
userfile=/home/kancelaria-2p/Desktop/uzivatelia.txt 
number=1234
username=$(cat $userfile | tr '[:upper:]'  '[:lower:]')
password="$username$number"
for user in $username
do      
         sudo useradd $$user
         echo "$user:$password" | sudo chpasswd
done

Unfortunately all I get is theese errors

chpasswd: line 2: missing new password
chpasswd: line 3: missing new password
chpasswd: line 4: missing new password
chpasswd: line 5: missing new password
chpasswd: line 6: missing new password
chpasswd: line 7: missing new password
chpasswd: line 8: missing new password
chpasswd: line 9: missing new password
chpasswd: line 10: missing new password

Can you help me fix it?

marek
  • 15
  • 7
  • Paste your code in https://www.shellcheck.net/, you will see you have some syntax issues. Fix that first. – Nic3500 Apr 11 '22 at 00:50
  • @Nic3500 already did and edited the code, can you help me now? – marek Apr 11 '22 at 08:39
  • Some platforms offer a higher-level tool `adduser` which allows you to create a new user and set their password in one step. Ubuntu is among them. – tripleee Apr 11 '22 at 09:27
  • Is `$password` set to something? Add an `echo $password` inside the loop to ensure some value is properly set. Or put `#!/bin/bash -x` to see debug messages. – Nic3500 Apr 12 '22 at 23:34

0 Answers0