I am trying to use a while loop to repeatedly prompt for a username during user registration until a valid username has been provided (i.e. it has NOT already been claimed).
Unfortunately, I've only been coding in bash for less than 70 hours now, so I wasn't familiar with how to do this. My initial thought was to use "goto" like in Windows batch scripts if there was a problem with the username, but apparently that's not a thing in bash and loops seemed to be the recommended route. I wasn't familiar with loops in bash so I did some research until I found a good answer on SO about how to do this. This isn't the exact post (I can't find it now), but I was looking at questions like this one.
The resulting code looks like this:
echo -e "Now, choose a unique username. No special characters, please."
uniqueuser=0 # Assume a valid username has not been chosen from the get-go
while [ "$uniqueuser" == "0" ]
do
read -p "Username: " username
lusername=`echo $username | sed y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/` #lowercase username
if [ "$lusername" == "admin" ] || [ "$lusername" == "administrator" ] || [ "$lusername" == "npstn" ] || [ "$lusername" == "phreak" ] || [ "$lusername" == "guest" ] || [ "$lusername" == "user" ] || [ "$lusername" == "sysop" ]
then
echo -e "That username is reserved. Please pick another username."
else
username=`php /home/com/encode.php "$username"`
available=`curl "https://example.com/redacted/redacted.php?key=$key&type=checkusername&username=$username" --silent`
if [ "$available" == "1" ]
then
uniqueuser=$((1)) # Username is unique and acceptable
else
echo -e "That username is taken. Please choose another one."
fi
fi
done <input.data # so that username variable persists outside of while loop
echo -e "That username is available... or was... now, it's yours!"
echo -e "On this board, we have no annoying password length or complexity requirements. That said, your password cannot be null, nor can it use the plus symbol. Choose wisely!"
When this part of the shell script is reached, I see:
Now, choose a unique username. No special characters, please.
/home/com/redacted.sh: line 4: input.data: No such file or directory
That username is available... or was... now, it's yours!
On this board, we have no annoying password length or complexity requirements. That said, your password cannot be null, nor can it use the plus symbol. Choose wisely!
The reason "input.data" is used is to get the $username variable out from the subshell and back into the main shell. However, that seems to be causing an error. How can I fix this?