Tuesday, April 27, 2010

Per/Shell Scripting : Braindump ~ tips/tricks

Note: These are braindumps !

While learning any topic I make notes, at first they are very basic and progressively they get advanced.

Don't expect super wisdom ("Gyan") out of these.
As I said just braindumps 8-)

If you have some tips you'd like to share, post in comments and I'll include them here for everybody's benefit.


Perl/Shell Scripting

Get yesterdays date via perl

ret=`perl -e '$d = time(); $d-=86400; @f = localtime($d); printf "%04d%02d%02d\n", $f[5]+1900, $f[4]+1, $f[3];'`

echo "Date from perl is : $ret"

Simple String concatenation example in perl -

String concatenation in ksh

m1='xx'

m2='YY'

combined="$m1$m2"

length of string

length($line)

Good link for perl - Clear & concise perl

Get todays date & current time
$time_v = time;
($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isd) = localtime($time_v);
($day, $month, $year) = (localtime)[3,4,5];

Check if the line is not a comment & is not 0 length
if( (!($line =~ m/^#/)) && (length($line) > 10) )

Split line with delimeter ":"
@cArr = split(/:/, $line);

Passing variables between shell scripts (ksh) & Perl

# typeset -x MY_VARIABLE="SomeValue"

# perl -e 'print $ENV{"MY_VARIABLE"}'

As seen here - http://www.unix.com/shell-programming-scripting/44882-passing-value-shell-perl.html

More info. about typeset - http://www.docs.hp.com/en/B2355-90046/ch23s06.html

perl date time object info.

http://datetime.perl.org/index.cgi?FAQBasicUsage

SED - sed one liners that'll save your life 8-) - Link

No comments:

Post a Comment