Thursday, September 22, 2011

getopts in perl

my %options=();
getopts("p:a:r:", \%options);

# test for the existence of the options on the command line.
# in a normal program you'd do more than just print these.
print "-p $options{p}\n" if defined $options{p};
print "-a $options{a}\n" if defined $options{a};
print "-r $options{r}\n" if defined $options{r};

Saturday, September 17, 2011

Find command examples


To exclude  directories while searching using find command
For example i want to prevent the find command from moving into the directories hhh and ggg while searching in my home directory

use this example:

find /home/karthik -name hhh -prune -name ggg -prune -o -type f

This will display all files in  my other directories in my home except moving into my ggg and hhh directories

To grep mutiple pattern in the find command

find /home/karthik -type f  | xargs egrep  "(hello)\|(great)|(awesome)"
In this case i am trying to search multiple word hello,great,awesome in the files searched by the find command.

To serach only in the current directory without moving into the sub-directories use:

find  /home/karthik maxdepth -1 -type f

Here all the files in the current directory is displayed without searching for files in any of the sub-directories
max-depth sets the maximum directory level the search can traverse.

To set the directory levels for searching the files you can use:
maxdepth and mindepth options

mindepth -to set the minimum directory level

maxdepth-to set the maximum directory level

In case if you want to search in sub-directory level 2 to 3
Set it as
set the  mindepth as 2 and maxdepth as 3.

find /home/karthik -mindepth 2 -maxdepth 3 -type f

It will search all the the files  from the second directory level to the third directory level.i.e
for example it searches in /home/karthik/fff/   or /home/karthik/fff/kkkk  and not in /home/karthik


To find only ASCII textfiles in your directory excluding all other binary files  use this:

find /home/karthik -type f |xargs file |grep -i ASCII | cut -d: f1

This uses file command which displays the type of file. And grep only text files from it and by cut command displays the filename from the search.


To find files which belong to a specific user :
Use this

find /home/karthik   -user  divya

In this case the find command searches for all the files which belong to divya in the /home/karthik directory.


To find files which is of specific size:


find /home/karthik  -type f  -size  +100c

Here it will search for all the files above 100 bytes

+100c  here   + - more than
If you want files less than 100 bytes specify it as -100c

If you want files of exact size of 100 bytes keep it as 100c


To find empty files in your directory:
for this use -empty option


find /home/karthik -type f -empty

This will list all the empty files in this directory.

To list all the non-empty files you can simply do:


find /home/karthik -type f   ! -empty

"!" just reverses the meaning of -empty

To find all the files of a provided permission


find /home/karthik -perm 777

It list all the files with the permission 777,
if you provide +444  it lists all the files above this permission.

To search in multiple directories you can use this:


find /home/karthik/ /tmp/ -type f

To search in multiple directory simply put it as said before.This will search in multiple directory /home/karthik/ and /tmp/


To grep pattern in the find list:

find /home/karthik/ -type f | xargs grep "hello"
This "xargs grep hello" command searches for hello across all the files returned by the "find /home/karthik/ -type -f"








 

Friday, September 16, 2011

find command complex pattern exclusion


For complex expression you need to include the pattern inside the brackets \( and \)

For example:

find /home/karthik  -name "*.pl" -o -name "*.pm" -o ! \( -name "*.*" -o -type d \)

Include:
1)include the files ending with .pl and .pm

Exclude:
1)exclude the files ending with any other pattern  than the specified "pl" and "pm".
2) to exclude the directories






Tuesday, September 13, 2011

Automating a telnet to a remote system using expect script and running a application

This program i have written to telnet to a remote system and run a applet in that remote system.And exit from the remote host once the expect script receives "zzz" from the applet.


!/usr/bin/expect
log_file -a /tmp/expectlog     <---all the interactions in the remote system are saved in this file
set timeout 60
spawn telnet   122.23.33.33
expect "login:"
send "xxxxx\n"                     <-----username
expect "Password:"         
send "fdfdfdffd\n"                 <-----password                 
expect "xxxxx"                       <---prompt expected after login
send "\n"
send "appletviewer  applet.html\n"
interact -o "zzz" return            <-----to come out of the interact mode once the expect receives "zzz" from the application
send "exit\r"                            <------to exit from the telnet session
expect eof {puts "finished "}    <------expecting eof when the spawn process completes
exit 0                                      <-----ending the expect script

Tuesday, September 6, 2011

Converting lowercase to uppercase and uppercase to lowercase in Shell


Simple use  truncate  in Shell

ddd="HELLO"
For converting uppercase to lowercase use  tr '[A-Z]' '[a-z]'
echo $ddd | tr '[A-Z]' '[a-z]'    will convert  uppercase into lowercase

For converting lowercase to uppercase use tr '[a-z]' '[A-Z]'
echo $ddd | tr '[a-z]' '[A-Z]'