Here is the fixed code....There were some errors that I'd like to point out. This runs flawlessly now.
Code:
#!/bin/bash
USERFILE="/etc/vsftpd/virtual-users.txt"
DBFILE="/etc/vsftpd/virtual-users.db"
function userdata {
echo $username > $USERFILE
echo $password >> $USERFILE
db4.8_load -T -t hash -f $USERFILE $DBFILE
rm $USERFILE
}
choice=4
echo "1. Create a new user"
echo "2. Change the password of an existing user"
echo "3. Disable an existing user"
echo -n "Choose wisely: "
read choice
if [ $choice -eq 1 ] ; then
echo "Starting FTP virtual user creation."
echo "Please enter the new username"
read username
echo "Please choose a password for the new user"
read -s password
userdata
echo "Creating user homefolder"
mkdir /home/ftp/$username
chown ftp:ftp /home/ftp/$username
echo "User $username with homedirectory /home/ftp/$username created."
elif [ $choice -eq 2 ] ; then
echo "Changing user password"
echo "Please enter the username"
read username
echo "Please enter the new password"
read -s password
userdata
echo "Password of $username changed"
elif [ $choice -eq 3 ] ; then
echo "Disabling FTP user"
echo "Please enter the username"
read username
# echo "Delete existing user folder and contents? y/n"
# read delete
# if [ $delete -eq y ] ; then
# echo "Removing /home/ftp/$USERNAME"
# rm -R /home/ftp/$USERNAME
# else
password=accessnomoreyoucanhaz
userdata
else
echo "Please choose a valid option"
choice=4
fi
Now for an explanation...
Code:
while [ $choice -eq 4 ] ; do
I take it you're a compiled language programmer (C/C++)? This is a common mistake. Read will already pause for input, we don't need to create this loop here. And if we did, we'd have to close it off with a "done".
Code:
if [ $choice -eq 1 ] ; then
echo "Starting FTP virtual user creation."
echo "Please enter the new username"
read username
echo "Please choose a password for the new user"
read -s password
userdata
echo "Creating user homefolder"
mkdir /home/ftp/$username
chown ftp:ftp /home/ftp/$username
echo "User $username with homedirectory /home/ftp/$username created."
else
if [ $choice -eq 2 ] ; then
echo "Changing user password"
echo "Please enter the username"
read username
echo "Please enter the new password"
read -s password
userdata
echo "Password of $username changed"
else
if [ $choice -eq 3 ] ; then
echo "Disabling FTP user"
echo "Please enter the username"
read username
# echo "Delete existing user folder and contents? y/n"
# read delete
# if [ $delete -eq y ] ; then
# echo "Removing /home/ftp/$USERNAME"
# rm -R /home/ftp/$USERNAME
# else
password=accessnomoreyoucanhaz
userdata
else
echo "Please choose a valid option"
choice=4
fi
fi
fi
You have NO IDEA how close you were here...except these else and if statements won't work in eachother. Instead, you need to use the "elif" command (else if) to test the number. So if it's 1, do this. Else if it's 2, do this. Else if it's 3...etc etc. Bash simply doesn't like the way you did it and it got lost. It's very picky like that...
To be honest, I wouldn't even use all those else if statements, I would've used a switch case. It's a lot cleaner and less code.
Hope I helped out!!!