1. Write a shell script in Linux to shift all characters in a
file forward by five characters. (Thus “a” becomes “f’”)?
cat file.txt|tr [a-z A_Z} [f-za-e F_ZA-E]
2. ODD or EvEn
echo "Enter the Number"
read a
b=`echo "$a % 2"|bc`
if [ $b -eq 0 ]
then
echo "Given Number is Even "
exit
fi
echo " Given Number is odd"
3.write a shell script that counts a number of unique word contained in the file and print them in alphabetical order line by line?
Make ' ' to '\n' then pipe to sort|uniq.
#!/bin/csh
# Here tr -s ' ' replaces all multiple ' ' with single ' '
# next pipe the above stream to replace each ' ' with '\n'
# next pipe the above stream to get a sorted list of words
# then pipe the unique words to outfile
tr -s ' ' < $1 | tr ' ' '\n' | sort | uniq > $1.out
4. how to convert a string to int???
To increment
echo enter the string
read str
str=`expr $str + 1`
echo $str
for integer
declare following in your script
typeset -i str
5.string reverse using shell scripts:
6.Shift Command in Command Line argts:
What is the use of "shift" command in passing parameters?
"shift" is useful when you need to access positional
parameters more than 9.
EX- execute a script to display 11th position parameter.
#./test.sh 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th
12th
ex:
withen the script write "shift 2" before "echo $9"to
display 11th parameter.
withen the script write "shift 3" before "echo $9"to
display 12th parameter.
7.What do u mean by $#,$* in unix programming?
$# The number of command line argument
$* All the arguments on the command lin
8. Grep,e-grep [ grep extended with Regular Expressions ]
Grep Man Page
9. How to modify the PATH variable and make it executable?
we can set PATH by using export PATH command.
ex:-
PATH=/opt/j2sdk1.4.2_04/bin:............
export PATH
and to make it executable we can set the same
in ".bash_profile".
To execute it ". .bash_profile"
2)
setenv PATH "/workingdir/"
10. Linux Shell Scripting Beginner Tutorial:
Click here
Disclaimer: Information here are Just gathered from Books and Web Links. Any Part of Information can be removed,if intimated..
Thursday, May 28, 2009
Wednesday, May 27, 2009
Google Search Tips
Google தேடல் டிப்ஸ்
மேற்கோள் குறிகள் இரண்டாக அமையட்டும்: எப்போதும் நீங்கள் தேடும் சொற்கள் அல்லது சொல் தொகுதி கொண்ட இணைய தளம் வேண்டுமென்றால் அவற்றை இரு மேற்கோள் குறிகளுக்குள் அமைக்கவும். எடுத்துக் காட்டாக women who love rosesஎன்ற சொல் தொகுதி கொண்ட தளம் தேவைப்படுகையில் மேற்கோள் குறி இல்லாமல் தேடிய போது 3,46,00,000 என்ற எண்ணிக்கையில் தளங்கள் இருப்பதாகக் காட்டியது. இவற்றில் பெரும்பாலானவை நாம் தேடும் தளங்களுக்குச் சம்பந்தமில்லாதவையாக இருக்கலாம். இதனையே ""women who love roses'' என டைப் செய்து தேடிய போது தளங்கள் 45,500 என்று காட்டப்பட்டது. மேற்கோள் குறிகளுக்குள் அமைக்கப்படுகையில் மிகச் சரியாக நாம் எந்த வரிசையில் சொற்களை அமைத்திருக்கிறோமோ அந்த வரிசையில் சொல் தொகுதிகள் இருந்தால் மட்டுமே தளங்கள் கணக்கில் எடுத்துக் கொள்ளப்பட்டு தரப்படுகின்றன.
1. சிறிய நட்சத்திரக் குறியீடு (*) : ஆங்கிலத்தில் wildcard என்று கூறப்படுவதனைக் குறிக்க சிறிய நட்சத்திரக் குறியீட்டினைப் பயன்படுத்துகிறோம். இதனை உங்கள் தேடுதல் பணியிலும் பயன்படுத்தலாம். குறிப்பிட்ட சொல்லினை அடுத்து எந்த சொல் இருந்தாலும் பரவாயில்லை என்று அமைக்க இந்த குறியீட்டினை அமைத்துத் தேடலாம். குறிப்பிட்ட சொல் தொகுதியில் ஒரே ஒரு சொல்லை மறந்துவிட்டு அதனைத் தேட முயற்சிக்கையில் இந்த வகை குறியீட்டுடனான தேடல் கை கொடுக்கிறது. எடுத்துக் காட்டாக காவிரியின் குறுக்கே உள்ள பாலம் என்பதனைக் குறிக்க "the bridge on the river cauvery'' என அமைக்கலாம். ஆனால் காவேரி என்பது மறந்துவிட்டால் the bridge on the river XXஎன அமைக்க முடியாது. அது தவறான தகவலையே தரும். இங்கே the bridge on the river * என அமைத்தால் காவேரி குறுக்கே உள்ள பாலங்கள் குறித்த தகவல் கிடைக்கும். இரண்டு சொற்கள் இருக்கலாம் என தேட விரும்பினால் "the bridge on the river ** ”என அமைக்கவும்.
2.குறிப்பிட்ட தளத்தில் மட்டும்: இணையத்தில் ஒரு குறிப்பிட்ட தளத்தில் மட்டும் நீங்கள் தேடும் தகவல் உள்ளது. ஆனால் அது எங்கே தரப்பட்டுள்ளது என்று நினைவில் இல்லை. அந்த தளத்தில் மட்டும் தேட விரும்பினால் "site:" என்று கொடுத்துத் தேடலாம். எடுத்துக் காட்டாக தமிழ் நாடு அரசின் இணைய தளமான www.tn.nic.in என்ற இணைய தளத்தில் மட்டும் ஞிச்ததிஞுணூதூ என்ற சொல் உள்ள இடங்களைப் பார்க்க விரும்பினால் "cauvery" site:www.tn.nic.in என டைப் செய்து கொடுத்து சரியான முடிவுகளைப் பெறலாம். இதே போல ஒரு குறிப்பிட்ட தளத்தில் நீங்கள் தேடும் தகவல் இருப்பது நன்றாக உங்களுக்குத் தெரியும். எனவே இந்த தளம் இல்லாமல் மற்ற தளங்களில் தேடித் தகவல்களை அறிய வேண்டும் என்றால் ஒரு கழித்தல் () அடையாளத்தை இங்கு ண்டிtஞு: ஆப்பரேட்டருக்கு முன்னால் பயன்படுத்த வேண்டும். எடுத்துக் காட்டாக காவேரி குறித்து தமிழ்நாடு அரசு தளம் தவிர மற்ற தளங்களில் தேட வேண்டும் என்றால் "cauvery" site:www.tn.nic.in எனக் கொடுத்துத் தேடலாம்.
3. அதே பொருள் தரும் சொற்களைத் தேட: ஒரு சொல்லின் பொருள் சார்ந்து இருக்கும் மற்ற சொற்களை அறிந்து கொள்ள கூகுள் தேடலை வரையறை செய்திடலாம். இதற்கு ஆங்கிலத்தில் tilde ("~") என அழைக்கப்படும் அடையாளக் குறியைப் பயன்படுத்த வேண்டும். இதனை நாம் தேடும் சொல்லின் முன்னால் எந்த இடைவெளியும் இன்றி அமைக்க வேண்டும். எடுத்துக் காட்டாக எம்.எஸ்.வேர்ட் தொகுப்பு குறித்து விளக்கமான குறிப்புகள் தரும் தளங்களைக் காண விரும்புகிறீர்கள். இந்த குறிப்புகள் என எப்படி வேண்டுமென்றாலும் இருக்கலாம். இவற்றைப் பெறும் வழியை இந்த ஆப்பரேட்டர் தருகிறது. இங்கு "msword" ~tutorial என அமைத்தால் எம்.எஸ் வேர்ட் குறித்த basics, hints, guide, tips, tricks ஆகிய அனைத்தும் கிடைக்கும்.
4.விளக்கங்கள் பெற: ஒரு சொல்லின் விளக்கங்கள் பெற கூகுள் "define" என்ற ஆப்பரேட்டரைக் கையாள்கிறது. எந்த சொல்லுக்கு விளக்கம் வேண்டுமோ அதன் முன்னால் இதனை இணைத்துச் செயல்படுத்தினால் போதும். எடுத்துக் காட்டாக nano technology என்ற சொல் குறித்து விளக்கம் வேண்டும் என்றால் define nano technology என்று கொடுத்தால் போதும். விளக்கத்தோடு இந்த சொல் பயன்படுத்தப்பட்டுள்ள அனைத்து தளங்களும் கிடைக்க what is nano technology எனக் கொடுத்துப் பெறலாம்.
மேற்கோள் குறிகள் இரண்டாக அமையட்டும்: எப்போதும் நீங்கள் தேடும் சொற்கள் அல்லது சொல் தொகுதி கொண்ட இணைய தளம் வேண்டுமென்றால் அவற்றை இரு மேற்கோள் குறிகளுக்குள் அமைக்கவும். எடுத்துக் காட்டாக women who love rosesஎன்ற சொல் தொகுதி கொண்ட தளம் தேவைப்படுகையில் மேற்கோள் குறி இல்லாமல் தேடிய போது 3,46,00,000 என்ற எண்ணிக்கையில் தளங்கள் இருப்பதாகக் காட்டியது. இவற்றில் பெரும்பாலானவை நாம் தேடும் தளங்களுக்குச் சம்பந்தமில்லாதவையாக இருக்கலாம். இதனையே ""women who love roses'' என டைப் செய்து தேடிய போது தளங்கள் 45,500 என்று காட்டப்பட்டது. மேற்கோள் குறிகளுக்குள் அமைக்கப்படுகையில் மிகச் சரியாக நாம் எந்த வரிசையில் சொற்களை அமைத்திருக்கிறோமோ அந்த வரிசையில் சொல் தொகுதிகள் இருந்தால் மட்டுமே தளங்கள் கணக்கில் எடுத்துக் கொள்ளப்பட்டு தரப்படுகின்றன.
1. சிறிய நட்சத்திரக் குறியீடு (*) : ஆங்கிலத்தில் wildcard என்று கூறப்படுவதனைக் குறிக்க சிறிய நட்சத்திரக் குறியீட்டினைப் பயன்படுத்துகிறோம். இதனை உங்கள் தேடுதல் பணியிலும் பயன்படுத்தலாம். குறிப்பிட்ட சொல்லினை அடுத்து எந்த சொல் இருந்தாலும் பரவாயில்லை என்று அமைக்க இந்த குறியீட்டினை அமைத்துத் தேடலாம். குறிப்பிட்ட சொல் தொகுதியில் ஒரே ஒரு சொல்லை மறந்துவிட்டு அதனைத் தேட முயற்சிக்கையில் இந்த வகை குறியீட்டுடனான தேடல் கை கொடுக்கிறது. எடுத்துக் காட்டாக காவிரியின் குறுக்கே உள்ள பாலம் என்பதனைக் குறிக்க "the bridge on the river cauvery'' என அமைக்கலாம். ஆனால் காவேரி என்பது மறந்துவிட்டால் the bridge on the river XXஎன அமைக்க முடியாது. அது தவறான தகவலையே தரும். இங்கே the bridge on the river * என அமைத்தால் காவேரி குறுக்கே உள்ள பாலங்கள் குறித்த தகவல் கிடைக்கும். இரண்டு சொற்கள் இருக்கலாம் என தேட விரும்பினால் "the bridge on the river ** ”என அமைக்கவும்.
2.குறிப்பிட்ட தளத்தில் மட்டும்: இணையத்தில் ஒரு குறிப்பிட்ட தளத்தில் மட்டும் நீங்கள் தேடும் தகவல் உள்ளது. ஆனால் அது எங்கே தரப்பட்டுள்ளது என்று நினைவில் இல்லை. அந்த தளத்தில் மட்டும் தேட விரும்பினால் "site:" என்று கொடுத்துத் தேடலாம். எடுத்துக் காட்டாக தமிழ் நாடு அரசின் இணைய தளமான www.tn.nic.in என்ற இணைய தளத்தில் மட்டும் ஞிச்ததிஞுணூதூ என்ற சொல் உள்ள இடங்களைப் பார்க்க விரும்பினால் "cauvery" site:www.tn.nic.in என டைப் செய்து கொடுத்து சரியான முடிவுகளைப் பெறலாம். இதே போல ஒரு குறிப்பிட்ட தளத்தில் நீங்கள் தேடும் தகவல் இருப்பது நன்றாக உங்களுக்குத் தெரியும். எனவே இந்த தளம் இல்லாமல் மற்ற தளங்களில் தேடித் தகவல்களை அறிய வேண்டும் என்றால் ஒரு கழித்தல் () அடையாளத்தை இங்கு ண்டிtஞு: ஆப்பரேட்டருக்கு முன்னால் பயன்படுத்த வேண்டும். எடுத்துக் காட்டாக காவேரி குறித்து தமிழ்நாடு அரசு தளம் தவிர மற்ற தளங்களில் தேட வேண்டும் என்றால் "cauvery" site:www.tn.nic.in எனக் கொடுத்துத் தேடலாம்.
3. அதே பொருள் தரும் சொற்களைத் தேட: ஒரு சொல்லின் பொருள் சார்ந்து இருக்கும் மற்ற சொற்களை அறிந்து கொள்ள கூகுள் தேடலை வரையறை செய்திடலாம். இதற்கு ஆங்கிலத்தில் tilde ("~") என அழைக்கப்படும் அடையாளக் குறியைப் பயன்படுத்த வேண்டும். இதனை நாம் தேடும் சொல்லின் முன்னால் எந்த இடைவெளியும் இன்றி அமைக்க வேண்டும். எடுத்துக் காட்டாக எம்.எஸ்.வேர்ட் தொகுப்பு குறித்து விளக்கமான குறிப்புகள் தரும் தளங்களைக் காண விரும்புகிறீர்கள். இந்த குறிப்புகள் என எப்படி வேண்டுமென்றாலும் இருக்கலாம். இவற்றைப் பெறும் வழியை இந்த ஆப்பரேட்டர் தருகிறது. இங்கு "msword" ~tutorial என அமைத்தால் எம்.எஸ் வேர்ட் குறித்த basics, hints, guide, tips, tricks ஆகிய அனைத்தும் கிடைக்கும்.
4.விளக்கங்கள் பெற: ஒரு சொல்லின் விளக்கங்கள் பெற கூகுள் "define" என்ற ஆப்பரேட்டரைக் கையாள்கிறது. எந்த சொல்லுக்கு விளக்கம் வேண்டுமோ அதன் முன்னால் இதனை இணைத்துச் செயல்படுத்தினால் போதும். எடுத்துக் காட்டாக nano technology என்ற சொல் குறித்து விளக்கம் வேண்டும் என்றால் define nano technology என்று கொடுத்தால் போதும். விளக்கத்தோடு இந்த சொல் பயன்படுத்தப்பட்டுள்ள அனைத்து தளங்களும் கிடைக்க what is nano technology எனக் கொடுத்துப் பெறலாம்.
TEN TIPS FOR Strengthening Perl Knowledge-1 :
1* use vars qw($VERSION $opt_h $opt_x $opt_n $..)
>> Make global variables ,same as using "our" before Variable Names..
2*What's the difference between dynamic and lexical (static) scoping? Between local() and my()?
local($x) saves away the old value of the global variable $x and assigns a new value for the
duration of the subroutine which is visible in other functions called from that subroutine. This is
done at run-time, so is called dynamic scoping. local() always affects global variables, also
called package variables or dynamic variables.
my($x) creates a new variable that is only visible in the current subroutine. This is done at
compile-time, so it is called lexical or static scoping. my() always affects private variables, also
called lexical variables or (improperly) static(ly scoped) variables.
For instance:
sub visible {
print "var has value $var\n";
}
sub dynamic {
local $var = 'local'; # new temporary value for the still-global
visible(); # variable called $var
}
sub lexical {
my $var = 'private'; # new private variable, $var
visible(); # (invisible outside of sub scope)
}
$var = 'global';
visible(); # prints global
dynamic(); # prints local
lexical(); # prints global
Notice how at no point does the value "private" get printed. That's because $var only has that
value within the block of the lexical() function, and it is hidden from called subroutine.
In summary, local() doesn't make what you think of as private, local variables. It gives a global
variable a temporary value. my() is what you're looking for if you want private variables.
See "Private Variables via my()" in perlsub and "Temporary Values via local()" in perlsub for
excruciating details.
3*How to embed data in your Perl program
Here's a sample Perl program that demonstrates how you can include (embed) data inside of
your Perl program, right in there next to the source code.
This simple program takes the data after the special __END__ tag, and makes it available to
your Perl source code.
#!/usr/bin/perl
while ()
{
print $_;
}
__END__
George Washington
Abraham Lincoln
John F. Kennedy
4*What is true and false in Perl?
Perl is a little unusual in not having true and false boolean operators. Because of this, and my
advancing age, I can never seem to remember what equates to true and false when using Perl,
so I decided to create this page.
True/false summary
In short, the following elements in Perl will equate to "false":
* The number zero (0) means false.
* The string zero ('0') means false.
* The empty string ('') means false.
Lots of other things equate to "true", including:
* Non-zero numbers
* Non-empty strings
5.Explain the difference between "my" and "local" variable scope declarations. ?
Both of them are used to declare local variables.
The variables declared with "my" can live only within the block it was defined and cannot get its
visibility inherited functions called within that block, but one defined with "local" can live within
the block and have its visibility in the functions called within that block.
6.E-val with its own interpreter >Make it Fail to Throw Errors..
The eval function provides a very simple way of checking certain events without
affecting the overall execution of your script.
In essence the eval function just initiates a new instance of the Perl interpreter in order to
evaluate a particular string or block.
Because eval evaluates a Perl statement or block within its own interpreter we can use it in
situations that might otherwise cause the Perl interpreter to fail.
This process works because an embedded eval block reports any errors raised by a call to die
through the $@ variable. In fact any exit is reported through eval to the $@ special variable. We
can demonstrate this with a simple eval block used to test the existence of a particular module:
eval
{
require Net::FTP;
}
print Error: Module failed to load ($@) if $@;
This outputs the following:
$ perl eval.pl
Failed to load Net::FTP: Can't locate Net/LICK.pm in @INC (@INC contains:
/usr/local/lib/perl5/5.6.0/i686-linux /usr/local/lib/perl5/5.6.0
/usr/local/lib/perl5/site_perl/5.6.0/i686-linux
/usr/local/lib/perl5/site_perl/5.6.0 /usr/local/lib/perl5/site_perl .) at
eval.pl line 1.
7. How to substitute a particular string in a file containing millions of records?
perl -pi -e "s/search_string/replace_string/g" SourceFile
-P
causes your program to be run through the C preprocessor before compilation by Perl.
(Because both comments and cpp directives begin with the # character, you should avoid
starting comments with any words recognized by the C preprocessor such as ``if'', ``else'', or
``define''.)
-i
-e commandline
may be used to enter one line of program. If -e is given, Perl will not look for a filename in the
argument list. Multiple -e commands may be given to build up a multi-line script. Make sure to
use semicolons where you would in a normal program.
-T
forces ``taint'' checks to be turned on so you can test them. Ordinarily these checks are
done only when running setuid or setgid. It's a good idea to turn them on explicitly for programs
that run on behalf of someone else whom you might not necessarily trust, such as CGI programs
or any internet servers you might write in Perl
8.What's the significance of @ISA, @EXPORT @EXPORT_O...??
@ISA -> each package has its own @ISA array. this array keep track of classes it is inheriting.
ex:
package child;
@ISA ( parentclass);
@EXPORT this array stores the subroutins to be exported from a module.
@EXPORT_OK this array stores the subroutins to be exported only on request.
9.Special Variables in Perl:
Go through This Link : Special Variables in Perl
10.where do you go for perl help?
You can use the command
-> perldoc -f function name
Ex: perldoc -f print
Shell Scripting Tip:
* Example for using getopts:
getopts => getting options to correct var..
while getopts t:r: MYOPTION
do
case MYOPTION in
t) RESTAURANTTYPE=$OPTARG ;;
r) RATING=$$OPTARG ;;
\?) echo "Sorry no such option, please try again"
exit 1
;;
done
// myprog -t North -r 35 or myprog -r 35 -t North
Tutorial for using getopts...[click]
>> Make global variables ,same as using "our" before Variable Names..
2*What's the difference between dynamic and lexical (static) scoping? Between local() and my()?
local($x) saves away the old value of the global variable $x and assigns a new value for the
duration of the subroutine which is visible in other functions called from that subroutine. This is
done at run-time, so is called dynamic scoping. local() always affects global variables, also
called package variables or dynamic variables.
my($x) creates a new variable that is only visible in the current subroutine. This is done at
compile-time, so it is called lexical or static scoping. my() always affects private variables, also
called lexical variables or (improperly) static(ly scoped) variables.
For instance:
sub visible {
print "var has value $var\n";
}
sub dynamic {
local $var = 'local'; # new temporary value for the still-global
visible(); # variable called $var
}
sub lexical {
my $var = 'private'; # new private variable, $var
visible(); # (invisible outside of sub scope)
}
$var = 'global';
visible(); # prints global
dynamic(); # prints local
lexical(); # prints global
Notice how at no point does the value "private" get printed. That's because $var only has that
value within the block of the lexical() function, and it is hidden from called subroutine.
In summary, local() doesn't make what you think of as private, local variables. It gives a global
variable a temporary value. my() is what you're looking for if you want private variables.
See "Private Variables via my()" in perlsub and "Temporary Values via local()" in perlsub for
excruciating details.
3*How to embed data in your Perl program
Here's a sample Perl program that demonstrates how you can include (embed) data inside of
your Perl program, right in there next to the source code.
This simple program takes the data after the special __END__ tag, and makes it available to
your Perl source code.
#!/usr/bin/perl
while (
{
print $_;
}
__END__
George Washington
Abraham Lincoln
John F. Kennedy
4*What is true and false in Perl?
Perl is a little unusual in not having true and false boolean operators. Because of this, and my
advancing age, I can never seem to remember what equates to true and false when using Perl,
so I decided to create this page.
True/false summary
In short, the following elements in Perl will equate to "false":
* The number zero (0) means false.
* The string zero ('0') means false.
* The empty string ('') means false.
Lots of other things equate to "true", including:
* Non-zero numbers
* Non-empty strings
5.Explain the difference between "my" and "local" variable scope declarations. ?
Both of them are used to declare local variables.
The variables declared with "my" can live only within the block it was defined and cannot get its
visibility inherited functions called within that block, but one defined with "local" can live within
the block and have its visibility in the functions called within that block.
6.E-val with its own interpreter >Make it Fail to Throw Errors..
The eval function provides a very simple way of checking certain events without
affecting the overall execution of your script.
In essence the eval function just initiates a new instance of the Perl interpreter in order to
evaluate a particular string or block.
Because eval evaluates a Perl statement or block within its own interpreter we can use it in
situations that might otherwise cause the Perl interpreter to fail.
This process works because an embedded eval block reports any errors raised by a call to die
through the $@ variable. In fact any exit is reported through eval to the $@ special variable. We
can demonstrate this with a simple eval block used to test the existence of a particular module:
eval
{
require Net::FTP;
}
print Error: Module failed to load ($@) if $@;
This outputs the following:
$ perl eval.pl
Failed to load Net::FTP: Can't locate Net/LICK.pm in @INC (@INC contains:
/usr/local/lib/perl5/5.6.0/i686-linux /usr/local/lib/perl5/5.6.0
/usr/local/lib/perl5/site_perl/5.6.0/i686-linux
/usr/local/lib/perl5/site_perl/5.6.0 /usr/local/lib/perl5/site_perl .) at
eval.pl line 1.
7. How to substitute a particular string in a file containing millions of records?
perl -pi -e "s/search_string/replace_string/g" SourceFile
-P
causes your program to be run through the C preprocessor before compilation by Perl.
(Because both comments and cpp directives begin with the # character, you should avoid
starting comments with any words recognized by the C preprocessor such as ``if'', ``else'', or
``define''.)
-i
-e commandline
may be used to enter one line of program. If -e is given, Perl will not look for a filename in the
argument list. Multiple -e commands may be given to build up a multi-line script. Make sure to
use semicolons where you would in a normal program.
-T
forces ``taint'' checks to be turned on so you can test them. Ordinarily these checks are
done only when running setuid or setgid. It's a good idea to turn them on explicitly for programs
that run on behalf of someone else whom you might not necessarily trust, such as CGI programs
or any internet servers you might write in Perl
8.What's the significance of @ISA, @EXPORT @EXPORT_O...??
@ISA -> each package has its own @ISA array. this array keep track of classes it is inheriting.
ex:
package child;
@ISA ( parentclass);
@EXPORT this array stores the subroutins to be exported from a module.
@EXPORT_OK this array stores the subroutins to be exported only on request.
9.Special Variables in Perl:
Go through This Link : Special Variables in Perl
10.where do you go for perl help?
You can use the command
-> perldoc -f function name
Ex: perldoc -f print
Shell Scripting Tip:
* Example for using getopts:
getopts => getting options to correct var..
while getopts t:r: MYOPTION
do
case MYOPTION in
t) RESTAURANTTYPE=$OPTARG ;;
r) RATING=$$OPTARG ;;
\?) echo "Sorry no such option, please try again"
exit 1
;;
done
// myprog -t North -r 35 or myprog -r 35 -t North
Tutorial for using getopts...[click]
Tuesday, May 26, 2009
SHELL SCRIPTING-II:
1. FILE DESCRIPTORS:
File descriptors are called file handles .
The three files opened for each command along with their corresponding file descriptors are
lStandard Input (STDIN), 0
lStandard Output (STDOUT), 1
lStandard Error (STDERR), 2
Ex:
command > file 2>&1
list > file 2>&1
Here STDOUT (file description 1) and STDERR (file descriptor 2) are redirected into the specified file.
Nohup process 2>&1& , 2=>STDERR
Command >file 2>&1
2> Output Error Msgs
1> Standard ERR
command 1> file1 2> file2
Here the STDOUT of the specified command is redirected to file1, and the STDERR (error messages) to file2.
NOTE:
n>&m
Here n and m are file descriptors (integers). If you let n=2 and m=1, you see that STDERR is redirected to
STDOUT. By redirecting STDOUT to a file, you also redirect STDERR
2. Shell Version of Cat:
#!/bin/sh
if [ $# -ge 1 ] ; then
for FILE in $@
do
exec 5<&0 < "$i"
while read LINE ; do echo $LINE ; done
exec 0<&5 5<&-
done
fi
3. Listing By Last Modifed :
start with ls -1 command again, but this time I'll use the -t (sort by last modified time) option
instead:
$ ls -1t /var/spool/mail
4. Shell function Returns Operating System..
getOSName() {
case ´uname -s´ in
*BSD)
echo bsd ;;
SunOS)
case ´uname -r´ in #version
5.*) echo solaris ;;
*) echo sunos ;;
esac
;;
Linux)
echo linux ;;
HP-UX)
echo hpux ;;
AIX)
echo aix ;;
*)
echo unknown ;;
esac
}
5. Copy Side Effects::
(Use Tar. )
Major prolem: can copy only Regular Files…… (files like file@ nt copied)
There are two other minor problems with using cp:
l Some versions of the cp command do not copy a file's owner and group. With these versions of cp,
the copied file has a different owner and group than the original.
l Some versions of cp do not copy a file's permissions correctly. With such a version of cp, the copied file might have different permissions than the original.
Using tar
By using tar you can move directories using the following procedure:
1. Make a tar file of the source directory.
2. Change to the destination directory.
3. Extract the source directory in the destination directory.
6. More n More –Magic of bin/sh:
When you ask a shell to execute the command $ date, the shell uses the system call exec to
ask the UNIX kernel to execute the command you requested.
The shell passes the name of the command that should be executed to the exec system call.
This system call reads the first two characters in a file to determine how to execute the command.
In the case of shell scripts, the first two characters are #!, indicating that the script needs to be interpreted by another program instead of executed directly.
The rest of the line is treated as the name of the interpreter to use.
Usually the interpreter is /bin/sh, but you can also specify options to the shell on this line.
Friday, May 15, 2009
Wednesday, May 13, 2009
My PERL HINTS
@array = (1, 2, 3);
print (@array, "\n");
This prints the following on your screen:
123
To get around this problem, put the array you want to print into a string:
print ("@array\n");
$scalar = @array;
($scalar) = @array;
In the first statement, the length of the list in @array is
assigned to $scalar. In the second statement, the first
element of @array is assigned to $scalar
while ($count <= @array) {
@slice = @array[1,2,3];
Although single elements of an array are referenced
using the $ character, array slices are referenced using
@:
$var = $array[0];
@subarray = @array[0,1];
The array-slice notation provides an easy way to swap elements in a list. The following
is an example:
@array[1,2] = @array[2,1];
This statement swaps the second and third elements of @array. As with the overlapping
array slices you saw earlier, the Perl interpreter copies @array[2,1] into a temporary locn.
@array =; // each line as element..
unless (open(OUTFILE, ">outfile")) { // opposite to IF.
7: die ("cannot open output file outfile\n");
8: }
the print statement sends output to the standard output file, which means
that it sends the output to the file associated with STDOUT
print STDOUT ("Here is a line of output.\n");
***** it reads a line of input from file1 because the file variable STDIN represents file1.
Similarly, specifying > on the command file redirects the standard output file from the
screen to the specified file
myperlprogoutfile
FILES:
-e exists, -r file permission, -w
$list = <>;
is called myprog and is called using the command
$ myprog file1 file2 file3
In this case, the first occurrence of the <> operator reads the first line of input from
file1. Successive occurrences of <> read more lines from file1. When file1 is exhausted,
<> reads the first line from file2, and so on. When the last file, file3, is exhausted, <>
returns an empty string, which indicates that all the input has been read.
In Perl, the <> operator actually contains a hidden reference to the array @ARGV. Here's
how it works:
1. When the Perl interpreter sees the <> for the first time, it opens the file whose
name is stored in $ARGV[0].
2. After opening the file, the Perl interpreter executes the following library
function:
shift(@ARGV);
This library function gets rid of the first element of @ARGV and moves every other
element over one. This means that element x of @ARGV becomes element x-1.
3. The <> operator then reads all of the lines of the file opened in step 1.
4. When the <> operator exhausts an input file, the Perl interpreter goes back to
step 1 and repeats the cycle again
PATTERNS:
===============
(0[1-9]|[12]\d|3[01]),
===> 0[1-9] =>01,02,03....09
|[12] => 1/2
\d =>any single digit(0..9)
RE=> 0 to 31..
Perl 5 makes life a little easier by supplying the x option. This tells the Perl interpreter
to ignore white space in a pattern unless it is preceded by a backslash.
( 0[13578] | 1[02] )\2( 0[1-9] | [12]\d|3[01] )
\2 ==> Separator.....l \2, which matches the character that separates the day, month, and year
m!/u/jqpublic/perl/prog1! ===> Gud .... instead of /\u\/j....
m can be used if / is not used pattern match....
here ! we used... we can use anything other than / wid m...
\d Any digit [0-9]
\D Anything other than a digit [^0-9]
\w Any word character [_0-9a-zA-Z]
\W Anything not a word
character
[^_0-9a-zA-Z]
\s White space [ \r\t\n\f]
\S Anything other than white
space
[^ \r\t\
g Match all possible patterns
i Ignore case
m Treat string as multiple
lines
o Only evaluate once
s Treat string as single line
x Ignore white space in
pattern
/\d{2} ([\W]) \d{2} \1 \d{2}/x
s/(\d+)/($1)/g ==> puts Paranthesis..
s/\bno\b/NO/gi => no to NO,No to NO,nO to NO.
$string =~ /abc/$var/o; ===> o --->only once.
$string = "This is a\ntwo-line string.";
$string =~ s/a.*o/one/s;
# $string now contains "This is one-line string."
If the m option is specified, ^ and $ match the beginning and end of any line.
$string = "The The first line\nThe The second line";
$string =~ s/^The//gm;
For example, the following substitution
replaces all occurrences of the words no, No, NO, and nO with NO. (Recall that the \b
escape character specifies a word boundary.)
$string =~ s/\Btwo\B/one/ s; // s----> treat as single line
Replacement Using an Expression
The e option treats the replacement string as an expression, which it evaluates before
replacing. For example, consider the following:
$string = "0abc1";
$string =~ s/[a-zA-Z]+/$& x 2/e // replacemt =>evaluated b4 replacing.
$string = "This is a\n two -lintwoe string.";
$string =~ s/\Btwo\B/That x 2/es; ==> Replaces only lintwoe ... linthatthate.
$string =~ s/\btwo\b/That x 2/es; ==>replaces only two.
if ($string =~/(\d)+\.(\d)+.(\d)+.(\d)+/ ) // IP Address :-)
[a-zA-Z][a-zA-Z0-9_.]+@[a-zA-Z]+\.com|net|org|edu // E-mail Addr.
/([\da-z]+)([:;])\1\2\1/ [] =>pattern memory \1,\2,\3... so on..
~ tr/0-9/ /s all occurences of number wid space..
/[a-z]+/i
/(?i)[a-z]+/
In both cases, the pattern matches one or more alphabetic characters; the i option
indicates that case is to be ignored when matching.
The syntax for embedded pattern options is
(?option)
where option is one of the options shown in Table 7.7.
Table 7.7. Options for embedded patterns.
Option Description
i Ignore case in pattern
m Treat pattern as multiple lines
s Treat pattern as single line
x Ignore white space in pattern
If you want to enclose a subpattern in parentheses without storing it in
memory, use the ?: extended pattern-matching feature. For example, consider this
pattern:
/(?:a|b|c)(d|e)f\1/
Perl 5 enables you to use the ?= feature to define a boundary condition that must be
matched in order for the pattern to match. For example, the following pattern matches
abc only if it is followed by def:
/abc(?=def)/
Similarly, the ?! feature defines a negative look-ahead condition, which is a boundary
condition that must not be present if the pattern is to match
@list = (1, 2, 3, 4, 5);
foreach $temp (@list) {
if ($temp == 2) {
$temp = 20;
}
}
In this loop, when $temp is equal to 2, $temp is reset to 20. Therefore, the list stored in
the array variable @list becomes (1, 20, 3, 4, 5).
Use this feature with caution, because it is not obvious that the value of @list has
changed.
Perl enables you to tell the Perl interpreter to restart an iteration of a loop using the
redo statement.
Like last and next, the syntax for the redo statement is simple:
// redo possible in for loop but not in =============>>>> do ..while loop/until LOOPs.
next LABEL;
redo LABEL;
you can define statements that are to be executed whenever the end of a
while loop or an until loop is reached. To carry out this task, specify a continue
statement after the loop.
$i = 1;
while ($i <= 10) {
print ("$i\n");
}
continue {
$i++;
}
print (@array, "\n");
This prints the following on your screen:
123
To get around this problem, put the array you want to print into a string:
print ("@array\n");
$scalar = @array;
($scalar) = @array;
In the first statement, the length of the list in @array is
assigned to $scalar. In the second statement, the first
element of @array is assigned to $scalar
while ($count <= @array) {
@slice = @array[1,2,3];
Although single elements of an array are referenced
using the $ character, array slices are referenced using
@:
$var = $array[0];
@subarray = @array[0,1];
The array-slice notation provides an easy way to swap elements in a list. The following
is an example:
@array[1,2] = @array[2,1];
This statement swaps the second and third elements of @array. As with the overlapping
array slices you saw earlier, the Perl interpreter copies @array[2,1] into a temporary locn.
@array =
unless (open(OUTFILE, ">outfile")) { // opposite to IF.
7: die ("cannot open output file outfile\n");
8: }
the print statement sends output to the standard output file, which means
that it sends the output to the file associated with STDOUT
print STDOUT ("Here is a line of output.\n");
***** it reads a line of input from file1 because the file variable STDIN represents file1.
Similarly, specifying > on the command file redirects the standard output file from the
screen to the specified file
myperlprog
FILES:
-e exists, -r file permission, -w
$list = <>;
is called myprog and is called using the command
$ myprog file1 file2 file3
In this case, the first occurrence of the <> operator reads the first line of input from
file1. Successive occurrences of <> read more lines from file1. When file1 is exhausted,
<> reads the first line from file2, and so on. When the last file, file3, is exhausted, <>
returns an empty string, which indicates that all the input has been read.
In Perl, the <> operator actually contains a hidden reference to the array @ARGV. Here's
how it works:
1. When the Perl interpreter sees the <> for the first time, it opens the file whose
name is stored in $ARGV[0].
2. After opening the file, the Perl interpreter executes the following library
function:
shift(@ARGV);
This library function gets rid of the first element of @ARGV and moves every other
element over one. This means that element x of @ARGV becomes element x-1.
3. The <> operator then reads all of the lines of the file opened in step 1.
4. When the <> operator exhausts an input file, the Perl interpreter goes back to
step 1 and repeats the cycle again
PATTERNS:
===============
(0[1-9]|[12]\d|3[01]),
===> 0[1-9] =>01,02,03....09
|[12] => 1/2
\d =>any single digit(0..9)
RE=> 0 to 31..
Perl 5 makes life a little easier by supplying the x option. This tells the Perl interpreter
to ignore white space in a pattern unless it is preceded by a backslash.
( 0[13578] | 1[02] )\2( 0[1-9] | [12]\d|3[01] )
\2 ==> Separator.....l \2, which matches the character that separates the day, month, and year
m!/u/jqpublic/perl/prog1! ===> Gud .... instead of /\u\/j....
m can be used if / is not used pattern match....
here ! we used... we can use anything other than / wid m...
\d Any digit [0-9]
\D Anything other than a digit [^0-9]
\w Any word character [_0-9a-zA-Z]
\W Anything not a word
character
[^_0-9a-zA-Z]
\s White space [ \r\t\n\f]
\S Anything other than white
space
[^ \r\t\
g Match all possible patterns
i Ignore case
m Treat string as multiple
lines
o Only evaluate once
s Treat string as single line
x Ignore white space in
pattern
/\d{2} ([\W]) \d{2} \1 \d{2}/x
s/(\d+)/($1)/g ==> puts Paranthesis..
s/\bno\b/NO/gi => no to NO,No to NO,nO to NO.
$string =~ /abc/$var/o; ===> o --->only once.
$string = "This is a\ntwo-line string.";
$string =~ s/a.*o/one/s;
# $string now contains "This is one-line string."
If the m option is specified, ^ and $ match the beginning and end of any line.
$string = "The The first line\nThe The second line";
$string =~ s/^The//gm;
For example, the following substitution
replaces all occurrences of the words no, No, NO, and nO with NO. (Recall that the \b
escape character specifies a word boundary.)
$string =~ s/\Btwo\B/one/ s; // s----> treat as single line
Replacement Using an Expression
The e option treats the replacement string as an expression, which it evaluates before
replacing. For example, consider the following:
$string = "0abc1";
$string =~ s/[a-zA-Z]+/$& x 2/e // replacemt =>evaluated b4 replacing.
$string = "This is a\n two -lintwoe string.";
$string =~ s/\Btwo\B/That x 2/es; ==> Replaces only lintwoe ... linthatthate.
$string =~ s/\btwo\b/That x 2/es; ==>replaces only two.
if ($string =~/(\d)+\.(\d)+.(\d)+.(\d)+/ ) // IP Address :-)
[a-zA-Z][a-zA-Z0-9_.]+@[a-zA-Z]+\.com|net|org|edu // E-mail Addr.
/([\da-z]+)([:;])\1\2\1/ [] =>pattern memory \1,\2,\3... so on..
~ tr/0-9/ /s all occurences of number wid space..
/[a-z]+/i
/(?i)[a-z]+/
In both cases, the pattern matches one or more alphabetic characters; the i option
indicates that case is to be ignored when matching.
The syntax for embedded pattern options is
(?option)
where option is one of the options shown in Table 7.7.
Table 7.7. Options for embedded patterns.
Option Description
i Ignore case in pattern
m Treat pattern as multiple lines
s Treat pattern as single line
x Ignore white space in pattern
If you want to enclose a subpattern in parentheses without storing it in
memory, use the ?: extended pattern-matching feature. For example, consider this
pattern:
/(?:a|b|c)(d|e)f\1/
Perl 5 enables you to use the ?= feature to define a boundary condition that must be
matched in order for the pattern to match. For example, the following pattern matches
abc only if it is followed by def:
/abc(?=def)/
Similarly, the ?! feature defines a negative look-ahead condition, which is a boundary
condition that must not be present if the pattern is to match
@list = (1, 2, 3, 4, 5);
foreach $temp (@list) {
if ($temp == 2) {
$temp = 20;
}
}
In this loop, when $temp is equal to 2, $temp is reset to 20. Therefore, the list stored in
the array variable @list becomes (1, 20, 3, 4, 5).
Use this feature with caution, because it is not obvious that the value of @list has
changed.
Perl enables you to tell the Perl interpreter to restart an iteration of a loop using the
redo statement.
Like last and next, the syntax for the redo statement is simple:
// redo possible in for loop but not in =============>>>> do ..while loop/until LOOPs.
next LABEL;
redo LABEL;
you can define statements that are to be executed whenever the end of a
while loop or an until loop is reached. To carry out this task, specify a continue
statement after the loop.
$i = 1;
while ($i <= 10) {
print ("$i\n");
}
continue {
$i++;
}
Subscribe to:
Posts (Atom)