Simple script for adding users

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:

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.

please check the attachment

        if [[ "$CNT"-gt "0 ]]; then
                          ^--- pls add double quote ("0")

I noticed that and after correction :

./add_user_rudi userlist /filelist /files /
./add_user_rudi: line 33: conditional binary operator expected
./add_user_rudi: line 33: syntax error near `"0"'
./add_user_rudi: line 33: `	if [[ "$CNT"-gt "0" ]]; then'

if [[ "$CNT"-gt "0" ]]; then'

forgot to add space after "$CNT"

Here's a complete version of your copyfiles function as there are still more errors in the version you sent:

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
        fi
        for i in $(cat $2) 
          do
          cp $3$i $4$user
          done
        chmod 700 $4$user
        }

still one problem, if selected not to overwrite, it doesnt create user_old folder but overwrites existing files.

Do you want to overwrite the file (y/n):
+ read answer
n
+ '[' N == N ']'
+ mv /neha neha_old
mv: cannot move `/neha' to a subdirectory of itself, `neha_old'
+ mkdir ///neha
mkdir: cannot create directory `///neha': File exists
cat $2)
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

check log from attachment.

But - the error msgs are clear, aren't they?

mv /neha neha_old
mv: cannot move `/neha' to a subdirectory of itself, `neha_old'
mkdir ///neha
mkdir: cannot create directory `///neha': File exists 

You need to design a dir structure that won't stand in its own light. And - use the parameters correctly. $4 here seems to contain the root dir. Then, use it consistently everywhere.
BTW - you had $4$user$old in your post #1...

1 Like

yes! yes! that was the last problem now code is working as it was designed for. Would you believe i have to submit this code tomorrow as my sem1 assignemnt! phew! what a close call!

Rudi, A BIG THANKS to you sir!

@MadeInGermany, thanks a lot for assisting.

am happy!! :slight_smile:

So - was this sort of homework, then? You rather should have posted it in the Homework & Coursework Questions - The UNIX and Linux Forums forum. Be glad you've not been intercepted...

wow i didnt knew there are forums which help in homework O.o will post there next time.

For homework, the solution might be okay.
In the real world, you would backup individual files to homedir/file_old, rather than dealing with a homedir_old