Monday, August 31, 2009

Accessing database Using Shell Script

Here i m going to tell u how can u access the database (mysql,Oracle ) using shell script in linux..

for mysql..

u need to install mysql on your linux distros..

after that as usual write a shell script to access database in user interactive mode..

running script example is...

#!/bin/bash
1 read -p "enter mysql user name:" name
2 stty -echo #echo off
3 read -p "enter mysql passwd :" password
4 stty echo #echo on
5 mysql -u $name -p$password<<END
6 use databasename;
7 select * from tablename
8 END

it asks user for mysql user name and password..then may asks for some pattern,input to search in database and finish with the desired result to the user.

NOTE: If there is no user name and password set for mysql..then skip first for lines of script and replace fifth line with

mysql <<END

also note that there is no space between -p and password provided by the user.



For Oracle10g XE(Express Edition) :


you need to install Oracle10g XE on your linux distros..

you may refer this link to installing on ubuntu..

if you are ready with your installation..
after that as usual write a shell script to access database in user interactive mode..

https://help.ubuntu.com/community/Oracle10g

running script example is...

#!/bin/bash
1 read -p "enter oracle user name:" name
2 stty -echo #echo off
3 read -p "enter mysql passwd :" password
4 stty echo #echo on
5 sqlplus $name/$password<<END
6 select * from tablename;
7 END

it asks user for oracle user name ( generally system )and password..then may asks for some pattern,input to search in database and finish with the desired result to the user.

Thank you............

Finding common text or difference in two files

Here I want to discuss the command or single liners for finding the words
common in two given text files or their difference .Difference what mean here is that such words which are present in one file but not in other just analogous to the set operation difference.

now coming to the procedure..

Although there is inbuilt command in unix for doing this, but i has the limitation that..

1.)input files must be sorted.
2.)it gives the lines common to both not words..(not sure..u can check it)

the command is comm file1 file1

it's output is 3 column output..
1st column gives the file1-file2
2nd column gives the file2-file1
3rd column gives the common of file1 and file2

you can get only what u want by suppressing the option such as

using comm -12 file1 file2

it gives the lines common to both files.

CAUTION: It Requires the input file must contain the lines in sorted order.otherwise it will give unpredictable output.

now my answer is..

using cat and grep command...

grep -o "`cat file2`" file1
or
cat file1 | grep -o "`cat file2`"

2nd one is better in case of searching lines.
as it gives output as line wise.first one gives all matching lines in a single line.

string within quote makes content of file2 as searching pattern for file1.
-o option is set for printing only the matching word as output.

if u want the common lines in the two files use -x option of grep.

for finding the difference use -v option .
for finding the words ignoring the case use -i option of grep.

now, If u don't want to use cat in creating pattern.u can directly use -f option of grep command to provide pattern from a file.

grep -of file1 file2

is same as above command .all other option follows with it.

NOTE : one thing u should keep in mind while using -f option is that it must be last option. as above..
e.g. grep -fo file1 file2 will not work for the same.

It overcomes all the limitations of the comm command in unix.
i.e.
1.)It don't require input files in sorted order.
2.)matching words can be found not only lines.

I hope u will like it..

Feel free to comment ..if u feel any difficulty in understanding..

Thanks..
Enjoy Scripting............

Thursday, July 30, 2009

Problems...

hii Guyss..
This post is not related to shell scripting..
but i wanna say u in his post that..if u have any problem in shell scripting..
Plzzz let us know ..we contributor of this blog will try to answer ur problem
Attach ur problem as comment to this post......

Qs are welcome.....

Thank you...

Wednesday, July 29, 2009

How can I see the O/P in a another Terminal?

If I wrote a script in a new Terminal. Then after I want to see the output of
that script in an another terminal. How Can I See that?

I have solution but if any one have direct command for it pl suggest me


You can find the name of the other terminal

i.e by
tty command you can see the name of the terminal

now you redirect the output to that terminal

tty
/dev/pts/0

tty
/dev/pts/1

echo "amit khatri">/dev/pts/1
you see the result on New terminal

Thursday, July 16, 2009

see the difference...

hii frnds..
now a interesting thing about the buffer used at the terminal on unix..

suppose the following C code is running on any unix system..

$ cat test.c

#include
int main()
{
printf("Hello\n");
if(fork()==0)
printf("World\n");
return 0;
}

$ gcc test.c
$ ./a.out
Hello
World
$ ./a.out>test
Hello
World
Hello
$

why output is different in two cases..

It is the difference between the buffer used for terminal standard output and the other ordinary file..if output is to be be shown on the terminal line buffer is used i.e. when it encounters '\n' it flushes out buffer content..but in case of other file buffer content is flushed out only when it is full..in the above code..that's why when child is run Hello is retained there in buffer and Hello is printed twice in case when output is redirected to file.But when it runs directly Hello is printed only one time..as there is '/n' which flushes out the buffer content.So Hello is printed only when parent is running..when '\n' is ommited..output will be same in both cases.Try it yourself.

Thank you...

Sunday, June 14, 2009

vowel count in a file ..........

A script count the number of occurance of vowels in particular file

i.e how Many times a vowel come in the file ....

grep -io [aeiouAeIOU] |sort|uniq -c

alphabetical order words...

Hiii Friends..

This time i have come with a prob..

Prob is : You have to print the all worlds in a file that are in increasing order..
i.e. abcd.. qrstz.. but it can't be like dcefgh...because c followed by d contradict the rules.

Thak you In Advance....

Saturday, June 6, 2009

a useful single liner for windows user.....

hiiii folks...

some time u have been facing prob in windows that due to some virus aur intruders attack there is an .exe file created in every folder with the same name as the folder..

it is very cumbersome to delete all those files manually...

my suggestion is if ur system is dual boot system..
1.) goto ur linux box..
2.) mount ur windows drives..
3.) become root user with su on fedora,redhat,mandriva or sudo su on ubuntu..
3.) run thd following command...
find / -name "*.exe" -print | awk -F"/" '{split($NF,a,"."); if($(NF-1)==a[1]){ print $0}}'

it will print all those files on your system any where which has the same name as its folder name.

4.) after inspecting the output if u r satisted ..
5.) run again the following command

find / -name "*.exe" -print | awk -F"/" '{split($NF,a,"."); if($(NF-1)==a[1]){ print $0}}' | xargs rm -f

friends u will not believe !.. it will wipe out ur all such files on your system in one go..

I think it will be useful..

Enjoy Linux !!...
if you find it is useful plzzzzzz do comment on this post !!...

single liner to calculate the sum of digits of a number...

let us suppose no. is 12647424
answer should be 1+2+6+4+7+4+2+4=30

logic is inferred from above line..

first number is converted .. e.g.

287 is converted as 2 + 8 + 7
and now apply expr commad on above pattern u will get answer..

now how to convert 287 to 2 + 8 + 7

echo $num | sed -e 's/[0-9]/ + &/g' -e 's/^ +//'`

will convert 287 to 2 + 8 + 7

now simply use expr over the above string...

expr `echo $num | sed -e 's/[0-9]/ + &/g' -e 's/^ +//'`

is it not amazing... free from looping construct....(as every one does...)
enjoy !!....

cool ways to count no. occourences of each words in a file....

Here i m giving ways to count frequency of each words in file..i.e. how many times a word is found...

1.)

awk '{ for(i=1;i<=NF;i++){a[$i]++;}} END{for(i in a) print i" "a[i]}' filename

filename must be raplaced by ur desired filename.

2.)

cat filename | tr '[ ]+' '\n' | sort | uniq -c

logic is convert all spaces n tabs to newline.
this will convert all words in a file to single line.
now sort the output..
find unique word with their count with -c option of uniq.

i m searching for other method too..
if u found then let the others know..

enjoy scripting.....