Linux Program and Commands
Linux Program and Commands
fi
10] Write a shell script to print week day name of the week
corresponding to week day number.Give apprropriate message
if the number is not between 1 to 7.
CODE:
echo -e “Enter day number”;
read a
case $a in
1) echo -e “Sunday”;;
2) echo -e “Monday”;;
3) echo -e “Tuesday”;;
4) echo -e “Wednesday”;;
5) echo -e “Thursday”;;
6) echo -e “Friday”;;
7) echo -e “Saturday”;;
*) echo -e “Invalid output! Kindly enter number between 1 to 7”;;
Esac
11] Using case, write a shell script to find out the entered
character is vowel or not.
CODE:
echo -e “Enter character”;
read a
case $a in
a) echo -e “VOWEL”;;
e) echo -e “VOWEL”;;
i) echo -e “VOWEL”;;
o) echo -e “VOWEL”;;
u) echo -e “VOWEL”;;
A) echo -e “VOWEL”;;
E) echo -e “VOWEL”;;
I) echo -e “VOWEL”;;
O) echo -e “VOWEL”;;
U) echo -e “VOWEL”;;
*) echo -e “Invalid Input”;;
Esac
12] Write a shell script to enter the number from user for choice
1 for +, 2 for -, 3 for *.Get two numbers again from the user and
calculate the result.
CODE:
echo -e “Enter number between 1 and 3”;
read a
echo -e “Enter two numbers”;
read b c
case $a in
1) echo -e “b+c = `expr $b + $c`”;;
2) echo -e “b-c = `expr $b - $c`”;;
3) echo -e “b*c= `expr $b \* $c`”;;
*) echo -e “Invalid Input”;;
Esac
13] Write a shell script to read a character n check whether it is
upper case letter or lower case letter or digit or any other
special character and give appropriate message.
CODE:
echo -e “Enter a character : “;
read n
case $n in
[A-Z]) echo -e “$n is an upper-case alphabet”;;
[0-9]) echo -e “$n is a digit”;;
[a-z]) echo -e “$n is a lower-case alphabet”;;
*) echo -e “$n is a special character”;;
esac
14] Write a shell script to print sum of n number using for and
while loop.
CODE:
FOR:
echo -e “Enter size:”;
read n
sum=0
echo -e “Enter numbers:”;
for((i=1; i <= n; i++))
do
read num
sum=`expr $((sum+num))`
done
echo -e “Sum is $sum”:
WHILE:
echo -e “Enter size:”;
read n
sum=0
i=1
echo -e “Enter numbers:”;
while [ $i -le $n ]
do
read num
sum=`expr $((sum+num))`
i=`expr $((i+1))`
done
echo -e “Sum is $sum”:
C Programming in Linux:-
printf(“%d is max\n”,b);
}
else{
Printf(“%d is max\n”,c);
}
return 0;
}
4] Find the factorial of number using C programming.
CODE:
#include <stdio.h>
int main()
{
int i;
int fact = 1;
int num;
printf(“Enter a number:\n”);
scanf(“%d” ,&num);
for(i=1;i<=num;i++)
{
fact=fact * i;
}
printf(“ Factorial of %d is %d\n”, num,fact);
return 0;
}
CODE:
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter two numbers:”);
scanf(“%d %d”,&a,&b);
c=a+b;
printf(“Sum of %d and %d is %d\n”,a,b,c);
}
Shell script program:-
perror("bad fork");
exit(1);
}
if (pid == 0)
{
printf("I am the child process \n");
}
else
{
wait(&status);/*parent waits for child to finish */
printf("I am the parent process");
}
}