LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-26-2012, 10:58 PM   #1
Gren
LQ Newbie
 
Registered: Apr 2012
Posts: 1

Rep: Reputation: Disabled
Bash script for adding users is running but not adding users


Hi all

My friend and I are trying to write a script for a Fedora system which adds users by reading from a text file with the layout usernameassword:groupname:fullname. The script seems to run as it displays the about and help text from their respective files, however no new users are being added when we check /etc/passwd/.

Code:
#!/bin/bash

#script to create user with username,password,groupname,fullname

if [ $(id -u) -ne 0 ]; then

#this above line will check the id not equal to 0

   echo "only root users may add a user to the system"
exit 1
fi

#this will display information about the script file when it is executed

 if [ $# -ne 1 ]; then
cat about.txt
exit 0

#here when -h is typed with script then it will display text in the helpfile.txt file and give information about using the script

fi
if [ $1 == "-h" ]; then
cat helpfile.txt
exit 1
fi

#this will make the string divided by ':'

sed 's/':'/ /g' $1 >list_user.txt

# read the userlist in format: username,password,groupname,fullname

cat list_user.txt | while read uname password gname fullname
do

#encrypting the password and assigned to variable

    pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)

#assiging the length of password to variable LEN

    LEN=$(echo ${#password})
    egrep -w  "^$uname" /etc/passwd >/dev/null

# this is to search the username in /etc/passwd and send output to null file

#if the username exists then it wil send the error report to error_report.txt

   if [ $? -eq 0 ]; then
         echo "$uname exists! try a different user name" >>error_report.txt
    continue

#if username does not exist then it will continue to the next iteration

   fi

# here we check the length of password is in between 9 an 12

if [[( $LEN -lt 9) || ($LEN -gt 12)]] ;  then
              echo " user with $uname: length of the password is not in between 9 and 12">>error_report.txt
       continue

#if password length is in between 9 and 12 then it will contiue to the next iteration

   fi
              egrep -w  "^$gname" /etc/group >/dev/null
          if [ $? -ne 0 ]; then

#if above command executed sucessfully then script will groupadd

                 groupadd "$gname"
           fi

#to create user with username,password,groupname,fullname

       useradd -p "$pass" -g "$gname" -c "$fullname" $uname
                 uid=$(grep -i ${uname} /etc/passwd | cut -d: -f3)

#we are assigning the 3rd field of the ouput of username from /etc/passwd

                 gid=$(grep -i ${uname} /etc/passwd | cut -d: -f4)

#we are assigning the 4th field of the output of username from /etc/passwd

       if [ $? -eq 0 ];then

# if the useradd cmd executed succesfully then userdetails are redirected to userdetails.txt

              echo $uname";"$uid";"$gname";"$gid";"$(date)";"$fullname>>userdetails.txt
       echo "$uname user added "
        else
           echo "user with $uname not added try to find error">>error_report.txt

#if useradd program is not added succesfull then error will be redirected to errorr_report

       fi
done
 
Old 04-27-2012, 03:15 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
1)
Code:
cat about.txt
this will only work when about.txt is in the current directory. You can also use bash here document here
Code:
cat <<- END_OF_DOCUMENT
	text ...
END_OF_DOCUMENT
2)
Code:
sed 's/':'/ /g' $1 >list_user.txt
You can't do this with sed. sed will search for the file $1 and try to read it. To pass string to sed, use either echo,

Code:
echo "$1" | sed '...'
or here string

Code:
sed '...' <<< "$1"
3)
Code:
cat list_user.txt | while read uname password gname fullname
So, you write the argument into file and then read it again? May I ask why?
Also, why the loop? The file will only contain one line.

4)
Code:
LEN=$(echo ${#password})
OK, why the echo? Why don't you just write
Code:
LEN="$(#password)"
5)
Code:
if [[( $LEN -lt 9) || ($LEN -gt 12)]] ;  then echo "..."
    ...
fi
You can also use bash arithmetics:
Code:
if (( (LEN < 9) || (LEN > 12) )); then
    ...
fi
or even

Code:
(( (LEN < 9) || (LEN > 12) )) && (echo ""; continue)
fi
6)
Code:
       useradd -p "$pass" -g "$gname" -c "$fullname" $uname
                 uid=$(grep -i ${uname} /etc/passwd | cut -d: -f3)
                 gid=$(grep -i ${uname} /etc/passwd | cut -d: -f4)
       if [ $? -eq 0 ];then
           ...
# if the useradd cmd executed succesfully then userdetails are redirected to userdetails.txt
No, actually the assignments will overwrite the exit status of useradd.

7) try to run your script with -x switch

8) did you look into your log file?

Last edited by millgates; 04-27-2012 at 03:23 AM. Reason: typos
 
Old 04-27-2012, 03:53 AM   #3
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by Gren View Post
Hi all


Code:
.......

#to create user with username,password,groupname,fullname

       useradd -p "$pass" -g "$gname" -c "$fullname" $uname
                 uid=$(grep -i ${uname} /etc/passwd | cut -d: -f3)
This line is maybe also trouble, because I don't find it using this way with specifying the option -p pass.
Leave that out and use another command:
Code:
passwd "username"
but then you will need to enter the password to the prompt.
Take a look here for the script example.

good luck
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding new users via Shell script coolfrog Linux - General 10 12-05-2010 10:47 AM
Adding users via a script kchakrak Linux - Server 6 10-26-2009 08:48 PM
Script for adding users BlueStag Linux - General 3 03-14-2006 09:40 PM
bash script for adding multiple users pilipk01 Linux - Newbie 4 01-12-2004 10:05 PM
Samba's running Now I need help with adding users ikw38 Linux - Networking 5 06-08-2003 06:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration