hello i'm making a bash script for adding users from a txt fille
i have a basic script that adds users and their password . when you type the users by hand , now i want to upgrade my script with a txt file of users and their password , but i don't know how to start .
#!/bin/bash # Script to add a user to Linux system
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
if [ $? -eq 0 ]; then
echo "User has been added to system!"
else
echo "Failed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi
I want a script that adds users from a txt file. From this file I want to take first name , last name and passwords, I have this basic schript I want to expand, but I'm not sure how to begin , So where I now manually enter the data, I would now like to get this from the text file
root@nio:/tmp# cat adduser.sh
#!/bin/bash
if [ $# -lt 1 ]
then
echo "Usage : $0 userfile"
exit
fi
if [ $(id -u) -eq 0 ]; then
while IFS=, read username password; do
if egrep "^$username" /etc/passwd >/dev/null; then
echo "$username exists!"
else
password=$(perl -e 'print crypt($ARGV[0], "password")' $password)
if useradd -m -p "$password" "$username" >/dev/null; then
echo "User '$username' has been added to system!"
else
echo "Failed to add user '$username'!"
fi
fi
done <"$@"
else
echo "Only root may add a user to the system"
exit
fi
Usage
root@nio:/tmp# sh adduser.sh file
User 'bart_simpson' has been added to system!
User 'maggy_simpson' has been added to system!
Thanks for the quick help, I just do not understand where the code is
where the script searches in the text file after the names
, i see you named your txt file , file
password= Assignment to a shell variable $() call to a subprocess perl -e call the perl binary to execute code from command line, instead of file source code 'print crypt($ARGV[0], "password")' display the result of calling the perl function crypt crypt($ARGV[0], "password") crypt will return a digest of whatever was given as argument. $ARGV[0] is the first argument from the command line, in this case the last $password term. "password" is what's called SALT string. It can be anything. Not to confuse with $password $password Argument given at the command line, in the form of perl variable, to crypt (presumably a string that represents a clear text password)
Best way of learn is to try:
Change password and random_text to anything you want
Another solution is to use openssl to generate encrypted passwords (it automatically chooses salt values for you, but you could duplicate what the perl code was doing by usint the -salt pa option
password=$(openssl passwd "$password")
password=$(openssl passwd -salt pa "$password")
Notice how the $password variable is quoted above, this is to protect against white space in the password.