validate username before using in the script

(cherry picked from commit 145a0add34)
This commit is contained in:
Marvin Roman 2021-11-20 23:58:34 -08:00
parent f4aae8bba8
commit 25dd4469a0
No known key found for this signature in database
GPG Key ID: AE99B800E0ED6CCC
1 changed files with 35 additions and 6 deletions

View File

@ -84,17 +84,46 @@ fi
echo -e "\nDone!\n"
if ! source install.conf; then
read -p "Please enter username:" username
echo "username=$username" >> ${HOME}/$SCRIPTHOME/install.conf
# Loop through user input until the user gives a valid username
while true
do
read -p "Please enter username:" username
# username regex per response here https://unix.stackexchange.com/questions/157426/what-is-the-regex-to-validate-linux-users
# lowercase the username to test regex
if [[ "${username,,}" =~ ^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\$)$ ]]
then
break
fi
echo "Incorrect username."
done
# save the username in lowercase form to install.conf
echo "username=${username,,}" >> ${HOME}/${SCRIPTHOME}/install.conf
fi
if [ $(whoami) = "root" ];
then
useradd -m -G wheel,libvirt -s /bin/bash $username
passwd $username
cp -R /root/$SCRIPTHOME /home/$username/
chown -R $username: /home/$username/$SCRIPTHOME
read -p "Please name your machine:" nameofmachine
echo $nameofmachine > /etc/hostname
cp -R ${HOME}/${SCRIPTHOME} /home/${username}/
chown -R ${username}: /home/${username}/${SCRIPTHOME}
# Loop through user input until the user gives a valid hostname, but allow the user to force save
while true
do
read -p "Please name your machine:" nameofmachine
# hostname regex (!!couldn't find spec for computer name!!)
if [[ "${nameofmachine,,}" =~ ^[a-z][a-z0-9_-\.]{0,62}[a-z0-9]$ ]]
then
break
fi
# if validation fails allow the user to force saving of the hostname
read -p "Username doesn't seem correct. Do you still want to save it? (y/n)" force
if [[ "${force,,}" = "y" ]]
then
break
fi
done
echo ${nameofmachine,,} > /etc/hostname
else
echo "You are already a user proceed with aur installs"
fi