HELP for my script

guys need help how to add user to my script.
note: this is not a home work... I'm just self studying unix/linux programming. thanks in advance.
and how can i add this script to the add user list below. dont know how to combine case and ifelse.

if [ $(id -u) -eq 0 ]; 
 then 	read -p "Enter username : " username 	
            read -s -p "Enter password : " password 	
            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
 else 	
           echo "Only root may add a user to the system" 	
           exit 2 
 fi 

-------------------------------------------------------------
my exercise:

#!/bin/bash
clear
PS3="Enter your choice> "
select i in "Current Logins" "Last Logins" "Generate List of Users" "Create User Accounts" "Exit"
do


case $i in
        "Current Logins") who;;
        "Last Logins") lastlog;;
        "Generate List of Users") awk -F':' '{ print $1}' /etc/passwd;;
        "Create User Accounts") useradd;;
        Exit) exit;;
        
*) echo "Invalid choice";;
esac         
done

You could just add the code there, but a more structured way would be if you turn you add_user part in to a function and call that in the case statement:

add_user() {
  ....
}

and later on

"Create User Accounts") add_user;;

I used a slightly different name, since on many systems useradd is an actual command.

1 Like

i tried using useradd but result is error

#!/bin/bash clear PS3="Enter your choice> " select i in "Current Logins" "Last Logins" "Generate List of Users" "Create User Accounts" "Exit" do   case $i in         "Current Logins") who;;         "Last Logins") lastlog;;         "Generate List of Users") awk -F':' '{ print $1}' /etc/passwd;;         "Create User Accounts") useradd;;  <------------ (here)         Exit) exit;;          *) echo "Invalid choice";; esac          done

where should i put

add_user() {   .... }

sorry im new to scripting...

Your return key appears to malfunction :wink:

You should first define you own add_user function:

add_user() {
  ....
}

And then you can call that inside your case statement:

"Create User Accounts") add_user;;
1 Like

Sir what i did is like this and the result was segmentation fault..

#!/bin/bash

clear
add_user() {
  add_user;
}
PS3="Enter your choice> "
select i in "Current Logins" "Last Logins" "Generate List of Users" "Create User Accounts" "Exit"
do


case $i in
        "Current Logins") who;;
        "Last Logins") lastlog;;
        "Generate List of Users") awk -F':' '{ print $1}' /etc/passwd;;
        "Create User Accounts") add_user;;
        Exit) exit;;
        
*) echo "Invalid choice";;
esac         
done

You need to mention user creation steps in the below function

add_user() {
## User creation steps / whatever you think are relevant
}
1 Like

Hi Sir sorry i really don't know what to do since I'm a beginner..

can u give a sample?

or can i add if else for that particular part?
like the script from the top?

is it possible to apply for the add user area only? without changing the whole?

Can you try this i am not root user to test this on my environment

#!/bin/bash

clear
add_user() {
PS3="Enter your choice> "
select i in "Current Logins" "Last Logins" "Generate List of Users" "Create User Accounts" "Exit"
do


case $i in
"Current Logins") who;;
"Last Logins") lastlog;;
"Generate List of Users") awk -F':' '{ print $1}' /etc/passwd;;
"Create User Accounts") add_user;;
Exit) exit;;

*) echo "Invalid choice";;
esac 
done
}
1 Like
#!/bin/bash
clear
add_user() {
## write your statements here
echo "Created user"
}
PS3="Enter your choice> "
select i in "Current Logins" "Last Logins" "Generate List of Users" "Create User Accounts" "Exit"
do

case $i in
"Current Logins") who;;
"Last Logins") lastlog;;
"Generate List of Users") awk -F':' '{ print $1}' /etc/passwd;;
"Create User Accounts") add_user;;
Exit) exit;;
*) echo "Invalid choice";;
esac 
done
1 Like

the whole script didn't run sir

#!/bin/bash
clear
add_user() {
PS3="Enter your choice> "
DT=$(date)
   echo "******************* $DT ***********************" 

select i in "Current Logins" "Last Logins" "Generate List of Users" "Create User Accounts" "Exit"

do
case $i in
        "Current Logins") who | wc -l;;
        "Last Logins") lastlog;;
        "Generate List of Users") awk -F':' '{ print $1}' /etc/passwd;;
        "Create User Accounts") add_user ;;
        Exit) exit;;

*) echo "Invalid choice";;
esac
done
}

---------- Post updated at 02:13 AM ---------- Previous update was at 02:00 AM ----------

im a noob... :frowning:

#!/bin/bash
clear
add_user() {
useradd -m -s /bin/bash     <------------------ this didn't work
echo "Created user"
}
PS3="Enter your choice> "
select i in "Current Logins" "Last Logins" "Generate List of Users" "Create User Accounts" "Exit"
do

case $i in
"Current Logins") who;;
"Last Logins") lastlog;;
"Generate List of Users") awk -F':' '{ print $1}' /etc/passwd;;
"Create User Accounts") add_user;;
Exit) exit;;
*) echo "Invalid choice";;
esac 
done

In post #1 you had two pieces of code, the first for creating users, the second for a menu. Your question was how you could integrate the two. My suggestion was to put the part for creating users into a function and then call that function in the second (menu) part...