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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-11-2012, 10:40 AM
|
#1
|
|
LQ Newbie
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8
Rep: 
|
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.
|
|
|
|
10-11-2012, 10:59 AM
|
#2
|
|
Member
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 987
Rep:
|
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"
|
|
|
|
10-11-2012, 11:43 AM
|
#3
|
|
LQ Newbie
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8
Original Poster
Rep: 
|
Quote:
Originally Posted by pingu
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.
|
|
|
|
10-11-2012, 11:48 AM
|
#4
|
|
Member
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 987
Rep:
|
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.
|
|
|
|
10-11-2012, 11:58 AM
|
#5
|
|
LQ Newbie
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8
Original Poster
Rep: 
|
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!
|
|
|
|
10-11-2012, 12:43 PM
|
#6
|
|
LQ Newbie
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8
Original Poster
Rep: 
|
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...
|
|
|
|
10-11-2012, 12:55 PM
|
#7
|
|
LQ Newbie
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8
Original Poster
Rep: 
|
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 
|
|
|
|
10-11-2012, 12:55 PM
|
#8
|
|
Member
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 987
Rep:
|
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! :-)
|
|
|
|
10-11-2012, 01:11 PM
|
#9
|
|
LQ Newbie
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8
Original Poster
Rep: 
|
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...
|
|
|
|
10-11-2012, 01:21 PM
|
#10
|
|
LQ Newbie
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8
Original Poster
Rep: 
|
any other suggestions for options that might be useful in this script?
|
|
|
|
10-11-2012, 01:41 PM
|
#11
|
|
LQ Newbie
Registered: Oct 2012
Location: NJ, USA
Distribution: Fedora 17
Posts: 8
Original Poster
Rep: 
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:21 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|