java program to validate email address

java program to validate email address

import java.util.regex.*;
import java.io.*;

class test
{
public static void main(String args[])
{
System.out.println("enter email address to check valid or not :-)\n");
try
{
DataInputStream ds=new DataInputStream(System.in);
Pattern p = Pattern.compile("[a-z]+.*[a-z]*@[a-z]*.[[com][co.in]]+");
Matcher m =p.matcher(ds.readLine());

if(m.matches())
{
System.out.println("\nTHIS E-MAIL ADDRESS IS VALID");
}
else
{
System.out.println("\nTHIS E-MAIL ADDRESS IS INVALID");

}

}
catch(Exception e){}
}
}

gtu operating system practicle

gtu operating system practical

A ) Accept numbers and perform addition, subtraction, division and multiplication.

ANS:

[mca09113@ALS ~]$ cat f5.sh

a=10

echo "Enter The Value of A: "

read a

b=10

echo "Enter The Value of B: "

read b

c=0

c=`expr $a + $b`

echo "Addition " $c

c=`expr $a - $b`

echo "Subtraction " $c

c=`expr $a / $b`

echo "Division " $c

c=`expr $a \* $b`

echo "Multiplication " $c

OUTPUT:

[mca09113@ALS ~]$ sh f5.sh

Enter The Value of A:

20

Enter The Value of B:

10

Addition 30

Subtraction 10

Division 2

Multiplication 200

B ) Accept the string and checks whether the string is palindrome or not.

ANS:

[mca09113@ALS ~]$ cat f6.sh

l=0

cnt=1

tag=0

echo "Enter a String?"

read str

l=`echo $str |wc -c`

l=`expr $l - 1`

lh=`expr $l / 2`

while [ $cnt -le $lh ]

do

c1=`echo $str|cut -c$cnt`

c2=`echo $str|cut -c$l`

if [ $c1 != $c2 ]

then

tag=1

fi

cnt=`expr $cnt + 1`

l=`expr $l - 1`

done

if [ $tag -eq 0 ]

then

echo "String is Palindrome"

else

echo "String is not Palindrome"

fi

OUTPUT:

[mca09113@ALS ~]$ sh f6.sh

Enter a String?

hemant

String is not Palindrome

[mca09113@ALS ~]$ sh f6.sh

Enter a String?

ava

String is Palindrome



C ) .Accept number and check the number is even or odd, finds the length of the number, sum of the

digit in the number.

ANS:

[mca09113@ALS ~]$ cat f7.sh

echo "Enter number:"

read no

c=`expr $no % 2`

if [ $c -eq 0 ]

then

echo "No is even "

else

echo "No is odd"

fi

cnt=`echo $no | wc -c`

cnt=`expr $cnt - 1`

echo "word count =" $cnt

i=1

while [ $i -le $cnt ]

do

j=`echo $no | cut -c $i`

k=`expr $k + $j`

i=`expr $i + 1`

done

echo "total =" $k

OUTPUT:

[mca09113@ALS ~]$ sh f7.sh

Enter number:

123

No is odd

word count = 3

total = 6

d ). Accept strings and replace a string by another string.

ANS:

echo "Enter any string:"

read str

echo "Your string is :" $str

echo "Enter word that your what replace with other word"

read oldstr

echo "Enter new string"

read newstr

str1=`echo $str | sed s/$oldstr/$newstr/g`

str2=`echo $str | replace $oldstr $newstr`

echo "after replacing " $str1

echo "after replacing " $str2

OUTPUT:

[mca09113@ALS ~]$ sh f8.sh

Enter any string:

hemant

Your string is : hemant

Enter word that your what replace with other word

t

Enter new string

tfofandi

after replacing hemantfofandi

e). Accept filename and displays last modification time if file exists, otherwise display appropriate message.

ANS:

[mca09113@ALS ~]$ cat f9.sh

echo "Enter file name"

read fnm

if [ ! -f $fnm ]

then

echo "File Not Found"

exit 1

fi

dt=`date -r $fnm`

echo "modification date is:" $dt

OUTPUT:

[mca09113@ALS ~]$ sh f9.sh

Enter file name

f6.sh

modification date is: Tue Dec 7 15:24:23 IST 2010


F ). Fetch the data from a file and display data into another file in reverse order.

echo "enter file name for fatching data and store in other file"

ANS:

[mca09113@ALS ~]$ cat f10.sh

echo "enter a file name to do operation "

read fnm

if [ ! -f $fnm ]

then

echo "File not found"

exit 1

fi

echo "enter new file name:"

read newfile

while read -n1 char;

do

str=$char$str

done<$fnm

echo $str>$newfile

echo $newfile "successfully created on" `pwd` " path"

OUTPUT:

[mca09113@ALS ~]$ sh f10.sh

enter a file name to do operation

f6.sh

enter new file name:

hello.txt

hello.txt successfully created on /home/PARAM/mca09113 path




GTU 3:

Write a script to find the global complete path for any file.

ANS:

[mca09113@ALS ~]$ cat gtu3.sh

echo -e "Enter the filename to search in current directory: \c"

read FILE

args=`find . -name $FILE | xargs`

for i in $args

do

if [ -f "$i" ]; then

CPATH=`readlink -f "$i"`

echo $CPATH

fi

done

noargs=${#args}

if [ "$noargs" -eq "0" ]; then

echo "No such a file exists"

fi

OUTPUT:

[mca09113@ALS ~]$ sh gtu3.sh

Enter the filename to search in current directory: gtu3.sh

/home/PARAM/mca09113/gtu3.sh

[mca09113@ALS ~]$ sh gtu3.sh

Enter the filename to search in current directory: f10.sh

/home/PARAM/mca09113/f10.sh

GTU 4:

Write a script to broadcast a message to a specified user or a group of users logged on any terminal

ANS:

[mca09113@ALS ~]$ cat gtu4.sh

who > e.txt

echo "For Broadcasting Messages."

if [ $# = 0 ]

then

echo "Enter the User Name:\c"

read nam

p=`grep $nam e.txt`

if [ "$p" != "" ]

then

write $nam

rm e.txt

exit

else

echo "The user is not logged in"

fi

rm e.txt

exit

fi

if [ "$1" = "all" ]

then

wall

fi

for nam in $*

do

p=`grep $nam e.txt`

if [ "$p" != "" ]

then

write $nam

else

echo "The User $nam is not logged in"

fi

done

OUTPUT:

[mca09113@ALS ~]$ sh gtu4.sh

For Broadcasting Messages.

Enter the User Name:\c

mca09112

The user is not logged in

[mca09113@ALS ~]$ sh gtu4.sh

For Broadcasting Messages.

Enter the User Name:\c

mca09113

Message from mca09113@ALS on pts/2 at 13:49 ...

hi

hi

hello

hello

GTU 5:

Write a script to copy the file system from two directories to a new directory in such a way that only the latest file is copied in case there are common files in both the directories.

ANS:

[mca09113@ALS ~]$ cat gtu5.sh

EXIT=n

while [ $EXIT != y ]

do

sleep 1

echo -e "\n"

echo -e " 1. Display PWD

2. Long Listing

3. Change Directory

4. Copy Newest File.

5. Exit

Enter Choice: \c"

read ch

case $ch in

1)

clear

pwd

;;

2)

clear

pwd

ls -l

;;

3)

echo -n "Enter Absolute Path to change directory: "

read apath

cd $apath

if [ $? -eq 0 ]; then

clear

echo "Working Directory Changed successfully to.."

sleep 1

pwd

else

clear

echo "Please check your PATH."

fi

;;

4)

clear

echo "Enter filenames to copy. ( * - for ALL Files, ELSE Separate files by spaces )"

read allfiles

if [ -f $allfiles ]; then

echo "Enter Absolute path, where to copy these files: "

read -e cpath

if [ -d $cpath ]; then

cp -u "$allfiles" $cpath

else

echo "There is no such a directory!"

fi

else

echo "There is/are no such file(s)!"

fi

;;

5)

clear

echo -n "Exiting.."

sleep 1

echo -n "."

sleep 1

echo -n "."

clear

exit

;;

*)

clear

echo "Invalid Choice"

;;

esac

done

i

OUTPUT:

[mca09113@ALS ~]$ sh gtu5.sh

1. Display PWD

2. Long Listing

3. Change Directory

4. Copy Newest File.

5. Exit

Enter Choice:

GTU 6:

wite a script to compare identically named files in two different directories and if they are same, copy one of them in a third directory.

ANS:

[mca09113@ALS ~]$ cat gtu6.sh

echo Enter the name of 1st directory

read dir1

echo Enter the name of 2nd directory

read dir2

echo Enter the name of the directory in which you want to copy the duplicate file

read dir3

cd `find /home/PARAM/mca09113 -type d -name $dir1`

for fl1 in *

do

echo $fl1

cd `find /home/PARAM/mca09113 -type d -name $dir2`

for fl2 in *

do

if [ $fl1 == $fl2 ]

then

cd ..

cp ./$dir2/$fl2 ./$dir3

cd `find /home/PARAM/mca09113 -type d -name $dir2`

rm $fl2

cd `find /home/PARAM/mca09113 -type d -name $dir1`

rm $fl1

fi

done

cd ..

mv ./temp1/* ./$dir2/$fl2

done

OUTPUT:

[mca09113@ALS ~]$ sh gtu6.sh

Enter the name of 1st directory

f1.sh

Enter the name of 2nd directory

f8.sh

Enter the name of the directory in which you want to copy the duplicate file

office

college

cp: cannot stat `./f8.sh/college': No such file or directory

rm: cannot remove `college': Is a directory

rm: cannot remove `college': Is a directory

mv: cannot stat `./temp1/*': No such file or directory

GTU 7

Write a script to delete zero sized files from a given directory .

ANS:

[mca09113@ALS ~]$ cat gtu7.sh

while true; do

read -e -p "Enter the Absolute Path: " path || exit

[[ -d $path ]] && break

echo "Invalid Directory!"

done

args=`find "$path" -empty -print0 | xargs -0`

for i in $args

do

if [ -f "$i" ]; then

rm -i "$i"

fi

done

OUTPUT:

[mca09113@ALS ~]$ sh gtu7.sh

Enter the Absolute Path: /home/param/mca09113

Invalid Directory!

GTU 8:

Write a script to display the name of those files (in the given directory) which are having multiple links

ANS:

[mca09113@ALS ~]$ cat g8.sh

echo "Enter Absolute path of directory"

read path

if test -d $path; then

cd $path

for i in *

do

for j in *

do

if test "$i" != "$j"; then

if test "$i" -ef "$j"; then

echo "$i" >> $$.temp

fi

fi

done

done

cat $$.temp | uniq

rm $$.temp

cd -

else

echo "Check your path."

fi

OUTPUT:

[mca09113@ALS ~]$ sh g8.sh

Enter Absolute path of directory

/home/param/mca09113

Check your path.

GTU 9:

Write a script to display the name of all executable files in the given directory.

ANS:

[mca09113@ALS ~]$ cat g9.sh

i=0

ls > test1

set `wc -l test1`

l=$1

while [ $l -ne 0 ]

do

line=`head -$l test1 | tail -1`

if [ -x $line ]

then

i=`expr $i + 1`

fi

l=`expr $l - 1`

done

echo -e "\t\t\tTotal no. of executable files : "

echo $i

OUTPUT:

[mca09113@ALS ~]$ sh g9.sh

Total no. of executable files : 4

GTU 10:

Write a script to display the date, time and a welcome message (like Good Morning etc.). The time should be displayed with “a.m.” or “p.m.” and not in 24 hours notation.

ANS:

[mca09113@ALS ~]$ cat g10.sh

time=`date +%H%M%S`

d1=`date +%d/%m/%y`

t1=`date +%I:%M:%S`

echo "date:"$d1

if [ $time -ge 000000 -a $time -lt 120000 ]

then

echo "time:"$t1" a.m"

else

echo "time:"$t1" p.m"

fi

if [ $time -lt 120000 -a $time -gt 60000 ]

then

echo "GooD MorninG"

else

if [ $time -eq 120000 ]

then

echo "GooD NooN"

else

if [ $time -gt 120000 -a $time -lt 180000 ]

then

echo "GooD AfterNooN"

else

if [ $time -gt 180000 -a $time -lt 210000 ]

then

echo "GooD EveninG"

else

echo "GooD NighT"

fi

fi

fi

fi

OUTPUT:

[mca09113@ALS ~]$ sh g10.sh

date:08/12/10

time:03:09:03 p.m

GooD AfterNooN

GTU 11:

Write a script to display the directory in the descending order of the size of each file.

ANS:

[mca09113@ALS ~]$ cat g11.sh

echo -n "Enter directory name : "

read dname

ls $dname > file

echo "No of files in directory : $(grep [^*$] file-c)"

rm -f file

ls | sort -r find . -size +1000c -print

or

echo Enter Directory

read direc

cd $direc

ls -Sl

OUTPUT:

[mca09113@ALS ~]$ sh g11.sh

Enter directory name : hm

grep: file-c: No such file or directory

No of files in directory :

sort: invalid option -- e

Try `sort --help' for more information.

g11.sh: line 12: or: command not found

Enter Directory

hm1

total 20

drwxr-xr-x 3 mca09113 Domain Users 4096 Aug 13 07:37 hm

drwxr-xr-x 2 mca09113 Domain Users 4096 Aug 13 08:12 myhome

-rw-r--r-- 1 mca09113 Domain Users 26 Aug 9 10:37 homead1.txt

-rw-r--r-- 1 mca09113 Domain Users 26 Aug 9 10:31 homeadd1.txt

-rw-r--r-- 1 mca09113 Domain Users 26 Aug 9 09:23 homeadd.txt

GTU 12:

Write a script to implement the following commands:

Tree (of DOS)

which (of UNIX)

ANS:

[mca09113@ALS ~]$ cat 12g.sh

echo "Enter Dir Name for Structure .."

read dir1

for i in `find $dir`

do

if [ -d $i ]

then

echo "+" $i ;

elif [ -f $i ]

then

echo "-----" $i

fi

done

OUTPUT:

[mca09113@ALS ~]$ sh 12g.sh

Enter Dir Name for Structure ..

college

+ .

GTU 13:

Write a script for generating a mark sheet after reading data from a file.

File contains student roll no, name , marks of three subjects.

ANS:

count=1

total=0

per=0

rem1=0

wc -l s > temp

echo "************************************************************"

>>s.out

echo " Students Result Are As Follows....."

>> s.out

echo "************************************************************"

>> s.out

echo "Rno NAME SUB1 SUB2 SUB3 TOTAL PERCENTAGE GRADE RESULT"

>> s.out

echo "************************************************************"

>> s.out

while [ $count -le `awk '{print $1}' temp` ]

do

let total=0

head -$count s > temp1

tail -1 temp1 > temp2

rollno=`awk '{print $1}' temp2`

name=`awk '{print $2}' temp2`

sub1=`awk '{print $3}' temp2`

sub2=`awk '{print $4}' temp2`

sub3=`awk '{print $5}' temp2`

let total=$total+`awk '{print $3}' temp2`

let total=$total+`awk '{print $4}' temp2`

let total=$total+`awk '{print $5}' temp2`

echo "STUDENT NAME IS :" `awk '{print $2}' temp2`

echo "TOTAL IS :" $total

let per=`expr $total / 3`

let rem1=`expr $total % 3`

if test $rem1 -ne 0

then

let per=$per+1

fi

echo "PERCENTAGE IS :" $per

if [ $per -ge 70 ]

then

grade="A+"

elif test $per -ge 60

then

grade="A"

elif test $per -ge 50

then

grade="B"

elif test $per -ge 45

then

grade="C"

fi

echo "GRADE IS :" $grade

let count=$count+1

echo $rollno $name " " $sub1 " " $sub2 " " $sub3 " " $total "

" $per " " $grade

>> s.out

echo "-----------------------------------------------------"

>> s.out

done

rm temp

rm temp1

rm temp2

OUTPUT:

GTU 14:

Write a script to make following file and directory management operations menu based:

Display current directory, List directory, Make directory, Change directory, Copy a file , Rename a file , Delete a file , Edit a file

ANS:

[mca09113@ALS ~]$ cat g14.sh

clear

choice=y

while [ "$choice" = "y" ]

do

echo " MAIN MENU "

echo "***************************"

echo "1 - DISPLAY CURRENT DIRECTORY"

echo "2 - LIST DIRECTORY"

echo "3 - MAKE DIRECTORY"

echo "4 - CHANGE DIRECTORY"

echo "5 - COPY A FILE"

echo "6 - RENAME A FILE"

echo "7 - DELETE A FILE"

echo "8 - EDIT A FILE"

echo "9 - EXIT"

echo "ENTER THE CHOICE :- "

read choice

case $choice in

1) echo "DISPLAYING CURRENT DIRECTORY"

pwd

;;

2) echo "\nLIST THE CURRENT DIRECTORY"

ls -l

read

;;

3) echo "\nMAKE A NEW DIRECTORY"

echo "\nENTER THE DIRECTORY NAME :-\c"

read dirname

mkdir "$dirname"

read dirname

;;

4) echo "\nCHANGE THE CURRENT DIRECTORY"

echo "\nENTER THE DIRECTORY TO WHICH YOU WANT TO

read dirname

pwd

cd ..

;;

5) echo "\nCOPY THE GIVEN FILE TO THE GIVEN DESTINAT

echo "\nENTER THE FILE TO COPY :- \c"

read file

echo "\nENTER THE DESTINATION TO COPY :- \c"

read destin

cp "$file" "$destin"

;;

6) echo "\nRENAMING THE GIVEN FILE"

echo "\nENTER THE FILE TO RENAME :- \c"

read file1

echo "\nENTER THE NEW NAME :- \c"

read file2

mv "$file1" "$file2"

;;

7) echo "\nDELETING THE GIVEN FILE"

echo "\nENTER THE FILE TO DELETE :- \c"

read file

rm "$file"

;;

8) echo "\nEDITING A GIVEN FILE"

echo "\nENTER FILE TO EDIT :- \c"

read file

vi "$file"

:q

;;

9) exit

;;

*) echo "Invalid Choice ....."

;;

esac

echo -e "Do u want to continue.....? [y/n]"

read choice

case $choice in

Y|y) choice=y;;

N|n) choice=n;;

*) choice=y;;

esac

done

OUTPUT:

[mca09113@ALS ~]$ sh g14.sh

MAIN MENU

***************************

1 - DISPLAY CURRENT DIRECTORY

2 - LIST DIRECTORY

3 - MAKE DIRECTORY

4 - CHANGE DIRECTORY

5 - COPY A FILE

6 - RENAME A FILE

7 - DELETE A FILE

8 - EDIT A FILE

9 - EXIT

ENTER THE CHOICE :-

1

DISPLAYING CURRENT DIRECTORY

/home/PARAM/mca09113

Do u want to continue.....? [y/n]

GTU 15:

Write a script which reads a text file and output the following

Count of character, words and lines.

File in reverse.

Frequency of particular word in the file.

Lower case letter in place of upper case letter.

ANS:

[mca09113@ALS ~]$ cat g15.sh

echo -e "\n Enter Filename : "

read filen

echo "--------------------------------------------------------------"

echo " MENU "

echo "--------------------------------------------------------------"

echo " a) Count the no. of char.s, words, lines."

echo " b) Display a file in reverse."

echo " c) Count the no. of occurrences of a particular word."

echo " d) Convert Lower case to UppeCase"

echo "--------------------------------------------------------------"

echo -e "\n Enter Your Choice : "

read c

case "$c" in

a) echo -e "\n Total lines,words,char.s " ; wc $filen ;;

b) rev $filen

read buf;;

c) echo -e "\nEnter word to find :"

read w

for i in `cat $filen`

do

echo $i >> f1.txt

done

echo -e "Total no. of words= \c" ;grep -c $w f1.txt

grep $w f1.txt

rm f1.txt;;

*) echo "Invalid choice"

esac

OUTPUT:

[mca09113@ALS ~]$ sh g15.sh

Enter Filename :

gtu7.sh

--------------------------------------------------------------

MENU

--------------------------------------------------------------

a) Count the no. of char.s, words, lines.

b) Display a file in reverse.

c) Count the no. of occurrences of a particular word.

d) Convert Lower case to UppeCase

--------------------------------------------------------------

Enter Your Choice :

a

Total lines,words,char.s

12 47 254 gtu7.sh

GTU 16:

Write a shell script to check whether the named user is currently logged in or not

ANS:

clear

echo -e "Enter The User name : "

read name

(who | grep $name ) &&

clear

echo "The User " $name &&

echo " Is Currently Logged In " || echo "is logged out"

OUTPUT:

[mca09113@ALS ~]$ sh g16.sh

Enter The User name :

mca09113

mca09113 pts/3 2010-12-08 15:01 (10.9.3.8)

The User mca09113

Is Currently Logged In

GTU 17:

Write a Script for Simple Database Management System Operation.

Database File Contains Following Fields.

EMP_NO ,EMP_NAME, EMP_ADDRESS, EMP_AGE, EMP_GENDER, EMP_DESIGNATION, EMP_BASIC_SALARY, Provide Menu Driven Facility For, VIEW RECORD BASED ON QUERY, ADD RECORD, DELETE RECORD, MODIFY RECORD., COUNT TOTAL NUMBER OF RECORDS EXIT

ANS:

[mca09113@ALS ~]$ cat 17g.sh

clear

i="y"

echo "Enter name of database "

read db

while [ $i = "y" ]

do

clear

echo "1.View the DataBase "

echo "2.View Specific Records "

echo "3.Add Records "

echo "4.Delete Records "

echo "5.Exit "

echo "Enter your choice "

read ch

case $ch in

1)cat $db;;

2)echo "Enter id :"

read id

l=`wc -l $db`

j=1

while [ $l -gt $j ]

do

$dbid=`cat $db | cut -f1 -d"," | head -n $i | tail -n 1`

if [ $id = $dbid ]

then

echo `head -n $i | tail -n 1`

break

fi

done

;;

3)echo "Enter new std id "

iread tid

echo "Enter new name:"

read tnm

echo "Enter designation "

read des

echo "Enter college name"

read college

echo "$tid $tnm $des $college">>$db;;

4)echo "Delete Records Where Id is"

read id

grep -v "$id" $db >dbs1

echo "Record is deleted"

cat dbs1;;

5)exit;;

*)echo "Invalid choice ";;

esac

echo "Do u want to continue ?"

read i

if [ $i != "y" ]

then

exit

fi

done

OUTPUT:

[mca09113@ALS ~]$ sh 17g.sh

Enter name of database

hm1

1.View the DataBase

2.View Specific Records

3.Add Records

4.Delete Records

5.Exit

Enter your choice

1

cat: hm1: Is a directory

Do u want to continue ?

GTU 18:

Write A Script To Perform Following String Operations Using Menu:

COMPARE TWO STRINGS.

JOIN TWO STRINGS.

FIND THE LENGTH OF A GIVEN STRING.

OCCURRENCE OF CHARACTER AND WORDS

E. REVERSE THE STRING.

ANS:

[mca09113@ALS ~]$ cat g18.sh

while true

do

echo ---------------------------------------

echo 1...COMPARE TWO STRINGS

echo 2...JOIN TWO STRINGS

echo 3...FIND THE LENGTH OF A GIVEN STRING

echo 4...OCCURRENCE OF CHARACTER AND WORDS

echo 5...REVERSE THE STRING

echo ---------------------------------------

echo Enter Choice:

read ch

echo ---------------------------------------

case $ch in

1)

echo Enter String1:

read str1

echo Enter String2:

read str2

if [ $str1 = $str2 ]

then

echo String is equal

else

echo String is not equal

fi

;;

2)

echo Enter String1:

read str1

echo Enter String2:

read str2

str3=$str1$str2

echo Join String: $str3

;;

3)

length=0

echo Enter String1:

read str1

length=`expr $str1 | wc -c`

length=`expr $length - 1`

echo Length: $length

;;

4)

echo Enter String:

read str

echo Enter Word to find

read word

echo $str | cat > str1.txt

grep -o $word str1.txt | cat > str2.txt

count=`grep -c $word str2.txt`

echo Count: $count

;;

5)

echo Enter String1:

read str

len=`expr $str | wc -c`

len=`expr $len - 1`

while [ $len -gt 0 ]

do

rev=`expr $str | cut -c $len`

ans=$ans$rev

len=`expr $len - 1`

done

echo Reverse String: $ans

;;

*)

echo Wrong Choice

;;

esac

echo Do u want to continue?

read ans

if [ $ans = n ]

then

exit

fi

done

OUTPUT:

[mca09113@ALS ~]$ sh g18.sh

---------------------------------------

1...COMPARE TWO STRINGS

2...JOIN TWO STRINGS

3...FIND THE LENGTH OF A GIVEN STRING

4...OCCURRENCE OF CHARACTER AND WORDS

5...REVERSE THE STRING

---------------------------------------

Enter Choice:

1

---------------------------------------

Enter String1:

jay somnath

Enter String2:

jay somnath

g18.sh: line 22: [: too many arguments

String is not equal

Do u want to continue?

n

GTU 19:

Write a script to calculate gross salary for any number of employees

Gross Salary =Basic + HRA + DA.

HRA=10% and DA= 15%.

ANS:

OUTPUT:

GTU 20:

Write a script to check whether a given string is palindrome or not.

ANS:

[mca09113@ALS ~]$ cat g20.sh

echo Enter String

read str

length=0

cnt=1

flag=0

length=`expr $str | wc -c`

length=`expr $length - 1`

temp=`expr $length / 2`

while test $cnt -le $temp

do

rev=`expr $str | cut -c $cnt`

ans=`expr $str | cut -c $length`

if [ $rev != $ans ]

then

flag=1

fi

cnt=`expr $cnt + 1`

length=`expr $length - 1`

done

if [ $flag -eq 0 ]

then

echo String is palindrome

else

echo String is not palindrome

fi

OUTPUT:

[mca09113@ALS ~]$ sh g20.sh

Enter String

jay somnath

expr: syntax error

String is palindrome

GTU 21:

Write a script to check whether a given number is palindrome or not.

ANS:

[mca09113@ALS ~]$ cat g21.sh

clear

echo "ENTER NUMBER :="

read number

rem=0

i=10

revnumber=0

for((;number>0;))

do

let revnumber=" revnumber * 10 "

let rem=" number % 10 "

let number=" number / 10 "

let revnumber=" revnumber + rem "

done

echo -e "\nREVERSE NUMBER :-\c" & echo $revnumber

OUTPUT:

[mca09113@ALS ~]$ sh g21.sh

ENTER NUMBER :=

12345

REVERSE NUMBER :-54321

GTU 22:

Write a script to display all words of a file in ascending order

ANS:

[mca09113@ALS ~]$ cat g22.sh

echo " Enter File Name"

read fname

sort $fname

or

echo -e "\n enter File name "

read fn

for i in `cat $fn`

do

echo $i >> f2.txt

done

sort f2.txt

rm f2.tx

OUTPUT:

[mca09113@ALS ~]$ sh g22.sh

Enter File Name

GTU 23:

Write a script to display all lines of a file in ascending order.

ANS:

echo " Enter File Name"

read fname

sort $fname

OUTPUT:

[mca09113@ALS ~]$ sh g23.sh

Enter File Name

myfile.txt

hi friends

how are you

thankyou

GTU 24:

Write a script to display the last modified file

ANS:

while true;

do

read -e -p "Enter Directory: " path || exit

[[ -d $path ]] && break

echo "Invalid Path, Try Again!"

done

cd $path

ls * -dpltr | grep -v '/$' | tail -n1

cd $OLDPWD

OUTPUT:

[mca09113@ALS ~]$ sh g24.sh

Enter Directory: hm1

-rw-r--r-- 1 mca09113 Domain Users 26 Aug 9 10:37 homead1.txt

GTU 25:

Write a shell script to add the statement #include at the beginning of every C source file in current directory containing printf and fprintf.

ANS:

OUTPUT:

GTU 26:

Write a script that behaves both in interactive and non-interactive mode. When no arguments are supplied, it picks up each C program from current directory and lists the first 10 lines. It then prompts for deletion of the file. If the user supplies arguments with the script, then it works on those files only

ANS:

[mca09113@ALS ~]$ cat g26.sh

N=$#

ext=C

if test "$N" -eq "0"; then

while true; do

read -e -p "Enter Path: " path || exit

[[ -d $path ]] && break

echo "Invalid Path, Try Again!"

done

path=${path%/}

for i in $path/*.C

do

if [ "$i" != $path/'*.C' ]; then

mv "$i" "${i/.C/}".c

clear

fi

done

for i in $path/*."$ext"

do

if [ "$i" != "$path"/'*.c' ]; then

clear

echo "File is $i"

head -n10 "$i" | nl

sleep 1

rm -i "$i"

else

echo "There are no matching \"C\" files to Prompt in this directory."

sleep 2

clear=no

fi

done

if test "$clear" != "no"; then

clear

echo "Remaining C files in the Directory..."

ls -1 $path/*.c

fi

else

for i in $path/*.C

do

if [ "$i" != $path/'*.C' ]; then

mv "$i" "${i/.C/}".c

clear

fi

done

for i in $*

do

clear

i="${i/.c/}"

i="$(pwd)/$i.c"

if [ -f "$i" ]; then

echo "File name is $i"

head -n10 "$i" | nl

sleep 1

rm -i "$i"

else

echo "There is no such a file with name: \"$i\" in current working directoy"

sleep 3

fi

done

clear

echo "Remaining C files in the Directory..."

ls -1 *.c

sleep 1

fi

OUTPUT:

[mca09113@ALS ~]$ sh g26.sh

Enter Path: hm1

File is hm1/*.C

head: cannot open `hm1/*.C' for reading: No such file or directory

rm: cannot lstat `hm1/*.C': No such file or directory

Remaining C files in the Directory...

hm1/t1.c


JAVA GTU PROGRAMMING

JAVA GTU PROGRAMMING


Write a simple java application to print a pyramid with 5 lines. The first line has one character, 2nd

line has two characters and so on. The character to be used in the pyramid is taken as a command

line argument




package gtu;

class printd

{

public void print(String ch)

{

for(int i=0;i<5;i++)

{

for(int k=5;k>i;k--)

System.out.print(" ");

for(int j=0;j

{

System.out.print(ch+" ");

}

System.out.println(" ");

}

}

}

class gtu_2

{

public static void main(String args[])

{

try

{

printd p=new printd();

p.print(args[0]);

}

catch(ArrayIndexOutOfBoundsException e)

{

System.out.println("caught " +e);

System.out.println("renter the character");

}

}

}




Write a Java application which takes several command line arguments, which are supposed to be

names of students and prints output as given below:

(Suppose we enter 3 names then output should be as follows):

Number of arguments = 3

1: First Student Name is =Tom

2: Second Student Name is =Dick

3: Third Student Name is =Harry

Hint: An array may be used for converting from numeric values from 1 to 20 into String.





package gtu;

class gtu_3

{

public static void main(String args[])

{

String arr[] = {"First", "Second", "Third", "Fourth", "Fifth",

"Sixth", "Seventh", "Eight", "Nineth","tenth",

"eleventh","twelth","thirteenth","fourteenth"};

for(int i=0;i

{

System.out.println((i+1)+": "+ arr[i]+" Student Name is = "+args[i]);

}

}

}






Write a class, with main method, which declares floating point variables and observe the output of

dividing the floating point values by a 0, also observe the effect of assigning a high integer value

(8 digits and above) to a float and casting it back to int and printing.






package gtu;

class gtu_4

{

public static void main(String args[])

{

float test=12.0f;

int test2=123456789;

System.out.println("float value divide by zero : "+test/0);

float test3=(int)test2; //narowing conversion

System.out.println("assigning integer to float : "+test3);

int test4;

test4=(int)test3;

System.out.println("casting float to integer : "+test4);

}

}




Write a class called Statistics, which has a static method called average,

which takes a onedimensional array for double type, as parameter,

and prints the average for the values in the array.

Now write a class with the main method, which creates a two-dimensional

array for the four weeks of a month, containing minimum temperatures for

the days of the week(an array of 4 by 7), and uses the average method of the

Statistics class to compute and print the average temperatures for

the four weeks.




package gtu;

import java.io.*;

class Statistics

{

public static void average(double arr[])

{

double avg=0;

for(int i=0;i

{

avg=avg+arr[i];

}

System.out.println("average of the array"+ avg/(arr.length));

}

}

class gtu_5 extends Statistics

{

public static void main(String args[])

{

DataInputStream ds=new DataInputStream(System.in);

double temp[][]=new double[4][7];

double week[]=new double[7];

int i,j,k;

String data;

try{

for(i=0;i<4;i++)

{

for(j=0;j<7;j++)

{

System.out.print("Enter " + (i+1) +"th Week and " +(j+1)+"th day: ");

data=ds.readLine();

temp[i][j]=Double.parseDouble(data);

}

}

for(i=0;i<4;i++)

{

for(j=0;j<7;j++)

{

}

average(temp[i]);

}

}

catch(Exception e)

{

System.out.println("exception caught");

}

}

}






Define a class called Product, each product has a name, a product code and manufacturer name.

Define variables, methods and constructors, for the Product class. Write a class called Test

Product, with the main method to test the methods and constructors of the Product class.






class Product

{

int pCode;

String pName;

String mName;

Product(int pc)

{

this.pCode=pc;

}

Product(int pc,String s1)

{

this.pCode=pc;

this.pName=s1;

}

Product(int pc,String s1,String s2)

{ this.pCode=pc;

this.pName=s1;

this.mName=s2;

}

}

class testPrduct

{

public static void main(String args[])

{

System.out.println("constructor of peoduct with pc");

Product p1=new Product(001);

System.out.println("constructor of peoduct with pc and pn");

Product p2=new Product(001,"laptop");

System.out.println("constructor of peoduct with pc,pn and mn");

Product p3=new Product(001,"laptop","lenovo");

}

}





Define a class called Cartesian Point, which has two instance variables, x and y. Provide the

methods get X() and get Y() to return the values of the x and y values respectively, a method called

move() which would take two integers as parameters and change the values of x and y respectively,

a method called display() which would display the current values of x and y. Now overload the

method move() to work with single parameter, which would set both x and y to the same values, .

Provide constructors with two parameters and overload to work with one parameter as well. Now

define a class called Test Cartesian Point, with the main method to test the various methods in the

Cartesian Point class.






package gtu;

import java.io.*;

class CP

{

int x,y;

public CP(int x,int y)

{

this.x=x;

this.y=y;

}

public CP(int a)

{

x=y=a;

}

int getx()

{

return x;

}

int gety()

{

return y;

}

void move(int x,int y)

{

this.x=x;

this.y=y;

}

void display()

{

System.out.println("Current value of x : "+getx());

System.out.println("Current value of y : "+gety());

}

void move(int a)

{

x=y=a;

}

}

class p7

{

public static void main(String arg[])

{

CP cp1 = new CP(5,25);

System.out.println("\n\nDefault Values with "+5+" And "+25);

cp1.display();

System.out.println("After Function move with two arguments is called,");

cp1.move(11,25);

cp1.display();

System.out.println("After Function move with one arguments is called,");

cp1.move(95);

cp1.display();

System.out.println("-------------------------------------");

CP cp2 = new CP(15);

System.out.println("\n\nDefault Values with"+15);

cp2.display();

System.out.println("After Function move with two arguments is called,");

cp2.move(35,65);

cp2.display();

System.out.println("After Function move with one arguments is called,");

cp2.move(105);

cp2.display();

}

}






Define a class called Triangle, which has constructor with three parameters, which are of type

Cartesian Point, defined in the exercise 7. Provide methods to find the area and the perimeter of

the Triangle, a method display() to display the three Cartesian Points separated by ':' character, a

method move() to move the first Cartesian Point to the specified x, y location, the move should

take care of relatively moving the other points as well, a method called rotate, which takes two

arguments, one is the Cartesian Point and other is the angle in clockwise direction. Overload the

move method to work with Cartesian Point as a parameter. Now define a class called Test Triangle

to test the various methods defined in the Triangle class. Similarly also define a class called

Rectangle which has four Cartesian Point.






package gtu;

class Triangle

{

CartesianPoint P1;

CartesianPoint P2;

CartesianPoint P3;

Triangle()

{

}

Triangle(CartesianPoint p1, CartesianPoint p2, CartesianPoint p3)

{

P1=p1;

P2=p2;

P3=p3;

}

void display()

{

System.out.println("Cartesian Point P1.X: "+P1.x);

System.out.println("Cartesian Point P1.Y: "+P1.y);

System.out.println("Cartesian Point P2.X: "+P2.x);

System.out.println("Cartesian Point P2.Y: "+P2.y);

System.out.println("Cartesian Point P3.X: "+P3.x);

System.out.println("Cartesian Point P3.Y: "+P3.y);

}

void move()

{

P1.x=P1.x+10;

P1.y=P1.y+10;

P2.x=P2.x+10;

P2.y=P2.y+10;

P3.x=P3.x+10;

P3.y=P3.y+10;

}

void rotate(CartesianPoint P,int degree)

{

int x,y;

x=(int)(P.x * Math.cos(degree) - P.y * Math.sin(degree));

y=(int)(P.x * Math.sin(degree) + P.y * Math.cos(degree));

P.x=x;

P.y=y;

}

public String toString()

{

System.out.println("Cartesian Point P1.X: "+P1.x);

System.out.println("Cartesian Point P1.Y: "+P1.y);

System.out.println("Cartesian Point P2.X: "+P2.x);

System.out.println("Cartesian Point P2.Y: "+P2.y);

System.out.println("Cartesian Point P3.X: "+P3.x);

System.out.println("Cartesian Point P3.Y: "+P3.y);

return "";

}

boolean equals(Triangle t)

{

boolean f;

if(P1.x==t.P1.x && P1.y==t.P1.y && P2.x==t.P2.x && P2.y==t.P2.y && P3.x==t.P3.x && P3.y==t.P3.y )

f=true;

else

f=false;

return f;

}

}

class Rectangle

{

CartesianPoint P1;

CartesianPoint P2;

CartesianPoint P3;

CartesianPoint P4;

Rectangle()

{

}

Rectangle(CartesianPoint p1, CartesianPoint p2, CartesianPoint p3,CartesianPoint p4)

{

P1=p1;

P2=p2;

P3=p3;

P4=p4;

}

void display()

{

System.out.println("Cartesian Point P1.X: "+P1.x);

System.out.println("Cartesian Point P1.Y: "+P1.y);

System.out.println("Cartesian Point P2.X: "+P2.x);

System.out.println("Cartesian Point P2.Y: "+P2.y);

System.out.println("Cartesian Point P3.X: "+P3.x);

System.out.println("Cartesian Point P3.Y: "+P3.y);

System.out.println("Cartesian Point P4.X: "+P4.x);

System.out.println("Cartesian Point P4.Y: "+P4.y);

}

void move()

{

P1.x=P1.x+15;

P1.y=P1.y+15;

P2.x=P2.x+15;

P2.y=P2.y+15;

P3.x=P3.x+15;

P3.y=P3.y+15;

P4.x=P4.x+15;

P4.y=P4.y+15;

}

void rotate(CartesianPoint P,int degree)

{

int x,y;

x=(int)(P.x * Math.cos(degree) - P.y * Math.sin(degree));

y=(int)(P.x * Math.sin(degree) + P.y * Math.cos(degree));

P.x=x;

P.y=y;

}

public String toString()

{

System.out.println("Cartesian Point P1.X: "+P1.x);

System.out.println("Cartesian Point P1.Y: "+P1.y);

System.out.println("Cartesian Point P2.X: "+P2.x);

System.out.println("Cartesian Point P2.Y: "+P2.y);

System.out.println("Cartesian Point P3.X: "+P3.x);

System.out.println("Cartesian Point P3.Y: "+P3.y);

System.out.println("Cartesian Point P4.X: "+P4.x);

System.out.println("Cartesian Point P4.Y: "+P4.y);

return "";

}

boolean equals(Rectangle t)

{

boolean f;

if(P1.x==t.P1.x && P1.y==t.P1.y && P2.x==t.P2.x && P2.y==t.P2.y && P3.x==t.P3.x && P3.y==t.P3.y && P4.x==t.P4.x && P4.y==t.P4.y )

f=true;

else

f=false;

return f;

}

}

class GTU8

{

public static void main(String arg[])

{

CartesianPoint P1=new CartesianPoint(10,20);

CartesianPoint P2=new CartesianPoint(20,20);

CartesianPoint P3=new CartesianPoint(30,20);

Triangle t=new Triangle(P1,P2,P3);

System.out.println("\n\n\t\tTriangle\n\n");

t.display();

System.out.println("\n\nOverload toString method\n\n");

System.out.println(t);

System.out.println("\nEach Cartesian Point is move 10 relatively\n");

t.move();

t.display();

System.out.println("\nEach Cartesian Point is rotating 90 degree");

t.rotate(P1,10);

t.rotate(P2,90);

t.rotate(P3,90);

t.display();

P1=new CartesianPoint(10,20);

P2=new CartesianPoint(40,30);

P3=new CartesianPoint(50,90);

Triangle t2=new Triangle(P1,P2,P3);

System.out.println("\n\n\t\tTriangle 2\n\n");

System.out.println(t2);

System.out.println("Equlas T1 and T2 "+ t.equals(t2));

CartesianPoint P4=new CartesianPoint(25,55);

Rectangle r=new Rectangle(P1,P2,P3,P4);

System.out.println("\n\n\t\tRectangle\n\n");

r.display();

System.out.println("\n\nOverload toString method\n\n");

System.out.println(r);

System.out.println("\nEach Cartesian Point is move 15 relatively\n");

r.move();

r.display();

System.out.println("\nEach Cartesian Point is rotating 90 degree");

r.rotate(P1,90);

r.rotate(P2,90);

r.rotate(P3,90);

r.rotate(P4,90);

r.display();

P1=new CartesianPoint(10,20);

P2=new CartesianPoint(20,20);

P3=new CartesianPoint(30,20);

P4=new CartesianPoint(25,55);

Rectangle r2=new Rectangle(P1,P2,P3,P4);

System.out.println("\n\n\t\tRectangle 2\n\n");

System.out.println(r2);

System.out.println("Equlas R1 and R2 "+ r.equals(r2));

}

}