LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-11-2012, 10:40 AM   #1
Omega Ra
LQ Newbie
 
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8

Rep: Reputation: Disabled
Shell Script Questions


Hey guys, I am new here and to Linux in General, but I am picking it up fairly well. For my class I am making a script to add users/groups to the system and manage them. I have it mostly working, but there are a couple of weird little issues (I am running this in Fedora 17 BTW). Here is my code
Code:
#!/bin/bash
# This Script is intended to make User and Group Creation and Management simple
#author: Shaun Sommer
#date created: 10/4/2012

#This will check to see if the user is root, if not it will exit.
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi
mkdir /home/users
mkdir /home/admins

function main_menu ()

{
#This Brings up the Menu Selection
while :
do
  clear
  echo "___________________________"
  echo "        Main Menu"
  echo "___________________________"
  echo "[1] Create User"
  echo "[2] Create Admin"
  echo "[3] Modify User"
  echo "[4] Create Group"
  echo "[5] Delete User"
  echo "[6] Exit"
  echo "==========================="
  echo -n "Enter your menu choice [1-6]:"
  read yourch #reads your choice and runs the selection from the case below.
  case $yourch in
 #case 1 creates a user with a password and adds them to the group Users,
#Users group must be created first
    1) read -p "Enter username : " username #Prompts you for Username, Password, and Comments
       read -p "Enter password : " password
       read -p "Enter comments: " GECOS
       egrep "^$username" /etc/passwd >/dev/null #Checks to see if username already exists
       if [ $? -eq 0 ]; then #if Username already exists display error
         echo "$username already exists!"
	 sleep 1	 #pause 1 second
	 main_menu      #return to main menu
       else
         useradd -p $password $username -d /home/users/$username #create user and home directory
	 usermod -c $GECOS $username  #add comments
	 usermod -g Users $username #add to group Users
	 [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
       fi	
       echo "Press a Key..." ; read ;;
#case 2 creates a user with a password and adds them to the group Admins,
#Admins group must be created first
    2) read -p "Enter username : " username  #Prompts you for Username, Password, and Comments
       read -s -p "Enter password : " password 
       read -p "Enter comments : " GECOS
       egrep "^$username" /etc/passwd >/dev/null #Checks to see if username already exists
       if [ $? -eq 0 ]; then #if Username already exists display error
         echo "$username already exists!"
	 sleep 1 #pause 1 second
         main_menu #return to main menu
       else 
         useradd -p $password $username -d /home/admins/$username #create user and home directory
         usermod -c $GECOS $username #add comments
         usermod -g Admins $username  #add to group Admins
         [ $? -eq 0 ] && echo "Admin has been added to system!" || echo "Failed to add an Admin!"
       fi
       echo "Press a Key..." ; read ;;
    3) sub_menu ;; #brings up sub-menu for user modify

    4) read -p "Enter name of Group : " group #prompts for group name
       egrep "^$group" /etc/group >/dev/null #checks to see if group name exists
       if [ $? -eq 0 ]; then #if group already exists display error
         echo "$group already exists!"
         sleep 1 #pause 1 second
	 main_menu #return to main menu
       else
         groupadd $group #add group
         [ $? -eq 0 ] && echo "Group has been added to system!" || echo "Failed to add Group!"
       fi
       echo "Press a Key..." ; read ;;
    5) read -p "Enter User to Delete : " username #prompt for username
	userdel -r  $username #delete user
	echo "User Deleted!"
	echo "Press a Key..." ; read ;;
    6) exit 0 ;;
    *) echo "Opps!!! Please select choice 1,2,3,4,5, or 6" ; #error message if input something outside options
       echo "Press a key..." ; read ;;
  esac
done
}

function sub_menu
#brings up the sub-menu for User modify
{
 while :
      do
       clear
       echo "___________________________"
       echo "        User Modify"
       echo "___________________________"
       echo "[1] Change Password"
       echo "[2] Unlock User"
       echo "[3] Lock User"
       echo "[4] Change GECOS"	
       echo "[5] Return to Main Menu"
       echo "==========================="
       echo -n "Enter your menu choice [1-5]:"
       read yourch #reads your choice and runs the selection from the case below.
        case $yourch in
         1) read -p "Enter Username : " username
	    read -s -p "Enter New Password : " password
 	    	usermod -p $password $username 
 		echo "Password Changed!" 
		echo "Press a key..." ; read ;;
         2) read -p "Enter User to Unlock : " username
	        usermod -U $username 
		echo "User Unlocked!" ;
		echo "Press a key..." ; read ;;
         3) read -p "Enter User to Lock : " username
                usermod -L $username 
		echo "User Locked!" ;
		echo "Press a key..." ; read ;;
	 4) read -p "Enter Username : " username
	    read -p "Enter Comments : " GECOS
		usermod -c $GECOS $username
		echo "Comments Changed!" ;
		echo "Press a key..." ; read ;;
         5) main_menu ;;
         *) echo "Opps!!! Please select choice 1,2,3,4, or 5" ;
            echo "Press a key..." ; read ;;
        esac
       done
}

main_menu

exit 0
Now here are the issues.
1) When adding the GECOS info to a user there are two little issues.
a)the prompt ends up on the same line as the password prompt instead of a new line (I tried removing the -s option on read but haven't tested it yet)
b)it only allows me to put one word for GECOS instead of a full line of text (I assume the problem is in my read statement, but can't figure out why)

2)When trying to add a User to a group (be it Users of Admins) it doesn't add them and instead makes a group with the Users name...I have checked /etc/group after to verify and the users are not added to the appropriate group.

Other than that, I think it is working as expected. Any help to figure this out would be most appreciated.
 
Old 10-11-2012, 10:59 AM   #2
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,295

Rep: Reputation: 123Reputation: 123
1) Add a line "echo "" after the "read ..."
2) Can't say why a group is created - are you really sure about this?
But the group name is always all letters lower case ' "users" not "Users"
 
Old 10-11-2012, 11:43 AM   #3
Omega Ra
LQ Newbie
 
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pingu View Post
1) Add a line "echo "" after the "read ..."
2) Can't say why a group is created - are you really sure about this?
But the group name is always all letters lower case ' "users" not "Users"
okay so I assume the echo is for 1a, getting the prompt on the next line, any idea about 1b? getting more than a single word for the GECOS variable?

as for 2, maybe that is why...I will try it with all lowercase.
 
Old 10-11-2012, 11:48 AM   #4
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,295

Rep: Reputation: 123Reputation: 123
I think - not in a position to check thorougly right now - that GECOS will be an array not a variable.
You can then use something like "$GECOS[1]" to catch a word.
Easier is to use double quotes when entering the text for GECOS.
... and you'll have to check all of this, I'm taking it from memory only! But I do believe it will put you on the right track.
 
Old 10-11-2012, 11:58 AM   #5
Omega Ra
LQ Newbie
 
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8

Original Poster
Rep: Reputation: Disabled
Ugh, I hate arrays. I have never done them in Shell, only VBScript. So I will have to look into that and see what I find, will let you know thanks!
 
Old 10-11-2012, 12:43 PM   #6
Omega Ra
LQ Newbie
 
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8

Original Poster
Rep: Reputation: Disabled
Well so far putting in the "echo" worked, but user still not being added to group. Plus, putting the variable $GECOS in "" when calling it worked so all I have to do is figure out the group thing...
 
Old 10-11-2012, 12:55 PM   #7
Omega Ra
LQ Newbie
 
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8

Original Poster
Rep: Reputation: Disabled
And I figured it out on the useradd command I added the -N option to not make group with username and the usermod -g I changed to usermod -G and it works
 
Old 10-11-2012, 12:55 PM   #8
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,295

Rep: Reputation: 123Reputation: 123
Check the man-pages for usermod, the syntax differs between distro's - or bash versions maybe.
All I know is that Debian Lenny and OpenSuse 12 are slightly different here.
It could be that you need like "usermod -Ga ..:" - well, that's just one guess.

---------- Post added 11th Oct 2012 at 19:56 ----------

You beat me with ten seconds! :-)
 
Old 10-11-2012, 01:11 PM   #9
Omega Ra
LQ Newbie
 
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8

Original Poster
Rep: Reputation: Disabled
LOL the -g is supposed to work too, not sure why it isn't the -G allows you to add multiple groups to a user in precedent order...
 
Old 10-11-2012, 01:21 PM   #10
Omega Ra
LQ Newbie
 
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8

Original Poster
Rep: Reputation: Disabled
any other suggestions for options that might be useful in this script?
 
Old 10-11-2012, 01:41 PM   #11
Omega Ra
LQ Newbie
 
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8

Original Poster
Rep: Reputation: Disabled
here is the fixed code if anyone wants it.

Code:
#!/bin/bash
# This Script is intended to make User and Group Creation and Management simple
#author: Shaun Sommer
#date created: 10/4/2012

#This will check to see if the user is root, if not it will exit.
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi
mkdir /home/users
mkdir /home/admins
groupadd admins

function main_menu ()

{
#This Brings up the Menu Selection
while :
do
  clear
  echo "___________________________"
  echo "        Main Menu"
  echo "___________________________"
  echo "[1] Create User"
  echo "[2] Create Admin"
  echo "[3] Modify User"
  echo "[4] Create Group"
  echo "[5] Delete User"
  echo "[6] Exit"
  echo "==========================="
  echo -n "Enter your menu choice [1-6]:"
  read yourch #reads your choice and runs the selection from the case below.
  case $yourch in
 #case 1 creates a user with a password and adds them to the group Users,
#Users group must be created first
    1) read -p "Enter username : " username #Prompts you for Username, Password, and Comments
       read -s -p "Enter password : " password
       echo
       read -p "Enter comments: " GECOS
       egrep "^$username" /etc/passwd >/dev/null #Checks to see if username already exists
       if [ $? -eq 0 ]; then #if Username already exists display error
         echo "$username already exists!"
	 sleep 1	 #pause 1 second
	 main_menu      #return to main menu
       else
         useradd -N -p $password $username -d /home/users/$username #create user and home directory
	 usermod -c "$GECOS" $username  #add comments
	 usermod -G users $username #add to group users
	 [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
       fi	
       echo "Press a Key..." ; read ;;
#case 2 creates a user with a password and adds them to the group Admins,
#Admins group must be created first
    2) read -p "Enter username : " username  #Prompts you for Username, Password, and Comments
       read -s -p "Enter password : " password 
       echo
       read -p "Enter comments : " GECOS
       egrep "^$username" /etc/passwd >/dev/null #Checks to see if username already exists
       if [ $? -eq 0 ]; then #if Username already exists display error
         echo "$username already exists!"
	 sleep 1 #pause 1 second
         main_menu #return to main menu
       else 
         useradd -N -p $password $username -d /home/admins/$username #create user and home directory
         usermod -c "$GECOS" $username #add comments
         usermod -G admins $username  #add to group admins
         [ $? -eq 0 ] && echo "Admin has been added to system!" || echo "Failed to add an Admin!"
       fi
       echo "Press a Key..." ; read ;;
    3) sub_menu ;; #brings up sub-menu for user modify

    4) read -p "Enter name of Group : " group #prompts for group name
       egrep "^$group" /etc/group >/dev/null #checks to see if group name exists
       if [ $? -eq 0 ]; then #if group already exists display error
         echo "$group already exists!"
         sleep 1 #pause 1 second
	 main_menu #return to main menu
       else
         groupadd $group #add group
         [ $? -eq 0 ] && echo "Group has been added to system!" || echo "Failed to add Group!"
       fi
       echo "Press a Key..." ; read ;;
    5) read -p "Enter User to Delete : " username #prompt for username
	userdel -r  $username #delete user
	echo "User Deleted!"
	echo "Press a Key..." ; read ;;
    6) exit 0 ;;
    *) echo "Opps!!! Please select choice 1,2,3,4,5, or 6" ; #error message if input something outside options
       echo "Press a key..." ; read ;;
  esac
done
}

function sub_menu
#brings up the sub-menu for User modify
{
 while :
      do
       clear
       echo "___________________________"
       echo "        User Modify"
       echo "___________________________"
       echo "[1] Change Password"
       echo "[2] Unlock User"
       echo "[3] Lock User"
       echo "[4] Change GECOS"	
       echo "[5] Return to Main Menu"
       echo "[6] Exit"
       echo "==========================="
       echo -n "Enter your menu choice [1-6]:"
       read yourch #reads your choice and runs the selection from the case below.
        case $yourch in
         1) read -p "Enter Username : " username
	    read -s -p "Enter New Password : " password
 	    	usermod -p $password $username 
 		echo "Password Changed!" 
		echo "Press a key..." ; read ;;
         2) read -p "Enter User to Unlock : " username
	        usermod -U $username 
		echo "User Unlocked!" ;
		echo "Press a key..." ; read ;;
         3) read -p "Enter User to Lock : " username
                usermod -L $username 
		echo "User Locked!" ;
		echo "Press a key..." ; read ;;
	 4) read -p "Enter Username : " username
	    read -p "Enter Comments : " GECOS
		usermod -c "$GECOS" $username
		echo "Comments Changed!" ;
		echo "Press a key..." ; read ;;
         5) main_menu ;;
	 6) exit 0 ;;
         *) echo "Opps!!! Please select choice 1,2,3,4, or 5" ;
            echo "Press a key..." ; read ;;
        esac
       done
}

main_menu

exit 0
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
which is the best forum for asking shell script questions emanresu LQ Suggestions & Feedback 2 04-11-2012 03:08 PM
three shell script questions qrshat Programming 4 02-27-2006 08:40 AM
Couple of Shell Script Questions mathias1979 Programming 3 06-16-2005 03:22 PM
two shell script questions tutwabee Linux - General 4 08-10-2004 06:03 PM
Shell Script Questions Col Panic Linux - General 13 10-12-2003 12:51 PM


All times are GMT -5. The time now is 12:27 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration