Simple script for adding users


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple script for adding users
# 8  
Old 01-06-2013
Pls post execution log (set -vx) with err msg.
On first sight, you may try double quotes if [[ "$?" -eq "0" ]]. Secondly, you may want to save $? to a variable and echo it for reference.
AND, on third sight, you are testing $? from the assignment old=_old which succeeded and thus $? is 0. So - go with the second proposal, use a variable.

Last edited by RudiC; 01-06-2013 at 04:47 AM.. Reason: Third sight
# 9  
Old 01-06-2013
error_log

Quote:
Originally Posted by RudiC
Pls post execution log (set -vx) with err msg.
On first sight, you may try double quotes if [[ "$?" -eq "0" ]]. Secondly, you may want to save $? to a variable and echo it for reference.
AND, on third sight, you are testing $? from the assignment old=_old which succeeded and thus $? is 0. So - go with the second proposal, use a variable.
Heres the error log :

Image

i tried putting double quotes but no luck.

PS: neha is first username in userlist.

---------- Post updated at 09:25 PM ---------- Previous update was at 09:09 PM ----------

i think the return code is 0 because we're creating the home dir in the createUser() function. here :

Code:
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

here we're using "useradd -m" to create a home dir for the given user. which in turn gives the return code as 0.
did i get it correct? or if am wrong, then how to modify the code to resolve the error as well as to fulfill the purpose of script?

Last edited by vish6251; 01-06-2013 at 05:31 PM..
# 10  
Old 01-07-2013
Again, output the $? after the cd command and after the assignment.
And, I'm pretty sure, the user directory will be created in useradd. So you need to check not for presence of dir, but for non-/ emptiness.
# 11  
Old 01-07-2013
Quote:
Originally Posted by RudiC
Again, output the $? after the cd command and after the assignment.
And, I'm pretty sure, the user directory will be created in useradd. So you need to check not for presence of dir, but for non-/ emptiness.
log showing return code after
Code:
cd $4$user

and
Code:
old=_old

Image


If possible, could you please modify the script for me? It would be great help.

Last edited by vish6251; 01-07-2013 at 04:14 PM..
# 12  
Old 01-10-2013
Pls post logs as text files, not screen shot images.
So, as the directory exists, you want to check if it's empty. After successfully cding to /neha, try sth like ls|wc -w. If that supplies anything different from 0, the dir is not empty and you should ask for overwrite.
# 13  
Old 01-10-2013
Quote:
Originally Posted by RudiC
Pls post logs as text files, not screen shot images.
So, as the directory exists, you want to check if it's empty. After successfully cding to /neha, try sth like ls|wc -w. If that supplies anything different from 0, the dir is not empty and you should ask for overwrite.
This is ful log report using
Code:
ls|wc -w

after assignment
Code:
old=_old

. Its showing that the dir is empty but still asking to overwrite. Return code after
Code:
ls|wc -w

is also 0.


Code:
root@bt:~# bash -vx add_user_orig userlist /filelist /files/ /
#!/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
	ls|wc -w
	echo code=$?
        if [[ $? -eq 0 ]]; then
                echo "file for $4$user already exists!"
                echo "Do you want to overwrite the file (y/n):"
                read answer
                if [[ $answer -eq y || $answer -eq Y ]]; then                        
                        for i in `cat $2`
                        do
                                cp $3$i $4$user > /dev/null
                        done
                elif [[ $answer -eq n || $answer -eq N ]]; 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
                fi
                chmod 700 $4$user /dev/null
        fi
}

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


################################### Main ##############################

        clear
+ 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
+ [[ 4 -lt 4 ]]

        # validate command line arguments
        wd=$(pwd)
pwd)
pwd
++ pwd
+ wd=/root
        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
+ [[ 4 -ne 0 ]]
+ ls userlist
+ [[ 0 -ne 0 ]]
+ ls /filelist
+ [[ 0 -ne 0 ]]
+ ls /files/
+ [[ 0 -ne 0 ]]
        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
cat $2
++ cat /filelist
+ for i in '`cat $2`'
+ ls //files//file1
+ [[ 0 -ne 0 ]]
+ for i in '`cat $2`'
+ ls //files//file2
+ [[ 0 -ne 0 ]]
        cd $wd > /dev/null
+ cd /root

        # Calling user creation method
        createUser $1 $2 $3 $4
+ createUser userlist /filelist /files/ /
cat $1
++ cat userlist
+ for w in '`cat $1`'
printf '%s\n' "$w";)
printf '%s\n' "$w";
++ printf '%s\n' neha
+ export user=neha
+ user=neha
perl -e 'print
crypt("password", "password")')
perl -e 'print
crypt("password", "password")'
++ perl -e 'print
crypt("password", "password")'
+ useradd -d /neha -m neha -p papAq5PwY/QQM
+ [[ 0 -ne 0 ]]
+ echo 'neha added with home_directory /neha and
default password: password'
neha added with home_directory /neha and
default password: password
+ echo 'Copying files...'
Copying files...
+ copyFiles userlist /filelist /files/ /
+ cd /neha
+ old=_old
+ wc -w
+ ls
0
+ echo code=0
code=0
+ [[ 0 -eq 0 ]]
+ echo 'file for /neha already exists!'
file for /neha already exists!
+ echo 'Do you want to overwrite the file (y/n):'
Do you want to overwrite the file (y/n):
+ read answer
n 
+ [[ n -eq y ]]
cat $2
++ cat /filelist
+ for i in '`cat $2`'
+ cp /files/file1 /neha
+ for i in '`cat $2`'
+ cp /files/file2 /neha
+ chmod 700 /neha /dev/null
+ for w in '`cat $1`'
printf '%s\n' "$w";)
printf '%s\n' "$w";
++ printf '%s\n' jess
+ export user=jess
+ user=jess
perl -e 'print
crypt("password", "password")')
perl -e 'print
crypt("password", "password")'
++ perl -e 'print
crypt("password", "password")'
+ useradd -d /jess -m jess -p papAq5PwY/QQM
+ [[ 0 -ne 0 ]]
+ echo 'jess added with home_directory /jess and
default password: password'
jess added with home_directory /jess and
default password: password
+ echo 'Copying files...'
Copying files...
+ copyFiles userlist /filelist /files/ /
+ cd /jess
+ old=_old
+ wc -w
+ ls
0
+ echo code=0
code=0
+ [[ 0 -eq 0 ]]
+ echo 'file for /jess already exists!'
file for /jess already exists!
+ echo 'Do you want to overwrite the file (y/n):'
Do you want to overwrite the file (y/n):
+ read answer
y
+ [[ y -eq y ]]
cat $2
++ cat /filelist
+ for i in '`cat $2`'
+ cp /files/file1 /jess
+ for i in '`cat $2`'
+ cp /files/file2 /jess
+ chmod 700 /jess /dev/null
+ return 0

Also if the answer for overwriting is "NO", the script doesn't create $user_old dir and simply copies files in current dir.

Last edited by vish6251; 01-10-2013 at 10:19 AM..
# 14  
Old 01-10-2013
Quote:
Originally Posted by vish6251
. . .
Its showing that the dir is empty but still asking to overwrite. Return code after
Code:
ls|wc -w

is also 0. . . .
Fine - but here we don't need the exit code, but the word count itself - i.e. the no. of files in dir:
Code:
CNT=$(ls|wc -w)
#    echo code=$?   <---- DON'T do this - see comment below!
if [[ "$CNT" -gt "0" ]]; then   #CNT is greater than zero - files exist - ask for overwrite!
.
.
.
fi

DON'T execute any command before evaluating the exit code - it will contain the last command's status, in the case above echo's status, which is almost always 0, rendering $? useless for your purposes. Save exit codes into e variable immediately for later evaluation!

Quote:
Also if the answer for overwriting is "NO", the script doesn't create $user_old dir and simply copies files in current dir.
Can't tell why - but the entire logics of your function needs some polishing. Step back and start from scratch - where do you come from? What do you want to achieve? E.g. there's four options - copy into empty dir, overwrite with or without saving old files, or don't copy at all. Try to sketch that on a paper slip and then start over coding. Setting the shell's -vx options helps troubleshooting as it prints out what the shell does.
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