ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i wrote this simple perl script to make a backup for the users of the system
the script is basically working by taking the users parameters for /etc/passwd file and pass it to
a Mysql Database ....,
PHP Code:
#!/usr/bin/perl -w use strict; use DBI; use diagnostics; #step1 - create connection objection . my $dsn = 'DBI:mysql:contacts'; my $user = 'adam'; my $password = 'secret'; my $conn = DBI->connect($dsn,$user,$password) || die "Error connecting" . DBI->errstr;
$file = "/etc/passwd";
open (han1, "$file") || die "error opening file: $!";
my @newrecords = <han1>;
foreach (@newrecords) {
@columns = split /:/;
my $username = $columns[0]; my $x = $columns[1]; my $userid = $columns[2]; my $groupid = $columns[3]; my $realname = $columns[4]; my $homedir = $columns[5]; my $shellpath = $columns[6];
$conn->do("insert into users(username,x,userid,groupid,realname,homedir,shellpath) values('$username','$x','$userid','$groupid','$realname','$homedir','$shellpath')") || die "error preparing query" . $conn->errstr; }
it gives me lot of errors i want to know what is wrong any help will be appreciated
I agree with wjevans, also, you might want to do the following:
If you want to say or die... use 'or', not '||'. Perl best practice (it's to do with precedence if you really want to know).
If you are going to swallow the whole file into an array, close the file (and check the close()) before continuing.
Use explicit vars ie don't rely on implicit $_
chomp() the @newrecords array before using it, otherwise each rec ends in a newline (\n) and you don't want that.
you can assign all the named vars in the split() instead of using the intermediate @columns array
Use the DBI quote method ( http://search.cpan.org/~timb/DBI-1.56/DBI.pm#quote ) for string type vars during the insert.
root@adam-laptop:/home/adam/Desktop# ./backup.pl
Global symbol "$file" requires explicit package name at ./backup.pl line 11.
Global symbol "$file" requires explicit package name at ./backup.pl line 13.
Global symbol "@columns" requires explicit package name at ./backup.pl line 20.
Global symbol "@columns" requires explicit package name at ./backup.pl line 22.
Global symbol "@columns" requires explicit package name at ./backup.pl line 23.
Global symbol "@columns" requires explicit package name at ./backup.pl line 24.
Global symbol "@columns" requires explicit package name at ./backup.pl line 25.
Global symbol "@columns" requires explicit package name at ./backup.pl line 26.
Global symbol "@columns" requires explicit package name at ./backup.pl line 27.
Global symbol "@columns" requires explicit package name at ./backup.pl line 28.
Execution of ./backup.pl aborted due to compilation errors (#1)
(F) You've said "use strict vars", which indicates that all variables
must either be lexically scoped (using "my"), declared beforehand using
"our", or explicitly qualified to say which package the global variable
is in (using "::").
Uncaught exception from user code:
Global symbol "$file" requires explicit package name at ./backup.pl line 11.
Global symbol "$file" requires explicit package name at ./backup.pl line 13.
Global symbol "@columns" requires explicit package name at ./backup.pl line 20.
Global symbol "@columns" requires explicit package name at ./backup.pl line 22.
Global symbol "@columns" requires explicit package name at ./backup.pl line 23.
Global symbol "@columns" requires explicit package name at ./backup.pl line 24.
Global symbol "@columns" requires explicit package name at ./backup.pl line 25.
Global symbol "@columns" requires explicit package name at ./backup.pl line 26.
Global symbol "@columns" requires explicit package name at ./backup.pl line 27.
Global symbol "@columns" requires explicit package name at ./backup.pl line 28.
Execution of ./backup.pl aborted due to compilation errors.
at ./backup.pl line 35
hello dbi is a bit above my perl experience as i'm still learning, however you missed a my from $file on line 13 and my @collumns on line 22, if i try to run your code i get
Code:
root@localhost perl]# ./diagnoser.pl
Can't locate DBI.pm in @INC (@INC contains: /usr/lib/perl5/5.8.8/i386-linux /usr/lib/perl5/5.8.8 /usr/lib/perl5/site_perl/5.8.8/i386-linux /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.7/i386-linux /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl .) at ./diagnoser.pl line 4.
BEGIN failed--compilation aborted at ./diagnoser.pl line 4.
sorry i couldn't be more help
paul
ps sorry i called it diagnoser.pl
i have fixed my at the $file and the @columns but by the way i have anew error at preparing query
Quote:
DBD::mysql::db do failed: INSERT command denied to user 'adam'@'localhost' for table 'users' at ./backup.pl line 30, <han1> line 34.
Uncaught exception from user code:
error preparing queryINSERT command denied to user 'adam'@'localhost' for table 'users' at ./backup.pl line 30, <han1> line 34.
at ./backup.pl line 30
mmm i think that user adam is denied from logging on /etc/passwd file but this user is supposed to be in the database not on the system
#!/usr/bin/perl -w
use strict;
use DBI;
use diagnostics;
#step1 - create connection objection .
my $dsn = 'DBI:mysql:backupDB';
my $user = 'adam';
my $password = 'secret';
my $conn = DBI->connect($dsn,$user,$password) || die "Error connecting" . DBI->errstr;
my $file = "/etc/passwd";
open (han1, "$file") || die "error opening file: $!";
my @newrecords = <han1>;
foreach (@newrecords) {
my @columns = split /:/;
my $username = $columns[0];
my $x = $columns[1];
my $userid = $columns[2];
my $groupid = $columns[3];
my $realname = $columns[4];
my $homedir = $columns[5];
my $shellpath = $columns[6];
$conn->do("insert into users(username,x,userid,groupid,realname,homedir,shellpath) values('$username','$x','$userid','$groupid','$realname','$homedir','$shellpath')") || die "error preparing query" . $conn->errstr;
}
#the last question i want to know how can i make this operation updated i mean if i add new user for example how can i make the program to fetch the new records and add to the database
If you mean a new uer entry in /etc/passwd, you'll have to write a SELECT that checks each user in the file against the DB and inserts if it's not there, or possibly UPDATEs if it is.
Put all the SQL statements into a separate sub for each one.
I also refer you to my prev comment.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.