Results 1 to 4 of 4
Hey,
For some reason my perl script isn't working to add users to my box. The script is:
Code:
#!/usr/bin/perl -w
my $home = "/home/"; #Base home directory for all ...
- 09-26-2008 #1Just Joined!
- Join Date
- Sep 2008
- Posts
- 3
Perl Scriptzz
Hey,
For some reason my perl script isn't working to add users to my box. The script is:
The only error message I receive when I run the file is "Abort". Nothing else. Any ideas of what might be wrong?Code:#!/usr/bin/perl -w my $home = "/home/"; #Base home directory for all users my $department; #Department of Users my $name; #Full name of users my $office; #User's office number my $extension; #User's office phone my $username; #Login name for user my $password; #Unencrypted password my $cryptpass; #Encrypted password my $groupID; #System group ID for user's department my $file; #File containing users to add my $test1; my $test2 = <USERSCRIPT>; my $lastname; my $middlename; my $firstname; my $firstlet; my $seclet; my $thirdlet; my $deplet; my $pw=crypt("Netsys1",14); print "Running script...."; open(USERSCRIPT, "lab5data.txt"); foreach $line (<USERSCRIPT>){ ($name,$office,$extension,$department) = split(/\t/, $line); chomp($department); if(lc($department) eq "engineering") { ($lastname,$firstname,$middlename) = split(" ", $name); $firstlet = substr($firstname,0,1); $seclet = substr($middlename,0,1); $thirdlet = substr($lastname,1,1); $deplet = substr($department,0,1); $username = $firstlet.$seclet.$thirdlet.$deplet; system("useradd $username -d /home/Engineering/$username -g $department -p $pw -s /bin/csh"); } else { ($lastname,$firstname,$middlename) = split(" ", $name); $firstlet = substr($firstname,0,1); $seclet = substr($middlename,0,1); $thirdlet = substr($lastname,1,1); $deplet = substr($department,0,1); $username = $firstlet.$seclet.$thirdlet.$deplet; system("useradd $username -d /home/$department/$username -g $department -p $pw"); } } close(USERSCRIPT); print "Script done."
- 09-27-2008 #2Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Firstly if you're forcing your users to use the abomination that is csh no wonder perl is bombing out.

When I run your script on my system I get the following diagnostics, which may help you track down your problems (the line numbers may not exactly match because of whitespace changes during cutting and pasting from your post):
Code:Warning: Use of "-s" without parentheses is ambiguous at ./perltest line 40. Use of /c modifier is meaningless without /g at ./perltest line 40. Bareword found where operator expected at ./perltest line 40, near "/bin/csh" (Missing operator before h?) Unquoted string "h" may clash with future reserved word at ./perltest line 40. String found where operator expected at ./perltest line 44, near "($lastname,$firstname,$middlename) = split("" (Might be a runaway multi-line "" string starting on line 40) (Missing semicolon on previous line?) String found where operator expected at ./perltest line 50, near "# system("" (Might be a runaway multi-line "" string starting on line 44) (Missing semicolon on previous line?) Bareword found where operator expected at ./perltest line 50, near "# system("useradd" (Missing operator before useradd?) Warning: Use of "-d" without parentheses is ambiguous at ./perltest line 50. Scalar found where operator expected at ./perltest line 50, near "/home/$department" (Missing operator before $department?) String found where operator expected at ./perltest line 56, near "print "" (Might be a runaway multi-line "" string starting on line 51) (Missing semicolon on previous line?) Bareword found where operator expected at ./perltest line 56, near "print "Script" (Do you need to predeclare print?) syntax error at ./perltest line 40, near "$pw -s " Can't find string terminator '"' anywhere before EOF at ./perltest line 56.
- 09-27-2008 #3Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
I fixed a few broken lines that caused most of the problems. They were at the lines calling useradd. On most systems you'll need remove the echo and to run as root if it seems OK when run as a normal user.
These are the additional changes I made:
- created a fake data file to test it. Providing samples of data always helps when asking questions in a forum.
- ran your code through perltidy.
- added echo to show what the command was.
- added "\n" at the end of your print statements.
This shell script drives your perl script:
This is the perl script on file p1:Code:#!/bin/bash - # @(#) s1 Demonstrate perl program. echo echo "(Versions displayed with local utility \"version\")" version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) perl set -o nounset echo FILE=${1-lab5data.txt} echo " Data file $FILE:" cat $FILE echo echo " Results:" ./p1 exit 0
This is the shell script output:Code:#!/usr/bin/perl -w my $home = "/home/"; #Base home directory for all users my $department; #Department of Users my $name; #Full name of users my $office; #User's office number my $extension; #User's office phone my $username; #Login name for user my $password; #Unencrypted password my $cryptpass; #Encrypted password my $groupID; #System group ID for user's department my $file; #File containing users to add my $test1; # my $test2 = <USERSCRIPT>; my $lastname; my $middlename; my $firstname; my $firstlet; my $seclet; my $thirdlet; my $deplet; my $pw = crypt( "Netsys1", 14 ); print "Running script....\n"; open( USERSCRIPT, "lab5data.txt" ); foreach $line (<USERSCRIPT>) { ( $name, $office, $extension, $department ) = split( /\t/, $line ); chomp($department); if ( lc($department) eq "engineering" ) { ( $lastname, $firstname, $middlename ) = split( " ", $name ); $firstlet = substr( $firstname, 0, 1 ); $seclet = substr( $middlename, 0, 1 ); $thirdlet = substr( $lastname, 1, 1 ); $deplet = substr( $department, 0, 1 ); $username = $firstlet . $seclet . $thirdlet . $deplet; system( "echo useradd $username -d /home/Engineering/$username -g $department -p $pw -s /bin/csh" ); } else { ( $lastname, $firstname, $middlename ) = split( " ", $name ); $firstlet = substr( $firstname, 0, 1 ); $seclet = substr( $middlename, 0, 1 ); $thirdlet = substr( $lastname, 1, 1 ); $deplet = substr( $department, 0, 1 ); $username = $firstlet . $seclet . $thirdlet . $deplet; system( "echo useradd $username -d /home/$department/$username -g $department -p $pw" ); } } close(USERSCRIPT); print "Script done.\n"
Best wishes ... cheers, drlCode:% ./s1 (Versions displayed with local utility "version") Linux 2.6.11-x1 GNU bash 2.05b.0 perl 5.8.4 Data file lab5data.txt: vieness e waltzer someplace post628741 engineering Results: Running script.... useradd ewie -d /home/Engineering/ewie -g engineering -p 14Yw2jgxwj1G2 -s /bin/csh Script done.
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 09-27-2008 #4Linux User
- Join Date
- Aug 2006
- Posts
- 458
if you have Python
Code:import os import crypt home="/home" pw = crypt.crypt("Netsys","14") shell = os.path.join("/bin","csh") print "running script" for lines in open("file"): name,office,extension,department=lines.strip().split() lastname,firstname,middlename = name.split() username = firstname[0]+middlename[0]+lastname[1]+department[0] if department.lower() == "engineering": path=os.path.join(home,"Engineering") cmd = "username %s -d %s -g %s -p %s -s %s " %( username, path , department,pw,shell ) else: path=os.path.join(home,department) cmd = "username %s -d %s -g %s " %( username, path ,username) os.system(cmd)


Reply With Quote