Simple script for adding users


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple script for adding users
# 15  
Old 01-10-2013
ok first problem is solved after implementing your solution. the script now checks if files are already present, if yes then asks to overwrite, if not then copies new files to dir. however if answered NOT TO overwrite, it doesnt create $user_old folder, instead overwrites files anyway.

This is the code :

Code:
copyFiles() {
        cd $4$user  
	old=_old
        ls|wc -w
	CNT=$(ls|wc -w)	
	if [[ $CNT -gt 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
	else
		for i in `cat $2`
                do
                        cp $3$i $4$user > /dev/null
                done
                chmod 700 $4$user /dev/null
        fi
}

any suggestions?

Last edited by vish6251; 01-10-2013 at 02:46 PM..
# 16  
Old 01-10-2013
First, you should delete all the > /dev/null
and especially the /dev/null behind the chmod 700 $4$user! This will spoil the permissions on /dev/null device,
please restore with
Code:
chmod 666 /dev/null

Then, it's more consequent to have mkdir $4$user
followed by additional commands chmod 700 $4$user and cd $4$user

Last edited by MadeInGermany; 01-10-2013 at 04:05 PM..
# 17  
Old 01-10-2013
Quote:
Originally Posted by vish6251
ok first problem is solved after implementing your solution. the script now checks if files are already present, if yes then asks to overwrite, if not then copies new files to dir. however if answered NOT TO overwrite, it doesnt create $user_old folder, instead overwrites files anyway.

This is the code : . . .
any suggestions?
Yes:
Code:
copyFiles() {
    cd $4$user  
    old=_old
#        ls|wc -w                               <--- remove, not needed
    CNT=$(ls|wc -w)    
    if [[ "$CNT" -gt "0" ]]; then               <--- double quotes never/rarely hurt
        echo "file for $4$user already exists!"
        echo "Do you want to overwrite the file (y/n):"
        read answer
        if [ "${answer^?}" == "N" ]; then       <--- make $answer upper case by parameter case modification, use string comparison operator, and double quotes ...
#                        for i in `cat $2`      <--- remove, will be done later
#                        do
#                                cp $3$i $4$user > /dev/null
#                        done
#   elif [[ $answer -eq n || $answer -eq N ]]; then   <--- remove, unneccessary
set -vx                                         <--- show shell's activities on variables
        mv $4$user $4$user$old > /dev/null      <---+ pls remove /dev/null to show what's going on
        mkdir /$4/$user > /dev/null             <---+
#            for i in `cat $2`                  <--- same as above
#                        do
#                                cp $3$i $4$user > /dev/null
#                        done

       fi     #    answer = "N" 

#    else
for i in $2                                     <--- cat not required!
   do
   cp $3$i $4$user > /dev/null                  <--- NOW you copy! And remove redirection...
   done
chmod 700 $4$user /dev/null                     <--- not the wisest thing to do...
#        fi
}

Not sure I caught every single stumbling block, but seems a good starting point to me...
# 18  
Old 01-10-2013
modified the code as suggested, this is error log :
Code:
./add_user_orig userlist /filelist /files/ /
./add_user_orig: line 34: conditional binary operator expected
./add_user_orig: line 34: syntax error near `"file'
./add_user_orig: line 34: `                echo "file for $4$user already exists!"

now this is new error Smilie

---------- Post updated at 08:28 PM ---------- Previous update was at 08:26 PM ----------

Quote:
Originally Posted by MadeInGermany
First, you should delete all the > /dev/null
and especially the /dev/null behind the chmod 700 $4$user! This will spoil the permissions on /dev/null device,
please restore with
Code:
chmod 666 /dev/null

Then, it's more consequent to have mkdir $4$user
followed by additional commands chmod 700 $4$user and cd $4$user
tried using
Code:
$user.old

instead of
Code:
$user$old

also removed
Code:
/dev/null

but no luck.

thanks for replying though
# 19  
Old 01-10-2013
There are several traps, e.g. file owner and permissions.
The following is simpler and should be safer:
Code:
copyFiles() {
        old=_old
        cd $4$user
        if [ $? -eq 0 ]; then
                echo "$4$user already exists !"
                printf "%s" "Do you want to overwrite the initial files (y/n):"
                read answer
                if [ "$answer" = "y" -o "$answer" = "Y" ]; then
                        for i in `cat $2`
                        do
                                if [ -e $4$user$i ]; then
                                  echo "backing up to $4$user$i$old"
                                  mv $4$user$i $4$user$i$old &&
                                  cp -p $4$user$i$old $4$user$i
# copied back to restore attributes and to ensure it's a plain file
                                fi
                                cp $3$i $4$user
                        done
                fi
                chmod 700 $4$user
        fi
}

# 20  
Old 01-10-2013
Quote:
Originally Posted by MadeInGermany
There are several traps, e.g. file owner and permissions.
The following is simpler and should be safer:
Code:
copyFiles() {
        old=_old
        cd $4$user
        if [ $? -eq 0 ]; then
                echo "$4$user already exists !"
                printf "%s" "Do you want to overwrite the initial files (y/n):"
                read answer
                if [ "$answer" = "y" -o "$answer" = "Y" ]; then
                        for i in `cat $2`
                        do
                                if [ -e $4$user$i ]; then
                                  echo "backing up to $4$user$i$old"
                                  mv $4$user$i $4$user$i$old &&
                                  cp -p $4$user$i$old $4$user$i
# copied back to restore attributes and to ensure it's a plain file
                                fi
                                cp $3$i $4$user
                        done
                fi
                chmod 700 $4$user
        fi
}

following this script, if answer is yes, the files are being copied to user dir but if said "No" then no files are being copied or backed up. and no user_old folders are created.
# 21  
Old 01-10-2013
Pls set -vx options in main script, and post the entire log (eventually as an attachment). Without the context, errors are very difficult to track down.
And, pls remove the # in front of the last fi

Here's the complete version of your copyfiles function, so you don't need to fumble around with half baked solutions:
Code:
copyFiles() {
        cd $4$user
        old=_old  
        CNT=$(ls|wc -w)
        if [[ "$CNT"-gt "0" ]]; then
                echo "file for $4$user already exists!" 
                echo "Do you want to overwrite the file (y/n):"
                read answer
                if [ "${answer^?}" == "N" ]; then          
                  mv $4$user $user$old
                  mkdir /$4/$user > /dev/null
                fi
         for i in $(cat $2) 
           do
           cp $3$i $4$user
           done
         chmod 700 $4$user
        fi
}

Any problems - post error log.

Last edited by RudiC; 01-10-2013 at 05:19 PM..
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