For using sed inside a shell script, The variable should be in "double quotes" and the command in 'single quotes'. Following is the example
#!/bin/bash
usage () {
echo "$0 -s < name> -r <replace name> -f <file name>"
exit
}
while getopts s:r:f: option
do
case "$option" in
s) search="$OPTARG";;
r) replace="$OPTARG";;
f) filename="$OPTARG";;
\?) usage
esac
done
sed -i 's/'"$search"'/'"$replace"'/g' "$filename"
Saturday, October 24, 2009
Using variable with sed inside a shell script
Posted by
Vishnu Agrawal
at
10:39 PM
0
comments
Labels: sed, shell scripting
Saturday, October 3, 2009
https redirection handling in httpunit
In httpunit when you are requesting a http page and if the page internally redirects to a https page, httpunit throws following error and response is not received:
sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
To resolve this error, add following code in your httpunit program.
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.*;
try{
SSLContext context = SSLContext.getInstance("SSLv3");
TrustManager[] trustManagerArray = { new NullX509TrustManager() };
context.init(null, trustManagerArray, null);
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new NullHostnameVerifier());
}catch(Exception e) {
e.printStackTrace();
}
class NullX509TrustManager implements X509TrustManager {
/* Implements all methods, keeping them empty or returning null */
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
}
class NullHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
Posted by
Vishnu Agrawal
at
4:23 PM
0
comments
Getting thread dump for java application
When an java application server freezes or becomes non-responsive, it is recommended to generate a thread dump for the application. A thread dump is a user-friendly snapshot of the threads and monitors in a Java Virtual Machine (JVM). A thread dump can range from fifty lines to thousands of lines of diagnostics depending on how complex your application is.
On UNIX platforms you can send a signal to a program by using the kill command. This is the quit signal, which is handled by the JVM. On Solaris you can use the command kill -QUIT process_id, where process_id is the process id of your Java program.
run the command kill -QUIT
On Windows platform, enter the key sequence <CTRL> <break>
Thread States :: The key used for the thread states in Thread dump is:
R ==> Running or runnable thread
S ==> Suspended thread
CW ==>Thread waiting on a condition variable
MW ==> Thread waiting on a monitor lock
MS ==> Thread suspended waiting on a monitor lock
Posted by
Vishnu Agrawal
at
3:57 PM
0
comments
Labels: java
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
Posted by
Vishnu Agrawal
at
8:16 PM
0
comments
Labels: perl script, sql
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.");
Posted by
Vishnu Agrawal
at
8:02 PM
0
comments
Labels: perl script
Thursday, June 25, 2009
Recover Photos from SD card
Recently my family went for a annual trip and accidentally removed the photos from my digital camera. They asked me if somehow we could recover those photos. As obvious, i did some googling and found out that there are many software available which can recover data from your SD card (most of them are paid and i was looking for any free alternative :) ). Finally i got this one PhotoRec (http://www.cgsecurity.org/wiki/PhotoRec). It do not require any installation, just download the software and start using it. (obviously you should have SD card reader in your system). Thanks to it, I could recover all photos.
While googling, i found other free software as well but didn't give them try, one of them was Mjm (http://www.mjm.co.uk/freephotorecoverysoftware.html)
Posted by
Vishnu Agrawal
at
10:32 PM
1 comments
Labels: digital camera
Sunday, May 31, 2009
Building brand value at workplace
Recently read an article from Delhi Times Ascent. Very inspiring and true
There are often times when an employee may wonder, how do i stand out in a team? And experts say that by simply completing tasks assigned on time or working hard for longer hours, may not necessarily be the answer. They say it takes more than just one's work and job profile to be outstanding. This is where the role of crafting your own "USP"(Unique Selling Proposition) comes into the picture.
Crafting your USP:
According to experts, the first thing to do while brand-building is to identify the qualities and characteristics that give you an edge over the rest. Understand your strengths/abilities and use it towards your advantage at the workplace. Maintaining good relations with people whom you deal with will also add to your brand value. Self-branding is no easy task. It comprises of identifying your best personality traits and displaying it from time to time through enhanced performance and higher productivity. This will not only get you noticed but will also give you an edge over your colleagues.
Build your brand:
- Be a great team-mate and a supportive colleague.
- Be passionate about your job and make others know that you are excellent in your particular task/job.
- Finish the tasks assigned to you on time with exceptional quality.
- Be proactive and discuss your views on improving things with your senior officials.
- Bring in innovations into your project/tasks to make it more efficient.
Posted by
Vishnu Agrawal
at
9:07 PM
0
comments
Labels: HR
Sunday, May 17, 2009
Comparing Excel Files
While testing any reporting application, one comes under situations when he/she have to compare two excel files. If the excel sheets are small, its easier to compare them manually but as long the sheets becomes large, you need some automated way to compare those.
Earlier, i used to export those files as csv and then comparing them using perl scripts but it was an overhead to exporting and then comparing so i thought of doing something within the excel sheets itself. After googling, i found that it can be done by using Excel Macros. I did try that, but found that you need to open those excel files in Microsoft Excel and then apply the macro, which again i didn't like as it require to open MS Excel. I was interested to have a program which i can run from command prompt. Finally i got my solution. I converted the above macro as a standalone vb script program and used it from command prompt.
The great benefit with this script is that i can take control of displaying the diff results (currently, i am highlighting the rows in the excel sheet itself.)
to run this program, open command prompt and run following command:
C:\> cscript /nologo ExcelCompare.vbs <ExcelFile1> <ExcelFile2>
Following is the program:
Posted by
Vishnu Agrawal
at
11:44 PM
0
comments
Run a Visual Basic Script Program from a command prompt
A Visual Basic script program can be run from a DOS prompt. cscript command can be used for this purpose.
C:\>cscript <pathToVBScriptFile>
by default cscript command displays the Microsoft Header but this can be avoided by providing /nologo argument.
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
C:\>cscript /nologo <pathToVBScriptFile>
User can also take control to take input and display the output on the console. For that user had to use following options in the VB Script.
dim input
dim output
dim name
set output = wscript.stdout
set input = wscript.stdin
output.write "Enter Your Name : "
name = input.readline
Posted by
Vishnu Agrawal
at
12:04 PM
0
comments
Labels: vbscript
Monday, April 27, 2009
Compare two files line by line
Posted by
Vishnu Agrawal
at
11:09 PM
0
comments
Labels: perl script