Simple script for adding users


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple script for adding users
# 1  
Old 12-12-2012
Code Simple script for adding users

Hi guys,

I've a simple linux script (made by my friend), which adds users to the system from userlist file. it also creates user home dir and copies certain files to the directory. To be honest, am a newbie in scripting so am unable to fully understand how the script is working. unfortunately, my friend is out of town so i cant get any help from him as of now. Thats why i posted it here. I hope someone from this forum could help me out on it. Code link :

Code:
#!/bin/sh
#set -x

################### Method to Create a User ########################

createUser() {
        for w in `cat $1`;
        do
                export user=$(printf '%s\n' "$w";)
                useradd -d $4$user -m $user -p $(perl -e 'print
crypt("password", "password")') > /dev/null
                if [[ $? -ne 0 ]]; then
                        echo "User $user you are trying to add already exists."
                        echo "Exiting..."
                        exit 1;
                fi
                echo "$user added with home_directory $4$user and
default password: password"
                echo "Copying files..."
                copyFiles $1 $2 $3 $4
        done
        return 0;
}

################### Create User Ends ################################

################## Copying User Files ##############################

copyFiles() {
        cd $4$user
        old=_old
        if [[ $? -eq 0 ]]; then
                echo "file for $4$user already exists !"
                echo "Do you want to overwrite the file (y/n):\c"
                read answer
                if [[ $answer -eq y || $answer -eq Y ]]; then
                        mv $4$user $4$user$old > /dev/null
                        mkdir /$4/$user > /dev/null
                        for i in `cat $2`
                        do
                                cp $3$i $4$user > /dev/null
                        done
                elif [[ $answer -eq n || $answer -eq N ]]; then
                        for i in `cat $2`
                        do
                                cp $3$i $4$user > /dev/null
                        done
                fi
                chmod 700 $4$user /dev/null
        fi
}

################# Copying User Files Ends ###########################
################################### Main ##############################
        clear

        # check command line arguments count
        if [[ $# -lt 4 ]] ; then
                echo "Command line arguments are need to run the script"
                echo "Usage: <Script Name>  path_to_username_list
files_list_path master_copies_path users_home_folder_path"
                echo "Exiting..."
                exit 1;
        fi

        # validate command line arguments
        wd=$(pwd)
        if  [[ $# -ne 0 ]]; then
                ls $1 > /dev/null
                if [[ $? -ne 0 ]]; then
                        echo "Error: Path to username list doesnt
exist, Please provide correct path"
                        echo "Exiting..."
                        exit 1;
                fi
                ls $2 > /dev/null
                if [[ $? -ne 0 ]]; then
                        echo "Error: Path to file listing file names
to be copied doesnot exist"
                        echo "Exiting..."
                        exit 1;
                fi
                ls $3 > /dev/null
                if [[ $? -ne 0 ]]; then
                        echo "Error: Path to the master copies doestnot exist"
                        echo "Exiting..."
                           exit 1;
                fi
        fi
        for i in `cat $2`
        do
                ls /$3/$i > /dev/null
                if [[ $? -ne 0 ]]; then
                        echo "file $i doesnt exist in $3"
                        echo "Exiting..."
                        exit 1;
                fi

        done
        cd $wd > /dev/null

        # Calling user creation method
        createUser $1 $2 $3 $4

############################# Main Ends ###############################
Arguments
1. complete path to the file listing the names of the user, eg:
/home/userlist where userlist is the filename

2. complete path to the file listing the names of the files to be
copied, eg: /root/filelistdir/filelist where filelist is the name of
the file containing filenames.

3. Complete path to the folder where the files are present, eg: /root/filelistdir/

4. Complete path where the users home path needs to be created.

Thanks

Last edited by vish6251; 12-23-2012 at 08:50 PM.. Reason: vish6251: pastebin link added; Scott Pastebin link removed
# 2  
Old 12-12-2012
Try running the script in debug mode. You may get clues as to how it works. This is how I would run the script in debug mode in bash:

Code:
$ bash -x script.sh

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 12-12-2012
thanks

Quote:
Originally Posted by balajesuri
Try running the script in debug mode. You may get clues as to how it works. This is how I would run the script in debug mode in bash:

Code:
$ bash -x script.sh

thanks i will try and see what i can learn from it.
# 4  
Old 12-15-2012
I have understood most if the script now but got a doubt in this section :

Code:
if [[ $answer -eq y || $answer -eq Y ]]; then
                        mv $4$user $4$user$old > /dev/null
                        mkdir /$4/$user > /dev/null
                        for i in `cat $2`
                        do
                                cp $3$i $4$user > /dev/null
                        done
                elif [[ $answer -eq n || $answer -eq N ]]; then
                        for i in `cat $2`
                        do
                                cp $3$i $4$user > /dev/null
                        done
                fi


Here the script reads from the input and if the answer is yes, then it moves old files to a dir $user_old then creates new home dir and copies master files there.

But i dont understand the elseif section, here if the answer is NO, then it should just leave and start loop again for 2nd user in the userlist. but instead it copies the files again. which we dont want to do.

So i guess the loop in elseif section is not needed here and should be removed. right? if not, please clarify why?

many thanks
Vishal
# 5  
Old 12-16-2012
That depends on what the question is. Were it "save old user files before copying (instead of overwrite)", then copying should occur in either case. But then still the elif were too many - put the for loop outside/after the if branch.
# 6  
Old 01-04-2013
Quote:
Originally Posted by RudiC
That depends on what the question is. Were it "save old user files before copying (instead of overwrite)", then copying should occur in either case. But then still the elif were too many - put the for loop outside/after the if branch.
I tested the script multiple times, it copies old files to the user_old dir regardless of the answer chosen by the user i.e. in both cases.
# 7  
Old 01-05-2013
theres one more problem in the script, whenever we run the script it says "file for user already exists", even if the user is being created for the 1st time. I think the problem is here :
Code:
copyFiles() {
        cd $4$user
        old=_old
        if [[ $? -eq 0 ]]; then
                echo "file for $4$user already exists !"
                echo "Do you want to overwrite the file (y/n):\c"
                read answer

here it checks if return code is equal to 0, if yes then shows that error. but how can it get a return code as 0, if the dir is not there? i tried to resolve this error but couldnt do it. would appreciate any help here..

thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script for adding users to file permissions

I need a script to add the following two users ids to the permissions for various files: IIS_WPG and IUSR_CowGirl. I am fairly familiar with scripting but haven't been able to figure out how to do this via a script. Manually doing it is slow. I don't want to create users but only add them to a... (2 Replies)
Discussion started by: Stu Loventhal
2 Replies

2. Windows & DOS: Issues & Discussions

Script for adding users to file permissions

I need a script to add the following two users ids to the permissions for various files: IIS_WPG and IUSR_CowGirl. I am fairly familiar with scripting but haven't been able to figure out how to do this via a script. Manually doing it is slow. I don't want to create users but only add them to a... (2 Replies)
Discussion started by: Stu Loventhal
2 Replies

3. AIX

adding users via smit

I apologize if this is a simple/stupid question. When I add users in smit as root, many(most) of the fields are automatically popluated with some basic default values. Some other admins here have access to create users via sudo, however when they create users (sudo smit users), the user gets... (3 Replies)
Discussion started by: mshilling
3 Replies

4. Shell Programming and Scripting

simple script to mount a folder in all users /home

Go easy on me - first post I need a simple script that will mount a directory in the /home folder of all users. I need to run this on boot and regular intervals as a cron job. I was hoping to achieve this by modifying fstab but it is not possible and I would like to avoid symlinks. I have... (7 Replies)
Discussion started by: barrydocks
7 Replies

5. UNIX for Dummies Questions & Answers

Adding users question

Hello there, I want to add new users to my system, so, being logged in as root I do useradd -m user_name, and the new user is added to the system. The problem is that it has more privileges than I expected. If I do su user_name then I am allowed to do cat /etc/passwd , so it is... (4 Replies)
Discussion started by: help.goes.here
4 Replies

6. Shell Programming and Scripting

Adding delimiter to logged in users

Hi guys! Just was wanting to run a command that would allow me to seperate the currently logged in users. Basically from this format: user1 user2 user3 To: user1|user2|user3 (Note the lack of a pipe at the end, not sure if thats possible) Basically it needs to be in this... (11 Replies)
Discussion started by: crawf
11 Replies

7. Programming

reg adding Users into at.allow and removing from at.allow

Hi , Thanks for your time . I am working on a application , which adds unix user through useradd and deletes user through userdel . both are admin commands . My requirement is i have to add a user into at.allow whenver a unix user is added through my application and the user should be... (4 Replies)
Discussion started by: naren_chella
4 Replies

8. UNIX for Dummies Questions & Answers

Adding users to /etc/group

I'm using SAM to add users on an HP and they're adding fine. But in /etc/group it only lists the group names. It's not adding the users in there. Is there a way to have them put in there without going into SAM and modifying the group and adding them? I guess what I want to happen is when I add... (1 Reply)
Discussion started by: golfhakker
1 Replies

9. Shell Programming and Scripting

Adding a backslash to users' input

Hi, I need to convert user-input from '(this)' to '\(this\)' before passing it to egrep. I've tried using TR, SED and NAWK to add the backslash, but the most I ever get is a backslash without a '(' or ')'. Any ideas? Thanks! (13 Replies)
Discussion started by: netguy
13 Replies

10. Shell Programming and Scripting

Adding users

Anyone have a simple shell script that will prompt and accept screen input for each field that is required in the /etc/passwd file? (3 Replies)
Discussion started by: Relykk
3 Replies
Login or Register to Ask a Question