Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need advice urgent

Status
Not open for further replies.

mohankrishna919

Programmer
Jan 18, 2012
14
0
0
US
I am new to perl
but after spending late nights for some days , i was able to generate code for database backup. the code will ask you enter servername , enter database name if i enter it will take backup
suppose my code is in backup.pl
now the thing is i need to get database backup by entering like this perl backup.pl -i servername -d databasename
if i hit enter it should take backup of given server name and database name.

My code:

use strict;
use Win32::SqlServer qw(SCALAR);

print "Please enter DB name:\n";
my $name = <STDIN>;
chomp($name);
# Log in to server .

my $sqlsrv = Win32::SqlServer::sql_init('localhost', 'geff', 'gentle', 'Northwind');
# Our SQL statement.

my $stmnt = <<SQLEND;
BACKUP database "$name" TO DISK = N'c:\\bak\\master.dmp'
SQLEND

# Run query.
my $result = $sqlsrv->sql($stmnt, SCALAR);

# Print results.
foreach my $name (@$result) {
print "$name\n";
}
 
You can run a command like this

Code:
perl myscript.pl username servername

simply by adding lines like these into your code:

Code:
$parameter1=$ARGV[0];
$parameter2=$ARGV[1];

Further parameters are just $ARGV[2], $ARGV[3] etc.

If I want something fancier I use

Code:
use Getopt::Long;
#get parameters
GetOptions("version=s" =>\$param{version},"fast=s" =>\$param{fast});
if(! $param{version})
{
    print "\nYou must specify the version attribute of the project e.g. -version 6.4.2_215\n";
    exit 1;
}
if(! $param{fast})
{
    print "\nYou must specify whether or not fast mode is to be used i.e. -fast yes|no\n";
    exit 1;
}
my $output_name=$param{version};
my $fast=$param{fast};
print "$output_name\n$fast\n"

This enables you to use a command like:

Code:
perl myscript.pl -fast yes -version 6.5.2_675
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top