LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-27-2012, 09:34 PM   #16
GavinCooper
LQ Newbie
 
Registered: Apr 2012
Location: New Zealand
Posts: 10

Original Poster
Rep: Reputation: Disabled

Code:
#!/bin/bash
#Purpose: Automatically add new users in a linux system based upon the data found within a text file
#         Assign encryped passwords to each user
#         Add users to groups or create new groups for these users
#         Report errors and successful operations where necessary in log files
#         post help options (echo)

#Root validation
if [[ $(id -u) -eq 0 ]]; then
  #Argument validation
  if [[ -z "$1" ]]; then
    echo "Please include a user detail text file as first argument"
    echo "Please include a report text file as second argument"
    echo "Please include an error report text file as the third argument"


  fi
fi

#Help validation and Help file
if [[ "$1" = "-h" ]]; then
  echo "This is the help information file"
  echo "This script is designed to add users to a linux system by reading in            formation from a user detail file (such as finale2.txt)"
  echo "The format of this user detail text file is "username password groupname        fullname" seperated using TAB spacing"
  echo "This script will read the first argument as the user detail file, the           second and third arguments will be read as a success report file and err        or report file respectively"
  exit
fi

#Reads first argument as user detail file for data
cat finale2.txt | while read uname password gname fullname; do
#Reads /etc/passwd for Username
 egrep -w "^$uname:" /etc/passwd >/dev/null 2>&1
#If Username is found then error reports
if [ $? == 0 ]
then
  echo "User Already Exists : Error adding user with username $uname;$gname;$fullname" >> Errors1.log

else
  #Reads /etc/group for Groupname
  egrep -w "^$gname" /etc/group
  #If Groupname is found then nothing
if [ $? == 0 ]; then
   echo ""
else
  #If Groupname not found then creates new group and reports
  groupadd "$gname"
    echo "Group Not Found: New Group $gname was created" >> Successes1.log
fi
#Retrieves Date
createddate=$(date)
#Perl password script takes input from Userlist
pass=$(perl -e 'print crypt($ARGV[0], "Password")' "$password")
#Adds Users with variables from userlist
useradd "$uname" -g "$gname"  -c "$fullname" -p "$pass"
#Reports information to successlist and errorlist report files
if [ $? == 0 ]
then
  groupid=$(id -g $uname)
  userid=$(id -u $uname)
  echo "User Successfully Added: $uname;$userid;$gname;$groupid;$createddate;$fullname" >> Successes1.log
else
  groupid=$(id -g $uname)
  userid=$(id -u $uname)
  echo "Useradd Error Occurred: $uname;$userid;$gname;$groupid;$createddate;$fullname" >> Errors1.log
   exit 1
fi
fi
done
 
Old 04-27-2012, 09:36 PM   #17
GavinCooper
LQ Newbie
 
Registered: Apr 2012
Location: New Zealand
Posts: 10

Original Poster
Rep: Reputation: Disabled
thats the updated script that was the old one, there were many errors but i seeked help from many sources and manage to come up with this solution. this one compiles with log and report file, unlike the previous which looped back at "exit 1" function mistakenly put there.
 
Old 04-28-2012, 04:37 AM   #18
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
That's fine. It's understandable if you posted the wrong code.

But my basic point still stands. Your script formatting needs a lot of work. I highly suggest you apply the suggestions I gave.

As it is, you're getting errors simply because you can't clearly read what you've written.


Here, I'll even do it for you this time, so you can see what kind of difference it makes.

Code:
#!/bin/bash

#Purpose: Automatically add new users in a linux system based upon the data found within a text file
#         Assign encryped passwords to each user
#         Add users to groups or create new groups for these users
#         Report errors and successful operations where necessary in log files
#         post help options (echo)

#Root validation
if (( $(id -u) == 0 )); then

	#Argument validation
	if [[ -z "$1" ]]; then

		echo "Please include a user detail text file as first argument"
    		echo "Please include a report text file as second argument"
	    	echo "Please include an error report text file as the third argument"

	fi
fi

#Help validation and Help file
if [[ "$1" = "-h" ]]; then

	echo "This is the help information file"
	echo "This script is designed to add users to a linux system by reading in formation from a user detail file (such as finale2.txt)"
	echo "The format of this user detail text file is "username password groupname fullname" seperated using TAB spacing"
	echo "This script will read the first argument as the user detail file, the second and third arguments will be read as a success report file and err or report file respectively"

	exit

fi

#Reads first argument as user detail file for data
cat finale2.txt | while read uname password gname fullname; do

	#Reads /etc/passwd for Username
	egrep -w "^$uname:" /etc/passwd >/dev/null 2>&1

	#If Username is found then error reports
	if (( $? == 0 )); then

		echo "User Already Exists : Error adding user with username $uname;$gname;$fullname" >> Errors1.log

	else

		#Reads /etc/group for Groupname
		egrep -w "^$gname" /etc/group

		#If Groupname is found then nothing
		if (( $? == 0 )); then

			echo ""

		else

			#If Groupname not found then creates new group and reports
			groupadd "$gname"

			echo "Group Not Found: New Group $gname was created" >> Successes1.log

		fi

		#Retrieves Date
		createddate=$(date)

		#Perl password script takes input from Userlist
		pass=$(perl -e 'print crypt($ARGV[0], "Password")' "$password")

		#Adds Users with variables from userlist
		useradd "$uname" -g "$gname"  -c "$fullname" -p "$pass"

		#Reports information to successlist and errorlist report files
		if (( $? == 0 )); then

			groupid=$(id -g $uname)
			userid=$(id -u $uname)

			echo "User Successfully Added: $uname;$userid;$gname;$groupid;$createddate;$fullname" >> Successes1.log

		else

			groupid=$(id -g $uname)
			userid=$(id -u $uname)

			echo "Useradd Error Occurred: $uname;$userid;$gname;$groupid;$createddate;$fullname" >> Errors1.log

			exit 1

		fi

	fi

done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
init.d script not executing and missing from chkconfig gw1500se Mandriva 3 11-19-2010 11:27 AM
Making rhythmbox work in Slackware64 13.1 -current: errors .. (helper script missing) GrapefruiTgirl Slackware 8 08-09-2010 05:08 AM
[SOLVED] avoid displaying errors while executing a script vikas027 Programming 4 10-31-2007 01:27 AM
Looping a script Keentolearn Linux - Newbie 4 01-30-2007 03:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:01 AM.

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