Create script to add user and create directory

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Create script to add user and create directory
# 8  
Old 03-21-2011
groupadd useradd

Couple of thoughts. Once you find that a department doesn't exist and create the directory for, it you may wish to create a group for it with 'groupadd'. Then set the GID to the newly created group.

A similar situation should be done for the user directory although you should create the directory with 'useradd'. 'useradd -g GROUP' will make sure the directory has the correct GID. Use the '-K UMASK=026' to make sure newly created files have the correct permissions. You'll have to look up how UMASK works. In this environment you may prefer 027 for more security.

A better way to handle the departments would be to create a list of valid existing departments. It would reduce spelling errors. The last option in the list could be "(N)ew department".

Code:
#!/bin/ksh
# root check code below
#deptroot=/home
deptroot=/home/wolverines
i=0
dept=()
echo "Department:"
find $deptroot -maxdepth 1 -type d | sort | while read x ;do
    bn=$(basename "$x");
    dept[$i]="$bn"
    i=$(($i + 1))
    echo "$i) $bn"
done
echo ""
echo -n "Enter number of department or 'n' for new department: "
read ans
echo "${dept[*]}"
if [[ "$ans" =~ ^[0-9]+$ ]]; then
    echo "Numeric $ans $i"
    if [ $ans -lt $i ]; then 
       echo "In range"
       d="${dept[$(($ans - 1))]}"
       echo "Department: $d"
    fi
elif [ "$ans" == "n" ]; then
    echo "Prompt for new department"
    # assign d=DEPARTMENT
    # make sure it doesn't exist.. if exists exit
    # groupadd $d
    # mkdir "$depthome/$d"
    # chgrp $d "$depthome/$d"
else
    echo "bad input"
    exit 1
fi

Output Example:
Code:
$ ./test.sh 
Department:
1) History
2) Math
3) PhysEd
4) Science
5) Zoology

Enter number of department or 'n' for new department: 3
History Math PhysEd Science Zoology
Numeric 3 5
In range
Department: PhysEd

Sorry but I had to use KSH as BASH has a bug with pipes and while loops spawning subprocesses. BASH can't see what happened in the loop after the loop.

Lastly I'd add a check for the user ID at the beginning of your script.
Code:
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

Only people with root access should be able to run any of this.
# 9  
Old 03-30-2011
Lastly I'd add a check for the user ID at the beginning of your script.
Code:
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

Only people with root access should be able to run any of this.[/QUOTE]

thanks i actually did this a couple of nights ago before i looked back at this thread.

as far as the departments go. as far as i know this script (unless my instructor takes it and uses it for future classes) is only going to be used by me for this one class. thank you for your help and direction.

now i do have another question though.

i have read on another forum for using a script to check if a user exists or not and this is what i have found:

$ egrep -i "^username" /etc/passwd

this is how i have it in my script:

/bin/egrep -i $UserName /etc/passwd
if [ $? -eq 0 ]
then
echo "User $UserName exists in /etc/passwd!"
else
echo "User $UserName does not exist in /etc/passwd!"
useradd -u 2000 $UserName
fi

my question is how do i get the little up arrow in the ("^username") to appear in vi editor?

or is my statement all wrong?

thanks
# 10  
Old 03-30-2011
Code:
egrep -q "^$username:" /etc/passwd
if [ $? -eq 0 ]; then
    echo "found"
fi

I don't understand your question about the carat character. '^' can be inserted like any other character. In VI you hit 'i' to insert and then <shift 6> on a US keyboard.

I added ':' character to the end of the match to make sure it is an exact match. For instance "^Joe" can match "Joe", "Joe2", or "Joe_38933". "^Joe:" will only match user "Joe".
This User Gave Thanks to m1xram For This Post:
# 11  
Old 03-31-2011
Quote:
Originally Posted by m1xram
Code:
egrep -q "^$username:" /etc/passwd
if [ $? -eq 0 ]; then
    echo "found"
fi

I don't understand your question about the carat character. '^' can be inserted like any other character. In VI you hit 'i' to insert and then <shift 6> on a US keyboard.
Thank you that is what i was looking for (the keyboard shortcut). man have i had a long day it was only after you said <shift 6> that i saw the character there on the keyboard when i looked for it before posting to this site. man i feel so dumb at the moment haha.

Quote:
Originally Posted by m1xram
I added ':' character to the end of the match to make sure it is an exact match. For instance "^Joe" can match "Joe", "Joe2", or "Joe_38933". "^Joe:" will only match user "Joe".
thank you i will do this as you said.

again thank you for your assistance!

---------- Post updated 03-31-11 at 11:43 AM ---------- Previous update was 03-30-11 at 08:54 PM ----------

my next problem is to create a counter for the UID when i add a user this is what i have currently without the counter

/bin/egerp -i "^$UserName:" /etc/passwrd
if [ $? -eq 0 ]
then
echo "User $UserName exists in /etc/passwd!"
else
echo "User $UserName does not exist in /etc/passwd!"
useradd -u 2000 $UserName


now what i am thinking is something along the lines of this:


/bin/egerp -i "^$UserName:" /etc/passwrd
if [ $? -eq 0 ]
then
echo "User $UserName exists in /etc/passwd!"
else
echo "User $UserName does not exist in /etc/passwd!"
userid=2000
while test $userid -le 3000
do
echo $userid
userid="expr $userid+1"
done
useradd -u $userid $UserName

does this look correct?

thank you
# 12  
Old 04-01-2011
'useradd' takes care of all that. It will also add the default directory and files in the new home directory and add the appropriate entries into /etc/passwd and /etc/shadow. No need for a counter. See previous comments for more information.

If you have to use a counter, these constructs will work also.

Code:
i=$userid
i=$(($i + 1))
#OR
let i=$userid
let i=++i;

But, then you'll have to find the highest UID and GID values in /etc/passwd for regular users. That will involve reading /etc/login.defs to get UID_MIN, UID_MAX, GID_MIN, GID_MAX and reading all of /etc/passwd for maximum valid UID and GID values. It's just not worth it. 'useradd' is much easier. To create password run 'passwd $user' after 'useradd'. So that's...

Code:
# if $dept doesn't exist
#   create new group with 'groupadd'
#   gid = dept's GID
#   create dir for dept
#   'chgrp' for dir of dept
# else
#   gid = dept's GID
# fi
useradd -m -d "/home/$dept/$user" -g $gid -K UMASK=026
passwd $user


Last edited by m1xram; 04-01-2011 at 07:40 AM..
# 13  
Old 04-01-2011
i came to the same conclusion about it being a waste of time. thank you

i did have the useradd and the groupadd in my script so i guess that will have to do.

thank you for your help
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. Shell Programming and Scripting

Create a folder under different user directory

Hello All, I have to write a shell script and use it in informatica. The script has to perform below actions: The script gets executed from edw user. Through the script, a DT folder has to be created under edw_sca user. Is this scenario possible through a SHELL script or not. ... (2 Replies)
Discussion started by: bghosh
2 Replies

3. Solaris

Unable to create or delete a directory in /usr with root user

Hi All, I am trying to uninstall jdk 1.5 from my Solaris 10 64 bit but some how was not successful.so tried to delete the folder of jdk from /usr but its throughing error as: Unable to remove directory jdk: Read-only file system Even I tried to create a dir in /usr but its not allowing me... (4 Replies)
Discussion started by: Pshah
4 Replies

4. AIX

How to create new user and add group

Hello, I am new in AIX please tell how can i create user and add group in this user for example, i want to create user umair and want to add this user primanry group DBA and secondary group ORACLE,how can i do this please tell in detail Thanks, Umair (1 Reply)
Discussion started by: umair
1 Replies

5. Solaris

create user with RWX access to a specific directory in Solaris 10

I need to create a user account for a developer that will allow him rwx access to all resources in a directory. How can I do that? Thanks (5 Replies)
Discussion started by: gsander
5 Replies

6. Shell Programming and Scripting

How to create a directory inside root as different user

Hi All, I have directory under /opt/test. The ownership of the test directory is root:root. I have login to the server as test user. I need to have some script to create a directory inside /opt/test. This script will be called as test user. When I try to execute... (4 Replies)
Discussion started by: kalpeer
4 Replies

7. Shell Programming and Scripting

create a new directory from cgi script

hello. i hav accepted name of directory from user through a form.now i need to create a directory under cgi-bin of that name.I am not able to do so.n help is required (12 Replies)
Discussion started by: raksha.s
12 Replies

8. UNIX for Dummies Questions & Answers

how to write script to create directory

Please help. I am the beginner. Don't understand about archive file. How to create a directory for the files from each archive with name of directory which equivalent to the base name of the archive. eg I have file abc.txt. How can I create a directory name abc. Thank you (1 Reply)
Discussion started by: snail
1 Replies

9. Shell Programming and Scripting

Create file in each user directory

Hi, Im newbie, I wanna to create file in each user directory, how to make that script, maybe someone can give me example, im confusing coz i have to change form one user directory to other Thank U. (8 Replies)
Discussion started by: cleks
8 Replies
Login or Register to Ask a Question