LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   User Management Script - error (https://www.linuxquestions.org/questions/linux-newbie-8/user-management-script-error-917116/)

GhostDZ9 12-04-2011 05:00 PM

User Management Script - error
 
Hey guys,

So I am doing a user management script for my linux class and I'm writing a bash script to create users. I have gotten most of it to work so far however I had to put it into a case and switch with a while loop to be able to complete the requirements. I am getting an error with the second case and i don't understand why can someone please help me?

Code:

#!/bin/bash
#Assignment 4 by Rehan Lakhani

#Script interface
#Declaring Variables
leave=no
while [ $leave = no ]
do
clear
cat<<EOF

Welcome to the user management script $USER
1) Create a new user
2) Delete a user
3) Modify a user
4) See last 10 created users
5) Quit
Please enter in a number:
EOF
read selection
case $selection in
        1)
        if [ $selection -eq 1 ]; then
        clear
                if [ $USER == root ]; then
                        read -p "Enter username: " username
                        read -p "Enter home directory: " homeDir
                        read -p "Enter users fullname: " fullName
                        read -p "Enter users shell: " uShell
                        egrep "^$username" /etc/passwd >/dev/null
                        if [ $? -eq 0 ]; then
                                echo "$username Exists!"
                                continue
                        else
                                useradd -c "$fullName" -d "$homeDir" -m $username  -s "$uShell"
                                passwd $username
                                [ $?  -eq 0 ] &&  echo "User has been added to the system!"  || echo "Failed to add user"
                                continue
                        fi
                else
                        echo "Only the root user can add a user to the system"
                fi
        ;;
        2)
        if [ $selection -eq 2 ]; then
        clear
                if [ $USER == root ]; then
                        read -p "Enter username: " username
                        read -p "Do you wish to delete the users home directory [y/n]: " answer
                        if [[ $answer == "y" || $answer == "Y" ]]; then
                                userdel -r $username
                        elif [[ $answer == "n" || $answer == "N" ]]; then
                                userdel $username
                                echo "Users home directory was not removed"
                        fi
                else
                        echo "Only the root user can remove a user from the system!"
                fi
        fi
        ;;
        3)
        elif [ $selection -eq 3 ]; then
                clear
                echo $testText3
        ;;
        4)
        elif [ $selection -eq 4 ]; then
                clear
                echo $testText4
        ;;
        5)
        elif [ $selection -eq 5 ]; then
                clear
                leave=yes
        fi
        *)
esac
done


colucix 12-04-2011 05:13 PM

You missed a fi at line 43, immediately before the closing ;; in case 1). I suggest also to re-read your course notes about the case statement. The question is: are the
Code:

if [ $selection -eq 1 ]; then ...
if [ $selection -eq 2 ]; then ...

statements really necessary?


All times are GMT -5. The time now is 04:18 PM.