i am currently trying to create a script which presents any particular person with a menu when running this particular bash script. On the menu i have allowed three options (1. Add user, 2. Delete user and 3.Quit). In relation to the add user option once selected there should be 2 options which allow the user to manually input details for a new user or add multiple users via a text file. I have been able to create the menus/options described above although i have been unable to create the script/body of the tasks for the add user manually and many users via txt file aswell as delete user.
Any help with my code would be much appreciated! thanks a lot!
Here is my script as of now :
#!/bin/bash
clear
echo "########## MENU ############\n"
options=("add_user" "delete_user" "exit")
select opt in "${options[@]}"
do
case $opt in
"add_user")
clear
while [ 1 ]
do
clear
echo "1. Add user manually"
echo "2. Add user via TXT file"
read -p "Enter your choice" ch
case $ch in
1)
read -p "Enter user name : " useradd
read -p "Enter user password:" passwd
echo -e "Successfully added the user"
;;
2)
if [ $(id -u) -eq 0 ]; then
for row in `more $1`
do
username=${row%:*}
password=${row#*:}
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
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a
user!"
fi
done
else
echo "Only root may add a user to the system"
exit 2
fi
esac
done
;;
"delete_user")
read -p "Enter a User name to delete "UNAME
passwd
deluser $UNAME
echo "User $UNAME has been deleted"
;;
"exit")
break
;;
esac
done
select
s (for the add user), 3.) set PS3 for the nicer select's prompt, 4.) yourclear
clearnig your error messages, so you should either don't use clear or pause you script after the error messages 5) why you usingmore
instead ofcat
or (better) an simple redirection to while-read loop? 6) finally, the most important - break your script into bash-functions for better manageability. – jm666 May 1 at 8:03