Notes: Sample Source Codes For Programming in Shell For Unix
Notes: Sample Source Codes For Programming in Shell For Unix
echo $fact
Q3. Write a shell script for checking Armstrong number or not
#!/bin/bash
echo "Enter Number"
read c
x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
n=`expr $r \* $r \* $r`
sum=`expr $sum + $n`
x=`expr $x / 10`
done
if [ $sum -eq $c ]
then
echo "It is an Armstrong Number"
else
echo "It is not Armstrong Number"
fi
do
p=$(( p * 2 ))
j=(( j + 1 ))
done
dec=$(( n * p ))
findec=$(( findec + dec ))
pow=$(( pow - 1 ))
i=$(( i + 1 ))
done
echo "Equivalent Decimal no:" $findec
Q7. Write a shell script to check for palindrome of a number.
#!/bin/bash
Q8. Write a shell script for finding the information about login name and file
name
#!/bin/bash
echo "Your user name: $(echo $USER)"
#!/bin/bash
echo "Enter value of x:"
read x
echo "Enter value of y:"
read y
echo "Before swap, x=$x and y=$y"
z=$x
x=$z
y=$z
echo "after swap, x=$x and y=$y"
int main()
{
int p_id,p_pid;
p_id=getpid();
p_pid=getppid();
printf("Child Proce id is: %d\n", p_id);
printf("Parent Process id is: %d\n", p_pid);
return 0;
}
Q13. Write a shell script to accept three command line arguments and display
each of them.
#!/bin/bash
echo "Script Name: $0"
echo "First Parameter of the script is $1"
echo "The Second Parameter is $2"
echo "The Complete list of arguments is $@"
echo "Total Number of Parameters: $#"
echo "The process ID is $$"
echo "Exit code for the script:$?"
Q14. Write a shell script to check whether the given file is present in a directory
and what permission are given to the owner.
#!/bin/bash
echo "Enter the file name"
read fname
if test -f $fname
the
echo "it is a file it has"
if test -r $fname
then
echo "read permission"
fi
if test -w $fname
the
echo "write permission"
fi
if test -x $fname
then
echo "execute permission"
else
echo "the given file does not exist" $fname
fi
Q15. Write a shell script to find area of circle, square, triangle and rectangle.
#!/bin/bash
echo -n "Enter base of traingle"
read b
echo -n "Enter height of a traingle"
read h
area=$(echo "scale=2;(1/2)*$b*$h" | bc)
echo "area of a traingle is $area"
echo -n " Enter the radius of the circle"
read r
area=$(echo "scale=2;3.14*($r*$r)" | bc)
echo "area of circle is $area"
echo "Enter a side of the square"
read s
echo "area of a square :`expr $s \* $s`"
echo "enter the length and breadth of a rectangle"
read leng
read brea
echo "area of the reactangle is :`expr $leng \* $brea`"
Q16. Write a shell script to accept three subject marks scored by a student and
declare result.
#!/bin/bash
tput clear
echo "Enter Student Name:"
read name
echo "Enter roll number:"
read rollnumber
echo "Enter 3 Subject marks:"
read m1 m2 m3
echo "Name of the student is: $name"
echo "Student Roll Number is: $rollnumber"
echo "3 Subject Marks"
echo "m1 m2 m3"
echo $m1 $m2 $m3
#!/bin/bash
clear
sum=0
i="y"
echo "enter first number:"
read n1
echo "enter second number:"
read n2
while [ $i = "y" ]
do
echo "1.addition"
echo "2.subtration"
echo "3.multiplication"
echo "4.division"
read ch
case $ch in
1)sum=`expr $n1 + $n2`
echo "sum = " $sum ;;