?
Solved

help with shell scripting

Posted on 2003-02-25
10
Medium Priority
?
283 Views
Last Modified: 2010-04-20
hi can anyone help me with shell scripting
i need to make a script that has an initial menu like below

1.Add Users
2.Delete Users
3.Modify Users
4.Exit

1.Add Users
when add users is selected the script should request a filename containing the user details , this file is going to have three fields the first name, fullname and group
for example it will add the user john , full name john smith , group1 and a passwd should be assigned also which will be listed in a text file with each successful user added., so basically the add user function needs to read a file, add each user from the file to the group and  specify a password all of which will be displayed in a usersadded textfile

2.the delete user function need to request a groupnumber and then search through the etc/password file and delete all users belonging to that group

3.Modify User Groups
the function should ask for a username , and then allow the user to enter the old user group and then enter the new user group

4.Exit
(very straight forward i can do this one)

if anyone can help me with all of this or seperate parts it would be greatly appreciated :) :)
0
Comment
Question by:mrkjudd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 3

Expert Comment

by:cdillon
ID: 8022527
What distribution of linux (including version number) are you using?
0
 

Expert Comment

by:qwyjebo
ID: 8023094
For deletion of users belonging to a particular group:

echo -n "Enter the group ID to be deleted: "
read GID
echo $GID | awk 'BEGIN {FS=":"} {
                       GID = $1
                       while (getline < "/etc/passwd" != 0)
                               if ($4 == GID) {
                                       CMD = "userdel -r "$1
                                       system(CMD)
                               }
            }'

This will delete entries from /etc/passwd, /etc/shadow, remove their home directory & also remove their mail spools.
0
 

Author Comment

by:mrkjudd
ID: 8023347
im using redhat 8.0
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

Author Comment

by:mrkjudd
ID: 8023373
that should be fine for deleting users i think :)
0
 
LVL 9

Accepted Solution

by:
majorwoo earned 1600 total points
ID: 8026768
#!/bin/bash

menu()
{
  echo "1. Add Users"
  echo "2. Delete Users"
  echo "3. Modify Users"
  echo "4. Exit"
  echo -n "Choice : "
 
  read CHOICE

  case "$CHOICE" in
    "1")
    addusers
    ;;

    "2")
    delusers
    ;;

    "3")
    modusers
    ;;

    "4")
    exit 0
    ;;

    *)
    echo "Invalid selection."
    exit 1
  esac

}

addusers()
{
  echo -n "Enter input filename : "
  read INFILE

  if [ ! -f $INFILE ]; then
    echo "Unable to stat $INFILE"
    exit 2
  fi

  while read LINE
  do
    UNAME=`echo $LINE | cut -d ',' -f1`
    FULLNAME=`echo $LINE | cut -d ',' -f2`
    MYGROUP=`echo $LINE | cut -d ',' -f3`
    useradd -c "$FULLNAME" -g $MYGROUP $UNAME
    #ask for passwd
    #echo "Set password for $UNAME"
    #passwd $UNAME
  done < $INFILE
}

delusers()
{
  echo -n "Enter the group ID to be deleted: "
  read myGID
 
  NAMES=`cat /etc/passwd | grep $myGID | cut -d ':' -f1`

  for NAME in $NAMES; do
    userdel -r $NAME
  done
}

modusers()
{
  #this will change the users default group, if you want to change
  #their optional other groups it needs to be modified

  #get the username
  echo -n "Enter the username : "
  read UNAME

  #show current group
  CURRGRP=`cat /etc/passwd | cut -d ':' -f1-4 | grep $UNAME | cut -d ':' -f4`
  CURRGRPNAME=`cat /etc/group | grep $CURRGRP | cut -d ':' -f1`
  echo "User is a member of group: $CURRGRPNAME"

  #get the newgroup
  echo -n "Enter the newgroup : "
  read NEWGRP

  #modify the user info
  usermod -g $NEWGRP $UNAME
}


#MAIN SCRIPT HERE

menu

exit 0




there you go...
I would not specify password in the file, but if you wanted  to you could write a c program to use crypt to generate those passwords and add them to the files..

Its much safer to manually set them:
passwd $USERNAME

you can uncomment the part to ask you to do it while you add the user (even if you just set it to changeme)
0
 
LVL 6

Expert Comment

by:mbarbos
ID: 8029162
Just a couple of ideeas:

1. You cannot use majorwoo's scripts, since greping that way will not ensure that the number involved is the GID. It can be any substring that matches.
You should use qwyjebo's script. That means awk. Awk will also help you fomat nicely the files.

2. Maybe you can have a look at dialog. It is a small utility to display dialog boxes in text mode, in shell scripts, and probably it would make your script look nicer. As I remember, dialog is available for RH (but I'm not sure)
0
 
LVL 2

Expert Comment

by:jimbb
ID: 8035441
(Yes, RHL8.0 does have dialog and gdialog.)
0
 

Author Comment

by:mrkjudd
ID: 8148835
can anyone help with adding users im having some problems
0
 
LVL 9

Expert Comment

by:majorwoo
ID: 8149309
ok, so back here - what problems are you having?
0
 

Author Comment

by:mrkjudd
ID: 8200900
thanks for the help
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question