[go: up one dir, main page]

0% found this document useful (0 votes)
122 views22 pages

CP 2

The document provides examples of simple shell programs to perform tasks like swapping two numbers, string concatenation, calculating simple interest, finding string length, converting string case, arithmetic operations, and conditional statements. It includes programs for finding the biggest of two numbers, checking login name, determining odd/even, displaying student marks, command line arguments, menu driven programs, finding the biggest of three numbers, appending files, arithmetic operations menu, reversing a number, and calculating factorials. The programs demonstrate basic programming concepts in shell scripting like variables, user input, conditional statements, loops, arithmetic and string operations.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views22 pages

CP 2

The document provides examples of simple shell programs to perform tasks like swapping two numbers, string concatenation, calculating simple interest, finding string length, converting string case, arithmetic operations, and conditional statements. It includes programs for finding the biggest of two numbers, checking login name, determining odd/even, displaying student marks, command line arguments, menu driven programs, finding the biggest of three numbers, appending files, arithmetic operations menu, reversing a number, and calculating factorials. The programs demonstrate basic programming concepts in shell scripting like variables, user input, conditional statements, loops, arithmetic and string operations.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 22

Simple shell programs

EX: NO: 2a

Swapping 2 numbers:

echo enter a b
read a b
echo before swapping
echo a=$a
echo b=$b
echo after swapping
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo a=$a
echo b=$b
OUTPUT:
enter a b
78
before swapping
a=7
b=8
after swapping
a=8
b=7

-------------------------------------------------------------------------------------EX: NO: 2b

STRING CONCATENATION

echo "enter first string:"


read s1
echo "enter second string:"
read s2
echo "Concatenation of 2 strings:$s1$s2"
OUTPUT:
enter first string:
xxx
enter second string:
yyy
Concatenation of 2 strings: xxxyyy

EX:NO:2c

SIMPLE INTEREST

echo "enter the principal amount :RS."


read p
echo "enter the no of years :"
read n
echo "enter hte rate of interest :"
read r
si=`expr "scale=2; ($p * $n * $r) / 100" | bc
echo "simple interest=Rs.$si"
OUTPUT:
enter the p amount :RS.
10000
enter the no of years :
2
enter hte rate of interest :
24
simple interest=Rs.4800.00
-------------------------------------------------------------------------------------EX:NO:2d
FIND THE STRING LENGTH
echo "enter the strings:"
read s1
l=`expr $s1 | wc -c`
l=`expr $l - 1`
echo "Th length of the given string $s1 is $l"
enter the strings:
deepa
Th length of the given string deepa is 5
-------------------------------------------------------------------EX: NO: 2e CONVERT THE GIVEN STRING LOWERCASE TO
UPPERCASE
echo "enter the string:"
read s1
t=`expr $s1 | tr [a-z] [A-Z]`
echo "the given string in uppercase: $t"
OUTPUT:
enter the string:
deepa

the given string in uppercase: DEEPA


------------------------------------------------------------------------------------------EX:NO:2f

ARITHMETIC OPERATION

echo "enter two values:"


read a b
add=`expr $a + $b`
echo "additon = $add"
sub=`expr $a - $b`
echo "subraction = $sub"
mul=`expr $a \* $b`
echo "multi = $mul"
div=`expr $a / $b`
echo "division = $div"
mod=`expr $a % $b`
echo "modulo division = $mod"
OUTPUT:
enter two values:
56
additon = 11
subraction = -1
multi = 30
division = 0
modulo division = 5
---------------------------------------------------------------------------------------------------------EX: NO: 3a
BIGGEST OF 2 NOS USING IF STATEMENT
echo "enter 2 values:"
read a b
big=$a
if [ $a -lt $b ]
then
big=$b
fi
echo "the biggest no is $big"
OUTPUT:
enter 2 values:
81
the biggest no is 8
----------------------------------------------------------------------------------------

EX:NO:3b

CHECK UR LOGIN NAME USING IF STATEMENT

echo "enter ur login name :"


read lname
if [ $LOGNAME = $lname ]
then
echo "Ur login name is : $lname"
fi
OUTPUT:
enter ur login name :
iecea02
Ur login name is: iecea02

-----------------------------------------------------------------------------------EX: NO: 3C
FINDING THE ODD OR EVEN NUMBER
# Even or Odd no
echo Enter N
read n
n1=`expr $n % 2`
if [ $n1 -eq 0 ]
then echo IT IS EVEN NO.
else
echo IT IT ODD NO
fi
OUTPUT:
Enter N
3
IT IT ODD NO
-------------------------------------------------------------------------------------------EX: NO: 3d

STUDENT MARK LIST

#student Information system


echo STUDENT INFORMATION SYSTEM
echo Enter student Name regno m1 m2 m3
read name rno m1 m2 m3

tot=`expr $m1 + $m2 + $m3`


avg=`expr $tot / 3`
echo " ---------------------------------------------------------"
echo "
MARK LIST
"
echo "----------------------------------------------------------"
echo "
Name : $name
"
echo "
Regno : $rno
"
echo "------------------------------------------------------------"
if [ $m1 -ge 50 ]
then
echo "
M1 : $m1 Pass
"
else
echo "
M1 : $m1 Fail
"
fi
if [ $m2 -ge 50 ]
then
echo "
M2 : $m2 Pass
"
else
echo "
M2 : $m2 Fail
"
fi
if [ $m3 -ge 50 ]
then
echo "
M3 : $m3 PASS
"
else
echo "
M3 : $m3 FAIL
"
fi
echo "-----------------------------------------------------------"
echo "
TOTAL : $tot
"
echo "
AVERAGE : $avg
"
echo "-----------------------------------------------------------"
if [ $m1 -ge 50 -a $m2 -ge 50 -a $m3 -ge 50 ]
then
echo "
RESULT : PASS
"
else
echo "
RESULT : FAIL
"
fi
OUTPUT:
STUDENT INFORMATION SYSTEM
Enter student Name regno m1 m2 m3
deepa 03 100 100 99
--------------------------------------------------------MARK LIST
---------------------------------------------------------Name: Sonia
Regno: 03
-----------------------------------------------------------M1: 100 Pass

M2 : 100 Pass
M3 : 99 PASS
----------------------------------------------------------TOTAL : 299
AVERAGE : 99
----------------------------------------------------------RESULT : PASS
---------------------------------------------------------------------------------EX:NO:3e
COMMAND LINE SUBSTITUTION
echo COMMAND LINE ARGUMENTS
echo total no. of arguments : $#
if [ $# -ne 2 ]
then
echo Required 2 arguments
else
echo first argument is $1
echo Second argument is $2
fi
OUTPUT:
Sh command
COMMAND LINE ARGUMENTS
total no. of arguments : 0
Required 2 arguments
Sh command hai bye
COMMAND LINE ARGUMENTS
total no. of arguments : 2
first argument is hai
Second argument is bye
----------------------------------------------------------------------------EX: NO: 3f
FINDING BIGGEST OF THREE NUMBERS
#Biggest of three numbers
echo Enter a b c
read a b c
if [ $a -gt $b -a $a -gt $c ]
then echo A is Greater
elif [ $b -gt $c ]
then echo B is Greater
else
echo C is Greater
fi
OUTPUT:
Enter a b c

123
C is Greater
-----------------------------------------------------------------------------------------------EX: NO: 3g

APPEND ONE FILE TO ANOTHER

echo Enter 2 file names


read f1 f2
if [ -f $f1 ]
then
if [ -f $f2 ]
then
echo both files exit
cat $f2>>$f1
echo 2nd file is successfully appended to 1st file
echo "1st file Contents"
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~
cat $f1
else
echo f1 exist f2 not exist
fi
elif [ -f $f2 ]
then
echo f1 not exist f2 exist
else
echo both does not exist
fi
OUTPUT:
Enter 2 file names
dd ee
both does not exist
Enter 2 file names
file1 file2
both files exit
2nd file is successfully appended to 1st file
1st file Contents
~~~~~~~~~~~~~~~~~~~~~~~~~~
if u think u can u can do it.
---------------------------------------------------------------------------------------------Ex: No: 3h
#Arithmetic Program
echo Enter 2 Nos
read a b

ARITHMETIC OPERATION

ch=1
until [ $ch -eq 5 ]
#Arithmetic Program
echo Enter 2 Nos
read a b
ch=1
until [ $ch -eq 5 ]
do
echo 1.Addition
echo 2.Subtraction
echo 3.Multiplication
echo 4.Division
echo 5.Exit
echo Enter your Choice
read ch
case $ch in
1) echo Addition
echo
ans=`expr $a + $b`;;
2)echo Subtraction
echo
ans=`expr $a - $b`;;
3) echo Multiplication
echo
ans=`expr $a \* $b`;;
4)echo Division
echo
ans=`expr $a / $b
esac
echo $ans
done
Output:
Enter 2 Nos2 6INSERT -1.Addition-2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter your Choice
1
Addition
8
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit

Enter your Choice


2
Subtraction
-4
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter your Choice
3
Multiplication
12
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter your Choice
4
Division
0
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter your Choice
5
----------------------------------------------------------------------------------------EX: NO: 3i
ch=1
until [ $ch -eq 7 ]
do
echo menu
echo 1.who
echo 2.date
echo 3.wc
echo 4.cal
echo 5.sort
echo 6.ls
echo 7.exit
echo Enter ur choice
read ch
case $ch in
1)who;;
2)date;;

MENU OPERATION

3)echo enter filename


read fname
wc $fname;;
4)cal;;
5)echo Enter filename
read fname
sort $fname;;
6)ls;;
7)exit
esac
done
OUTPUT:
menu
1.who
2.date
3.wc
4.cal
5.sort
6.ls
7.exit
Enter ur choice
1
root :0
Apr 21 09:42
it61 pts/1
Apr 21 14:15 (192.170.170.7)
test pts/3
Apr 21 14:30 (192.170.170.49)
it45 pts/12
Apr 21 14:55 (192.170.170.8)
it33 pts/8
Apr 21 14:58 (192.170.170.35)
it58 pts/17
Apr 21 14:59 (192.170.170.9)
it38 pts/18
Apr 21 14:59 (192.170.170.27)
it57 pts/11
Apr 21 14:59 (192.170.170.29)
it40 pts/9
Apr 21 15:00 (192.170.170.3)
it35 pts/14
Apr 21 15:00 (192.170.170.26)
it56 pts/21
Apr 21 15:00 (192.170.170.11)
it37 pts/20
Apr 21 15:00 (192.170.170.14)
it62 pts/13
Apr 21 15:00 (192.170.170.32)
it48 pts/24
Apr 21 15:01 (192.170.170.31)
it60 pts/5
Apr 21 15:03 (192.170.170.4)
it61 pts/23
Apr 21 15:05 (192.170.170.7)
it52 pts/15
Apr 21 15:06 (192.170.170.34)
it46 pts/25
Apr 21 15:07 (192.170.170.17)
it45 pts/22
Apr 21 15:09 (192.170.170.8)
it64 pts/2
Apr 21 15:09 (192.170.170.12)
it41 pts/10
Apr 21 15:10 (192.170.170.30)
it34 pts/26
Apr 21 15:11 (192.170.170.16)
it44 pts/4
Apr 21 15:11 (192.170.170.6)
it36 pts/6
Apr 21 15:11 (192.170.170.13)
it53 pts/27
Apr 21 15:12 (192.170.170.25)

it55 pts/7
Apr 21 15:16 (192.170.170.10)
it43 pts/19
Apr 21 15:17 (192.170.170.5)
menu
1.who
2.date
3.wc
4.cal
5.sort
6.ls
7.exit
Enter ur choice
2
Tue Apr 21 15:19:21 IST 2009
menu
1.who
2.date
3.wc
4.cal
5.sort
6.ls
7.exit
Enter ur choice
3
enter filename
reverse
10 35 151 reverse
menu
1.who
2.date
3.wc
4.cal
5.sort
6.ls
7.exit
Enter ur choice
4
April 2009
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
menu
1.who
2.date
3.wc
4.cal

5.sort
6.ls
7.exit
Enter ur choice
5
Enter filename
reverse
a=`expr $n % 10`
do
done
echo Enter a number
echo The reversed No is $rev
n=`expr $n / 10`
read n
rev=0
rev=`expr $rev "*" 10 + $a`
while [ $n -ne 0 ]
menu
1.who
2.date
3.wc
4.cal
5.sort
6.ls
7.exit
Enter ur choice
6
arith arithmetic bigthree factorial menu oe reverse student
menu
1.who
2.date
3.wc
4.cal
5.sort
6.ls
7.exit
Enter ur choice
7
-------------------------------------------------------------------------------------EX: NO: 3j
FACTORIAL
#Factorial of the given number
echo Enter N
read n
i=1
f=1
while [ $i -le $n ]
do

f=`expr $f \* $i`
i=`expr $i + 1`
done
echo Factorial of the given number is $f
OUTPUT:
Enter N
6
Factorial of the given number is 720
---------------------------------------------------------------------------------------EX: NO: 3k REVERSING THE GIVEN NUMBER
echo Enter a number
read n
rev=0
while [ $n -ne 0 ]
do
a=`expr $n % 10`
rev=`expr $rev "*" 10 + $a`
n=`expr $n / 10`
done
echo The reversed No is $rev
OUTPUT:
Enter a number
123454
The reversed No is 454321
--------------------------------------------------------------------------------------------EX: NO: 3l

PALANDROME

echo Enter string


read str
count=$(echo $str | wc -c)
while [ $count -gt 0 ]
do
temp=`echo $str | cut -c$count`
count=`expr $count - 1`
stre=`echo $stre$temp`
done
if [ $str == $stre ]
then
echo THE GIVEN STRING IS A PALINDROME
else
echo THE GIVEN STRING IS NOT A PALINDROME
fi

OUTPUT:
Enter string
mala
THE GIVEN STRING IS NOT A PALINDROME
Enter string
priya
THE GIVEN STRING IS NOT A PALINDROME
EX: NO: 3m

SUM OF 10 NOS USING FOR LOOP

sum=0
for i in 1 2 3 4 5 6 7 8 9 10
do
sum=`expr $sum + $i`
done
echo "the sum of first ten nos is: $sum"
The sum of first ten nos is: 55
EX: NO: 3n

USAGE OF THE CONTINUE STATEMENT

ans=" "
echo "Do u want to enter value for ans(y/Y) ?:"
read ans
while [ $ans = y -o $ans = Y ]
do
echo "enter name : "
read name
echo $name >> names
wish=" "
echo "Do U want to continue ?"
read wish
if [ $wish = y -o $wish = y ]
then
continue
else
echo "the content of the file name is :`cat names`"
exit
fi
done
OUTPUT:
Do u want to enter value for ans(y/Y) ?:
y
enter name :
sumi

Do U want to continue ?
y
enter name :
rubi
Do U want to continue ?
n
the content of the file name is : sumirubi
-----------------------------------------------------------------------------------------EX: NO: 4

COMPUTE X RAISE TO THE POWER OF Y USING


FUNCTION

#include<stdio.h>
main()
{
int x,y;
int power(int,int);
printf("Enter x and y values...");
scanf("%d%d",&x,&y);
printf("%d to the power of %d is ...%d",x,y,power(x,y));
}
int power(int x,int y)
{
int p=1;
if(y>=0)
while(y--)
p*=x;
else
while(y++)
p/=x;
return(p);
}
OUTPUT:
cc power.c
./a.out
Enter x and y values...3 3
3 to the power of 3 is ...27

EX:NO:5

NESTED FUNCTION

#include<stdio.h>
main()
{
int x,y,z;
int ratio(int,int,int);
printf("enter x,y,z values");
scanf("%d%d%d",&x,&y,&z);
printf("ratio is %d \n",ratio(x,y,z));
}
int ratio(int x,int y,int z)
{
int diff(int,int);
if(diff(y,z))
return(x/(y-z));
else
return(0);
}
int diff(int m,int n)
{
if(m!=n)
return(1);
else
return(0);
}
OUTPUT:
cc nest.c
./a.out
enter x,y,z values 10
enter x,y,z values10 10 5
ratio is 2
--------------------------------------------------------------------------------------------EX: NO: 6
DEMONSTRATION OF GLOBAL VARIABLE
#include<stdio.h>
int a;
main()
{
a=39;
int f1();
int f2();
int f3();

printf("A....%d\n",a);
printf("A....%d\n",f1());
printf("A....%d\n",f2());
printf("A......%d\n",f3());
}
f1()
{
a=a+10;
return(a);
}
f2()
{
a=a+20;
return(a);
}
f3()
{
a=a+30;
return(a);
}
OUTPUT:
cc global.c
./a.out
A....39
A....49
A....69
A......99
--------------------------------------------------------------------------------------------EX: NO: 7

TRANSFER A STRUCTURE TO A FUNCTION

#include<stdio.h>
typedef struct
{
char* name;
int no;
int age;
}record;
main()
{
void fun(record *pt);
static record std = {"LAK",15,25};
printf("%s\t%d\t%d\t",std.name,std.no,std.age);
fun(&std);
printf("\n%s\t%d\t%d",std.name,std.no,std.age);
}
void fun(record *pt)

{
pt->name = "NNNN";
pt->no = 16;
pt->age= 26;
}
OUTPUT:
LAK 15 25
NNNN 16 26
----------------------------------------------------------------------------------------------------------EX:NO:8

DYNAMIC MEMORY ALLOCATION

#include<stdio.h>
#include<stdlib.h>
main()
{
int *a,*n,size;
printf("enter the size");
scanf("%d",&size);
n=(int *)malloc(size * sizeof(int));
printf("address of the first byte is %u \n",n);
printf("enter the value");
for(a=n;a<n+size;a++)
scanf("%d",a);
printf("printing the value...\n");
for(a=n+size-1;a>=n;a--)
printf("%d is stored in address %u\n",*a,a);
}
enter the size 10
address of the first byte is 5246992
enter the value3456
566
454
2442
907
67
45
40
33
3
printing the value...
3 is stored in address 5247028
33 is stored in address 5247024
40 is stored in address 5247020
45 is stored in address 5247016

67 is stored in address 5247012


907 is stored in address 5247008
2442 is stored in address 5247004
454 is stored in address 5247000
566 is stored in address 5246996
3456 is stored in address 5246992
-----------------------------------------------------------------------------------------------------------EX:NO:9
POINTER TO POINTER
#include<stdio.h>
main()
{
int a=22;
int *b;
int **c;
b=&a;
c=&b;
printf("\n value of a is %d",a);
printf("\n value of a is %d",*(&a));
printf("\n value of a is %d",*b);
printf("\n value of a is %d",**c);
printf("\n value of b and address of a=%u",b);
printf("\n value of c and address of b=%u",c);
printf("\n address of a=%u",&a);
printf("\n address of b=%u",&b);
printf("\n address of a=%u",*c);
printf("\n address of b=%u",&b);
printf("\n address of b=%u",c);
printf("\n address of c=%u",&c);
}
OUTPUT:
value of a is 22
value of a is 22
value of a is 22
value of a is 22
value of b and address of a=3221223852
value of c and address of b=3221223840
address of a=3221223852
address of b=3221223840
address of a=3221223852
address of b=3221223840
address of b=3221223840
address of c=3221223832

--------------------------------------------------------------------------------------EX: NO: 10 a

FILE HANDILING
PROGRAM TO CREATE A FILE

#include<stdio.h>
main()
{
int fd;
int protection_mode;
protection_mode=0644;
if((fd=creat("dept.dat",protection_mode))==-1)
{
perror("error!file not created");
exit(0);
}
else
printf("file is created successfully");
}
OUTPUT:
cc create.c
./a.out
file is created successfully.

EX: NO: 10b

PROGRAM TO OPEN FILE

#include<stdio.h>
main()
{
int fd;
if((fd=open("oop.dat",0))==-1)
{
perror("cannot open the file file1.dat");
exit(0);
}
else
{
printf("opened successfully");
}
}

OUTPUT:
cc open.c
./a.out
cannot open the file file1.dat: No such file or directory
---------------------------------------------------------------------------------------------EX: NO: 10 c PROGRAMM TO OPEN WITH CREATE
#include<stdio.h>
main()
{
int fd;
if((open("file1.dat",0,0644))==-1)
{
fd=creat("file1.dat",0);
printf("file is created and opened successfully");
}
else
{
printf("successfully created and opened");
}
}
OUTPUT:
cc co.c
./a.out
file is created and opened successfully
----------------------------------------------------------------------------------------EX: NO: 10d

PROGRAMM TO READ A FILE

#include<stdio.h>
main()
{
char buffer[10],x_reads;
int fd;
int x_read;
if((fd=open("oop.dat",0))==-1)
{
printf("error cannot open file1.txt");

exit(1);
}
do
{
printf("reading the file\n");
read(fd,buffer,10);
printf("%s\n",buffer);
}
while(x_read==10);
close(fd);
}
OUTPUT:
cc read.c
./a.out
error cannot open file1.txt
----------------------------------------------------------------------------------------------EX:NO:10 e PROGRAMM TO WRITE A FILE
#include<stdio.h>
main()
{
char buffer[10];
int fd;
printf("enter the input\n");
scanf("%s\n",buffer);
if((fd=creat("write.txt",0660))==-1)
{
printf("error cannot create the file");
exit(1);
}
write(fd,buffer,(unsigned)strlen(buffer));
printf("\n");
close(fd);
}
OUTPUT:
cc write.c
./a.out
enter the input
vcew
cat write.txt
vcew

You might also like