Monday, July 20, 2009

Run sql query from command line

We all know to run sql scripts (.sql files) from command prompt but sometimes we need to run a sql query from command prompt. It is sometimes necessary when we have to fetch a query result from a script (shell/perl) and process it. By following way, we can run a sql query from command prompt.

echo "select * from dual;" | sqlplus -S user/pwd\@sid

Send Email from perl script

Sometimes we do not have enough privileges to install perl modules on the host system and we have to work with ideal installation of perl. In that case, simpler way to send emails using perl is to use sendmail command. Following function can be used to send emails in perl.

sub sendEmail
{
($from, $to, $subject, $message) = @_;

my $sendmail = '/usr/lib/sendmail';
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $from\n";
print MAIL "To: $to\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message\n";
close(MAIL);
}

above function can be used as below:

sendEmail("vishnu\@test.com", "vishnu\@mytest.com", "Test sendEmail.", "Testing sendEemail function.");