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, 12:33 AM   #1
GavinCooper
LQ Newbie
 
Registered: Apr 2012
Location: New Zealand
Posts: 10

Rep: Reputation: Disabled
Major errors in script in executing and missing looping constructs (Please help !!)


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 "No arguments found!"
    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"
    echo "Use the -h argument (i.e. ./script -h) for help"
  exit 1
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 information from a user detail file (such as userlist.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 error report file respectively"
  exit
fi

#Reads first argument as user detail file for data
  cat userlist.txt | while read uname password gname fullname
#Reads /etc/passwd for Username
  egrep -w "^$uname" /etc/passwd
#If Username is found then error reports
  if [ $? == 0 ]; then
  echo "User Already Exists : Error adding user with username $uname;$gname;$fullname" >> Successes1.log
  exit 1
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")' $pass)
#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
    echo "Error: Must be root user to execute script"
    exit
  fi
done

Last edited by GavinCooper; 04-27-2012 at 12:51 AM.
 
Old 04-27-2012, 12:34 AM   #2
GavinCooper
LQ Newbie
 
Registered: Apr 2012
Location: New Zealand
Posts: 10

Original Poster
Rep: Reputation: Disabled
ive hit a brick wall guys please help, my field is networking and well im rather fresh to linux scripting. i cant for the love of god execute this thing.
 
Old 04-27-2012, 12:42 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Please use CODE tags when posting code to preserve indentation and make it more readable (most easily done by going into advanced mode and using the # button -- you can do that when editing and existing post too).

How do you try to execute the script? Is there any output?
 
Old 04-27-2012, 12:47 AM   #4
GavinCooper
LQ Newbie
 
Registered: Apr 2012
Location: New Zealand
Posts: 10

Original Poster
Rep: Reputation: Disabled
sorry really new this, but um i get a "syntax error: unexpected end of file" and "unexpected token 'done`" tried to find a looping contruct to add a do but failing miserably

---------- Post added 04-27-12 at 05:48 PM ----------

i use a bash command to execute it (.sh file btw )
 
Old 04-27-2012, 01:08 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by GavinCooper View Post
sorry really new this, but um i get a "syntax error: unexpected end of file" and "unexpected token 'done`" tried to find a looping contruct to add a do but failing miserably

---------- Post added 04-27-12 at 05:48 PM ----------

i use a bash command to execute it (.sh file btw )
Copy and paste from your command line session is more accurate and easier than a verbal description.

There was a missing if line ~18. Maybe something else too -- it's not the sort of script I want to run to test. Easy to see when indented regularly:
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 "No arguments found!"
    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"
    echo "Use the -h argument (i.e. ./script -h) for help"
    exit 1
  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 information from a user detail file (such as userlist.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 error report file respectively"
  exit
fi

#Reads first argument as user detail file for data
cat userlist.txt | while read uname password gname fullname
#Reads /etc/passwd for Username
egrep -w "^$uname" /etc/passwd
#If Username is found then error reports
if [ $? == 0 ]; then
  echo "User Already Exists : Error adding user with username $uname;$gname;$fullname" >> Successes1.log
  exit 1
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")' $pass)
#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
  echo "Error: Must be root user to execute script"
  exit
fi
 
Old 04-27-2012, 01:18 AM   #6
GavinCooper
LQ Newbie
 
Registered: Apr 2012
Location: New Zealand
Posts: 10

Original Poster
Rep: Reputation: Disabled
Wow this time it ran but it did not create the user nor produce a log it just echo'd the statements i added for the -h switch. not response was given other than that when i ran it.
 
Old 04-27-2012, 01:35 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
If you want effective help then help us to help you by copying and pasting the command line used to run the script.
 
Old 04-27-2012, 04:21 AM   #8
GavinCooper
LQ Newbie
 
Registered: Apr 2012
Location: New Zealand
Posts: 10

Original Poster
Rep: Reputation: Disabled
okie cool how can i help more ??

i use this command to execute the script

bash gfile.sh
 
Old 04-27-2012, 05:06 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by GavinCooper View Post
okie cool how can i help more ??

i use this command to execute the script

bash gfile.sh
And the output, was it
Code:
This is the help information file
This script is designed to add users to a linux system by reading information from a user detail file (such as userlist.txt)
The format of this user detail text file is username password groupname fullname seperated using TAB spacing
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 error report file respectively
That's what you added for the -h switch but from the information given it is more likely to have been "No arguments found! ...".

Are you not in a position to copy text from your terminal into your web browser?
 
Old 04-27-2012, 05:11 AM   #10
GavinCooper
LQ Newbie
 
Registered: Apr 2012
Location: New Zealand
Posts: 10

Original Poster
Rep: Reputation: Disabled
ive just up dated my script and i do get that put put if i state this line

[root@fiddles GavGav]# bash gfile.sh -h


Im really sorry what information would you like me to copy il re paste my updated script

---------- Post added 04-27-12 at 10:11 PM ----------

Code:
<!-- language: lang-sh -->


    #!/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 "Usage: $0 usernames report errors" 1>&2
        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"
        echo "Use the -h argument (i.e. ./script -h) for help"
        exit 1
      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 information from a user detail file (such as userlist.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 error report file respectively"
      exit
    fi
    
    #Reads first argument as user detail file for data
    cat jan.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
    done < $1
    #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
      echo "Error: Must be root user to execute script"
      exit 1
    fi
    fi
    done
 
Old 04-27-2012, 08:21 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
As an example of what would be helpful, here's a command line and the resulting output
Code:
c@CW8:/var/tmp$ ./try.sh -h
This is the help information file
This script is designed to add users to a linux system by reading information from a user detail file (such as userlist.txt)
The format of this user detail text file is username password groupname fullname seperated using TAB spacing
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 error report file respectively
Do you still have a question? The script runs and prints help text when called with the -h option.
 
Old 04-27-2012, 09:32 AM   #12
GavinCooper
LQ Newbie
 
Registered: Apr 2012
Location: New Zealand
Posts: 10

Original Poster
Rep: Reputation: Disabled
Code:
[root@fiddles GavGav]# bash gfile.sh -h
This is the help information file
This script is designed to add users to a linux system by reading information from a user detail file (such as userlist.txt)
The format of this user detail text file is username password groupname fullname seperated using TAB spacing
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 error report file respectively

Last edited by GavinCooper; 04-27-2012 at 09:51 AM.
 
Old 04-27-2012, 10:15 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Cool

Is the rest of the script's functionality working? If so, you can mark the thread SOLVED using the thread tools button.
 
Old 04-27-2012, 10:26 AM   #14
GavinCooper
LQ Newbie
 
Registered: Apr 2012
Location: New Zealand
Posts: 10

Original Poster
Rep: Reputation: Disabled
thanks alot every thing works now
 
Old 04-27-2012, 11:32 AM   #15
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
Does it really run? Seriously? Because there seem to be too many broken structures for me to really believe that.

Example:
Code:
    cat jan.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
    done < $1

The while..done loop here is being fed stdin from two sources at once, the pipe from jan.txt and the redirection from $1. Several of the if statements, here and elsewhere, don't seem to line up with proper closing marks either.

Then there's another "done" dangling at the end of the script that doesn't seem to match any actual loop.


Debugging things like this would be a lot easier if you applied better formatting. Don't just indent by two spaces, use a whole tab stop. And don't do it haphazardly, consistently indent every loop and test. Make sure that every single level of subcommands has its own clear indentation step.

Add some more blank lines between logical sections too. Leave plenty of blank space between the loops and groups of commands, so you can see at a glance where the change-ups occur.


Watch what happens when I try to apply consistent formatting to the above code:
Code:
cat jan.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
	
done < $1

Notice how easily we can see now that neither of the if statements has proper closings (at least not inside this code block, where they should be). That, or the "done" line is in the wrong place.


Finally, you should try to be consistent with your commands. For example, sometimes you use [..] tests, and sometimes [[..]] tests. Try to stick to the latter when using bash. (And if it's an integer comparison, switch to using ((..)) instead.)


Other than that though, I have to say that your in-line syntax is generally pretty good. Everything is quoted properly, everything is commented, and there's no glaring ugliness. You just need to take more care at the macro level.

Last edited by David the H.; 04-27-2012 at 11:35 AM.
 
  


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 12:59 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