Program:
echo ‘enter the I file’
read a
echo ‘enter the II file’
read b
echo ‘enter the III file’
read c
cat $a $b > $c
cat $c
Output:
[mit27@mcettelnet ~]$ cat>aa
hi
[mit27@mcettelnet ~]$ cat>bb
how are you
[mit27@mcettelnet ~]$ sh append.sh
.enter the I file.
aa
.enter the II file.
bb
.enter the III file.
cc
hi
how are you
[mit27@mcettelnet ~]$
Basic Shell Programming – To perform Arithmetic Operations
Program:
Arithmetic Operation (case statement)
echo "Arithmetic Operation"
echo "********************"
echo "Enter the two numbers"
read a b
echo "1.ADD\n2.SUB\n3.MUL\n4.DIV\n5.EXIT\n"
echo "Enter the your option"
read op
echo "The result is"
case "$op" in
1)expr $a + $b;;
2)expr $a - $b;;
3)expr $a \* $b;;
4)expr $a / $b;;
5)exit;;
esac
Output:
[mit27@mcettelnet ~]$ nano arith.sh
[mit27@mcettelnet ~]$ sh arith.sh
Arithmetic Operation
********************
Enter the two numbers
5 2
1. ADD\n2.SUB\n3.MUL\n4.DIV\n5.EXIT\n
Enter the your option
3
The result is
10
Basic Shell Programming – To find the greatest of two numbers
Program:
echo Enter a Number
read num1
echo Enter a Number
read num2
if test $num1 -ge $num2
then echo $num1 is greater than $num2
else
echo $num2 is greater than $num1
fi
Output:
[mit27@mcettelnet ~]$ nano gretest.sh
[mit27@mcettelnet ~]$ sh greatest.sh
Enter a Number
10
Enter Number
8
10 is greater than 8
Basic Shell Programming – To find the sum of digits
Program:
echo "enter the number"
read a
b=`expr $a % 10`
c=`expr $a / 10`
d=`expr $b + $c`
echo "the sum of digit is $d"
Output:
[m08cs127@mcettelnet ~]$ sh digits.sh
enter the number
45
the sum of digit is 9
Basic Shell Programming – To Check a number is even or odd
Program:
echo Enter a Number to Find Even or odd
read num
b=`expr $num % 2 |bc`
if [ $b -eq 0 ]
then
echo Even number
else
echo Odd Number
fi
Output:
[m08cs114@mcettelnet ~]$ sh odd.sh
Enter any no:
58
58 is even
Basic Shell Programming – To find the Fibonacci series
Program:
echo "Enter the range"
read r
a=-1
b=1
i=1
while [ $i -le $r ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1 `
done
Output:
[m08cs127@mcettelnet ~]$ sh fib.sh
enter the fib series range
4
0
1
1
2
Basic Shell Programming – To find the Factorial of a given number
Program:
echo "enter number"
read num
i=1
fact=1
while test $i -le $num
do
fact=`expr $fact \* $i`
i=`expr $i + 1`
done
echo $num! = $fact
Output:
[m08cs127@mcettelnet ~]$ sh fact.sh
enter number
4
4! = 24
Program to print variables from memory address.
Program:
#include
#include
main()
{
int *a, *n, size;
printf("Enter the size..");
scanf("%d",&size);
n=(int *)malloc(size * sizeof(int));
printf("Address or the first byte is...%d\n",n);
printf("Enter the values..");
for(a=n;a
printf("Printing the values...\n");
for(a=n+size-1;a>=n;a--)
printf("%d is stored in address %d\n",*a,a);
}
Output:
Enter the size..5
Address or the first byte is...161255432
Enter the values..1
2
3
4
5
Printing the values...
5 is stored in address 161255448
4 is stored in address 161255444
3 is stored in address 161255440
2 is stored in address 161255436
1 is stored in address 161255432
Program to swap two numbers using pointers.
Program:
#include
int swap(int *i, int *j);
int main()
{
int a,b;
printf("Enter the 2 nos");
scanf("%d %d",&a,&b);
printf("%d %d\n",a,b);
swap(&a,&b);
printf("%d %d\n",a,b);
}
int swap(int *i, int *j)
{
int t;
t = *i;
*i = *j;
*j = t;
}
Output:
Enter the 2 nos
5
9
5 9
9 5
Program to illustrate function without argument and no return value
Program:
#include
main()
{
void message(void);
message();
}
void message()
{
char str[100];
printf("\nenter a string name");
scanf("%s",str);
printf("welcome %s",str);
}
Output:
Enter a string.. CSE
WELCOME TO… CSE
Program to illustrate function with argument and no return value
Program:
#include
void add(int,int);
main()
{
int a,b;
printf("\nenter the two number");
scanf("%d%d",&a,&b);
add(a,b);
}
void add(int a,int b)
{
int c;
c=a+b;
printf("%d",c);
}
Output:
Enter two numbers.. 10 20
The addition of two numbers 10 and 20 is 30
Program to illustrate function with argument and with return value.
Program:
#include
int add(int x, int y);
int main()
{
int a, b, c;
printf("Enter two numbers...");
scanf("%d %d", &a,&b);
c= add(a,b);
printf("The addition of two numbers %d and %d is %d", a, b, c);
}
int add(int x, int y)
{
int z;
z=x + y;
return(z);
}
Output:
Enter two numbers.. 5 6
The addition of two numbers 5 and 6 is 11