Sponsored Content
Top Forums Shell Programming and Scripting Simple script for adding users Post 302743391 by vish6251 on Wednesday 12th of December 2012 05:13:40 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 09:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy