Featured Post

Monday, August 1, 2016

FOSS Lab sample prorgams

                                          http://cjuschools.blogspot.in/



//particular user has logged in or not

echo "Enter username"
read username
c=`who|grep -c $username`
echo $c
if [ $c -gt 0 ]
then
 echo "$username is logged"
else
 echo "No such user"
fi

output:

                     Enter username
                     anil
                    anil is logged



// shell program to check whether a given file has read write execute perimissions
echo "enter your file name"
read fname
if [ -r $fname ]
then
echo "given file has read permission"
fi
if [ -w $fname ]
then
echo "given file has write permission"
fi
if [ -x $fname ]
then
echo "given file has execute permission"
fi

output:
       enter your file name
       hello.c
      given file has read permission
      given file has write permission
      
         


//shell program to count the number of files in a directory

echo "enter a directory name"
read dname
i=0
for k in `ls $dname`
do
i=`expr $i + 1 `
done
echo "no of files in directory are $i"


output:
              enter a directory name
              netsem2014
              no of files in  directory are 24

// shell program to check whether the given number is prime or not

echo "enter a number"
read num
i=2
flag=0
while [ $i -lt $num ]
do
   if [ `expr $num % 2` -eq 0 ]
    then
    flag=1
    fi
    i=`expr $i + 1`
 done
if [ $flag -eq 0 ]
then
  echo "given number is prime"
else
  echo "given unmber is not a prime"
fi

output:
            enter a number
            23
            given number is not a prime



//shell program to check print fibonacci sequence with in the given range 
echo "enter the range"
read n
a=0
b=1
echo $a
echo $b
c=0
c=`expr $a + $b`
while [ $c -lt $n ]
do
 echo $c
 a=$b
 b=$c
  c=`expr $a + $b`
 done

output:
          enter the range
          10
           0 1 1 2 3 5 8


// shell program to concatenate two strings

echo "enter first string"
read s1
echo "enetr second string "
read s2
s3=$s1$s2
len=`echo $s3 | wc -c`
len=`expr $len - 1`
echo " concatenated string is $s3 of length $len "

output:
   enter first string
   raj
   enter the second string
  kumar
  concatenated string is rajkumar of length 8


//shell program to find the reverse of a given string

echo "Enter any string:"
read string
len=`expr $string|wc -c`
len=`expr $len - 1`
while [ $len -gt 0 ]
do
        r=`expr $string|cut -c $len`
        rev=$rev$r
        len=`expr $len - 1`
done
echo "The reverse of given string is $rev"


output:
        enter any string
        anil
        The reverse of given string is jar


//shell program to check whether the given string is palindrome or not

echo "Enter a string"
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
i=1
j=`expr $len / 2`
while [ $i -le $j ]
do
k=`echo $str | cut -c $i`
l=`echo $str | cut -c $len`
if [ $k != $l ]
then
echo "String is not palindrome"
exit
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
echo "String is palindrome"

ouput
    Enter a string
    madam
    given string is palindrome

// shell program to find the grade of students 
echo "enter your name and number"
read name num
echo "enter your five subjects marks"
read sub1
read sub2
read sub3
read sub4
read sub5
total=`expr $sub1 + $sub2 + $sub3 + $sub4 + $sub5`
avg=`expr $total / 5`
echo "$name \t $num"
if [ $avg -ge 80 ]
then
echo "grade A"
elif [ $avg -ge 70 -a $avg -lt 80  ]
then
echo "grade B"
elif [ $avg -ge 60 -a $avg -lt 70 ]
then
echo "grade C"
elif [ $avg -ge 50 -a $avg -lt 60 ]
then
echo "grade D"
elif  [ $avg -ge 40 -a $avg -lt 50 ]
then
echo " grade E"
else
echo "grade F"
fi


ouput:
         enter your name and number
         raj   120A0501
       enter your five subject marks
       60 60 60 60 60 
       raj 120A0501 grade C

https:theunixprogramming.blogspot.in


// shell program to calculate the employee net salary


echo "enter your name "
read empname
echo "enter your empid"
read empid
echo " enter your basic salary"
read basic
HRA=`expr $basic \* 18 / 100`
DA=`expr $basic \* 35 / 100`
IT=`expr $basic \* 14 / 100`
TA=`expr $basic \* 10 / 100`

total=`expr $basic + $HRA  + $DA + $IT + $TA`

echo "$empid \t $empname \t basic salary is $basic \t HRA is $HRA DA is $DA IT is $IT \t TA is $TA and total salary is $total"

output:
   enter your name
   raj
   enter your empid
   120A0501
   enter your basic salary
   10000
   120A0501 raj basic salary is 10000 HRA is 1800  DA is 3500 IT is 1400 TA is 1000 and total salary is 17700


// Shell program to fine GCD of two given numbers
echo "enter two numbers"
read a
read b
while [ $a -ne $b ]
do
if [ $a -gt $b ]
then
a=`expr $a - $b`
else
b=`expr $b - $a`
fi
done
echo "gcd is "
echo $a

output:
enter two numbers
10 25
  gcd is 5 


//menu options 
echo "1 present working directory  2 display date  3  display users logged into the system 
echo "enter your choice"
read a
case $a in
1)pwd;;
2)date;;
3)who;;
esac   

output:
     1. present working directory  2. display date   3. display users logged into the system.
    enter  your choice
    1
     /home/anil/~$




// shell program to check whether the given number is armstrong or not
echo "enter a 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 "armstrong number"
else
echo "not armstrong number"
fi

output:
enter a number
153
 armstrong number



https://theunixprogramming.blogspot.in/



//shell program to print mathematical tables
echo "enter your range"
 read x
i=1
res=0

 while [ $i -le $x ]
  do
 echo "Multiplication table $i"
    j=1

  while [ $j -le 10 ]
   do
   res=`expr $i \* $j`
   echo "$i*$j= $res"
   j=`expr $j + 1`
  done

 i=`expr $i + 1`
 echo ""
 echo ""
 echo ""

done

output;
enter your range
2

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


//shell program to print the ncr values
fact()
{
  i=1
  a=1
  while [ $i -le $x ]
  do
   a=`expr $a \* $i`
   i=`expr $i + 1`
  done
}
echo "enter the n value"
 read n
echo "enter the r value"
 read r
 x=$n
 fact
 nf=$a

 x=$r
 fact
 rf=$a

 x=`expr $n - $r`
 rf=$a
 fact
nrf=$a

 den=`expr $rf \* $nrf`
  res=`expr $nf / $den`
echo $res

output:
enter the n value
4
enter the r value
1
4


//shell program to check whether the give number is palindrome or not
echo "Enter any number"
read num
m=`expr $num`
sum=0
while [ $num -gt 0 ]
do
r=`expr $num % 10`
sum=`expr $sum \* 10 + $r`
num=`expr $num / 10`
done
if [ $m -eq $sum ]
then
echo "Given number is palindrome"
else
echo "The number is not palindrome"
fi

output:
Enter any number
121
Given number is palindorme


//shell program to implement bubble sort

echo "Enter your range"
read n
i=0
echo "Enter the elements"
while [ $i -lt $n ]
do
 read num
 eval arr$i=$num
 i=`expr $i + 1`
done
i=0
j=0
while [ $i -lt $n ]
do
 j=`expr $i + 1`
 while [ $j -lt $n ]
 do
  eval x=\$arr$i
  eval y=\$arr$j
  if [ $x -gt $y ]
  then
   x=`expr $x + $y`
 y=`expr $x - $y`
   x=`expr $x - $y`
   eval arr$i=$x
   eval arr$j=$y
  fi
  j=`expr $j + 1`
 done
 i=`expr $i + 1`
done
echo "The array after sorting is "
i=0
while [ $i -lt $n ]
do
 eval t=\$arr$i
 echo "$t"
 i=`expr $i + 1`
done

output:
Enter your range
5
Enter your elements
10 5 3 1 15
The array after sorting is
1 3 5 10 15 


//shell program to fin min and max elements in a array
echo "Enter your range"
read n
i=0
echo "Enter the elements"
while [ $i -lt $n ]
do
 read num
 eval arr$i=$num
 i=`expr $i + 1`
done
i=0
eval min=\$arr$i
eval max=\$arr$i
i=1
while [ $i -lt $n ]
do
 eval t=\$arr$i
 if [ $t -lt $min ]
 then
  min=$t
  elif [ $t -gt $max ]
 then
  max=$t
 fi
 i=`expr $i + 1`
done
echo "The minimum value of the array is $min"
echo "The maximum value of the array is $max"

output:
Enter your range
5
Enter the elements
10 5 20 2  45
The minimum value of array is 2
 The maximum value of array is 45




//shell program to implement linear search
echo "enter the size"
read n
i=0
echo "enter the elements"
while [ $i -lt  $n ]
do
read num
eval arr$i=$num
i=`expr $i + 1`
done
echo "enter the key"
read key
i=0
while [ $i -lt $n ]
do
eval t=\$arr$i
if [ $t -eq $key ]
then
echo "found at `expr $i + 1`"
exit
else
i=`expr $i + 1`
fi
done
              
output:
enter the size
5
enter the elements
10 23 12   78 90
enter  the key
12
found at third location
  


//shell program to implement binary search

  echo "enter array size"
read n
i=0
echo "enter the elements in order"
while [ $i -lt $n ]
do
read num
eval arr$i=$num
i=`expr $i + 1`
done
echo "enter the key"
read key
beg=0
end=`expr $n - 1`
while [ $beg -le $end ]
do
r=`expr $beg + $end`
mid=`expr $r / 2`
eval t=\$arr$mid
if [ $t -eq $key ]
then
echo "found at `expr $mid + 1`"
exit
elif [ $key -gt $t ]
then
beg=`expr $mid + 1`
elif [ $key -lt $t ]
then
end=`expr $mid - 1`
fi
done
if [ $beg -gt $end ]
then
echo "not found"
fi

output:
enter array size  
5
enter the elements in order
10 20 30 45 50 
enter the key
30
30 is found at location 3


//awk program to check whether the student is pass or fail

awk 'BEGIN{print"\nNanme\tds\tc\tjava\ttotal\taverage\tpassoorfail"}
{
        total=0
        total=total+$2+$3+$4
        avg=total/3
        if ( $2>=35&&$3>=35&&$4>=35 )
        {
                str="pass"
        }
        else
        {
                str="fail"
        }
        print"\n"$1"\t"$2"\t"$3"\t"$4"\t"total"\t"avg"\t"str
}' pass    //note    pass is text file containing student data

output:
       Name         ds        c     java        total     average       pass or fail
        anil            60      60    60           180       60               pass
        raj              90      90     30          210       70               fail





//awk program to print student marks list

awk 'BEGIN{print"\nNanme\tds\tc\tjava\ttotal\taverage"}
{
        total=0
        total=total+$2+$3+$4
        avg=total/3
        print"\n"$1"\t"$2"\t"$3"\t"$4"\t"total"\t"avg
}' pass             //note: pass is text file which contains student data

output:
 Name        ds       c         java      total    average
  anil           90     90          60      240        80
   raj            60     70          80      210        70
   kumar      50     50          50      150        50





//count characters

awk 'BEGIN{count=0;print "CHARACTERS"}
{
  len=0
  len=length($0)
  count=count+len

}
END{print "No of characters in file are: \t"count}' sample.c  // note: sample.c is text document

Output:
 No of characters in file are 37


Singal handling in UNIX



                            http://cjuschools.blogspot.in/


                   
shell programs , user loggne in or not, jntuk foss lab programs syllabus, foss lab syallabus, foss lavy syllabus anhra university unix programmng lab, FOSS lab nannay university, FOSS lab JNTU Hyderbad foss lab JNTU anantapur, UCEk foss lnagaramab jntuk, FOSS lab vizaynagram , FOSS lab R13 regulation, list of  ,awk script, filters in unix, shells , shell program to find minimum and maximum elements in a unix, shell program to check whether the given number is armstrong number or not, shell program to check whether the given number is palindrome or not,

No comments:

Post a Comment