Custom Search

Saturday 21 April 2012

Write a Linux shell script that prints only given no. of lines from top & bottom of a file, Unix program to print numbers from top & bottom of a file


Write a Linux shell script that prints only given no. of lines from top & bottom of a file, Unix program to print numbers from top & bottom of a file


Program
clear
echo-n "Enter file name:"
read fn
echo-n "No. of line:"
read n


t='head-$n<$fn'
b='tail-$n<$fn'


echo"Top:"
echo$b


Output:
Enter file name: temp.sh
No. of line:3
Note : 3 lines from top and 3 lines from bottom are displayed here.
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script to convert uppercase to lowercase letters for a given file, Unix program to convert uppercase to lowercase letters


Write a Linux shell script to convert uppercase to lowercase letters for a given file, Unix program to convert uppercase to lowercase letters


Program
clear


echo"Enter file name:"
read fn


a='tr'[A-Z]''[a-z]'<$fn'
echo$a


Output:
Enter File name: temp.sh
ALL THE CONTENT IS CONVERTED INTO CAPITAL LETTERS.
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script to count number of characters, words & lines, Unix program to count number of characters, words & lines


Write a Linux shell script to count number of characters, words & lines, Unix program to count number of characters, words & lines


Program
clear
echo-n "Enter file name:"
read fn


c='wc-c<$fn
w='wc-w<$fn
l='wc-l<$fn


echo"NO. of characters:"$C
echo"No.of Words:"$w
echo"No.of Lines:"$1


Output:


Enter file name: temp.sh
No.of characters:359
No. of words:45
No. of lines:6
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script to check whether the entered number is Armstrong number or not using the command line argument, Unix program for Armstrong number


Write a Linux shell script to check whether the entered number is Armstrong number or not using the command line argument, Unix program for Armstrong number


Program
clear
no=$1
n=$1


while[ $n-gt 0]
do


a='expr $n%10'
a3='expr$a\*$a\*$a'
temp='expr$temp+ $a3'
n='expr$temp+$a3'
done


if[$temp-eq$no]
then
  echo"It is Armstrong number"
else
  echo"It is Armstrong number"
fi




Output:


>sh Armstrong.sh 159
It is Armstrong number
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script to list, make, delete & copy file, Linux shell script to display, change & remove a directory, Unix program list, make, delete, copy file & directory


Write a Linux shell script to list, make, delete & copy file, Linux shell script to display, change & remove a directory, Unix program list, make, delete, copy file & directory 


Write a Linux shell script to display the following menu.
1) Files- 1. List files
          2. Make a file
          3. delete a file
          4. Copy a file1.


2) Directory- 1. Make a directory
              2. Display current directory
              3. Change current directory
              4. Remove current directory
3) Quit


Program
clear
echo"1.FILE"
echo"2.DIRECTORY"
echo"3.QUIT"
echo""


echo-n"Enter your choice:"
read ch


case $ch in
1) echo""
  echo"   1. List files
  echo"   2. Make a file
  echo"   3. delete a file
  echo"   4. Copy a file
  echo""


echo-n "Enter ypur name"
read ch1


case $ch1in
 1) echo""
   is;;
 2) echo""
   echo-n "Enter file name:"
   read fn
   cat>$fn 
   echo "File successfully created"
   echo""
   is;;
 3) echo""
   echo-n"Enter file name to delete"
   read fn
   rm-r$fn
   echo""
   echo"File successfully deleted"
   echo""
   IS;;
 4) echo""
   echo-n "Enter file name from you want to copy:"
   read f1
   echo-n" Enter file name to you want to copy:"
   read f2
   cp $f1 $f2
   echo""
   echo" File successfully copied";;
   
   esac;;


2) echo""
  echo" 1. Make a directory
  echo" 2. Display current directory
  echo" 3. Change current directory
  echo" 4. Remove current directory
  echo""


  echo-n "Enter your choice:"
  read ch2
  
  case $ch2 in
  1) echo""
     echo-n "Enter the name for directory:"
     read dn
     mkdir $ dn
     echo""
     echo" Directory successfully created"
     echo""
     is;;

  2) echo-n "Current directory:"
     pwd
     echo""
     
  3) echo""
     echo-n" Current directory:"
     pwd
     echo-n"Enter the name of directory you want to go to:"
     read dn
     cd $dn
     echo"Directory has been changed successfully"
     echo-n " Current directory"
     pwd
     echo"";;
  4) echo""
     echo-n "Enter old name:"
     read od
     echo-n "Enter new name:"
     read nd
     mv $od $nd
     echo" Directory successfully renamed"
     echo"";;


     esac;;


3) exit;;
esac


Output:


1. FILE
2. DIRECTORY
3. QUIT 
Enter your choice:1


1. LIST FILES
2. MAKE FILE
3. DELETE FILE
4. COPY FILE
Enter your choice:2


All files in current directory are listed here.
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script for calendar for current month & year also for range of months, Unix program to print the calender


Write a Linux shell script for calendar for current month & year also for range of months, Unix program to print the calender 


Program
clear
cal
echo""
echo""
echo"Enter year"
read y
echo"Enter starting month"
read m1
echo"Enter ending month"
read m2
for ((i=m1;i<=m2;i++))
do
 cal $i$y
done


Output:
Enter year 2009
Enter starting month :2
Enter ending month :3


Note: it will show Feb. 2009 and month 2009 cal.
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script that evaluates the following series 1+1/1+1/2+1/3+...+1/n, Unix program to evaluates the series


Write a Linux shell script that evaluates the following series 1+1/1+1/2+1/3+...+1/n, Unix program to evaluates the series


Program
clear
echo"Enter number upto which series to be printed"
read n
sum=1
for((i=1;i<=n;i++))
do
 sum='echo$sum +1\$i|bc-1'
done
echo$ sum


Output:
Enter number upto which series to be printed:2
2.5
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script for generating mark sheet & calculate grade also, Linux program to find total, average, percentage & Grade for given subjects


Write a Linux shell script for generating mark sheet & calculate grade also, Linux program to find total, average, percentage & Grade for given subjects


Program
clear
echo"Enter no of students:"
read ns


for((i=1;i<ns;i++))
do
echo""""
echo"Enter roll no of student$i:"
read s1[i]


echo "Enter subject1 marks:"
reads1[i]


echo "Enter subject2 marks:"
reads2[i]


echo "Enter subject3 marks:"
reads3[i]


done


for((i=1;i<ns;i++))
do


 total[i]='expr${s1[i]}+${s2[i]}+${s3[i]}
 avg[i]='expr${total[i]}/3'


 if[$avg[i]-ge70]
then 
  grd[i]="DISTINCTION"


  elif[$avg[i]=ge60-a$avg[i]-le69]
then
  grd[i]="SECONDCLASS"


elif[$avg[i]=ge40-a$avg[i]-le49]
then


 grd[i]="PASSCLASS"


else
  grd[i]="FAIL"
fi


done


for((i=1;i,=ns;i++))
do
echo""
echo"______________________________"
echo"Roll no:"${rn[i]}
echo"______________________________"
echo" Subject 1     "${s1[i]}
echo" Subject 2     "${s2[i]}
echo" Subject 3     "${s3[i]}
echo"______________________________"
echo"Total         "${total[i]}
echo"______________________________"
echo" Percentage:  "${avg[i]}
echo"______________________________"
echo" Grade:       "${grd[i]}
echo"______________________________"
echo""
done


Output:
Enter no of student:1
Enter roll no for student:21
Enter marks for subject1:90
Enter marks for subject2:91
Enter marks for subject3:92


_____________________________________
Roll no:21
_____________________________________
Subject 1      90
Subject 2      91
Subject 3      92
_____________________________________
Total          276
_____________________________________
Percentage:    91
_____________________________________
Grade          Distinction
_____________________________________
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script that reverses the string, Unix program to find shell script that reverses the string


Write a Linux shell script that reverses the string, Unix program to find shell script that reverses the string


Program
clear
echo "Enter any string:"
read a
b=$a
d='expr$a|wc-c'
c='expr$d-1'
while[$c-gt 0]
do
 n='expr$a|cut-c$c'
 r=$r$n
 c='expr$c-1
done
echo"Reverse string:"$r


Output:


Enter any string:
laptop
Reverse string: potpal
<<< Previous  ||  Current  ||  Next >>>  

Friday 20 April 2012

Write a Linux shell script to check whether the string is palindrome, Unix program to check whether the string is palindrome


Write a Linux shell script to check whether the string is palindrome, Unix program to check whether the string is palindrome


Program
clear 
echo"Enter any string:"
read a


b=$a
d='expr$a| wc-c'
c='expr$d-1


While [$c-gt 0]
do
  n='expr $a|cut-c$c'
  r=$r$n
  c='expr$c-1
done


echo$r


if[$b==$r]
then
  echo"String is palindrome"
else 
  echo"Not palindrome string"
fi


Output:
Enter any string:
madam
madam
String is palindrome.
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script to find factorial of a given number, Unix program to find factorial of n numbers


Write a Linux shell script to find factorial of a given number, Unix program to find factorial of n numbers


Program
clear
echo "Enter the number:"
read n


fact=1


for ((i=1;i<=n;i++))
do
  fact='expr$fact\*$i'
done


echo "Factorial:"$fact




Output
Enter the number
4
Factorial:24
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script that reverses the number, Unix program to reverse the number


Write a Linux shell script that reverses the number, Unix program to reverse the number




Program
clear
echo "Enter any number:"
read n
sum=0
while [ $n-gt 0]
do
   a='expr $n%10'
done
echo "Reverse no:" $Sum


Output
Enter any number 
435
Reverse no: 534
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script to print and add all even numbers between given range, Unix program to print and add all even numbers


Write a Linux shell script to print and add all even numbers between given range, Unix program to print and add all even numbers


Program


clear 
echo "Enter Starting no:"
read n1
echo "Enter Ending no:"
read n2


sum=0


for((i=n1;i<=n2;i++))
do
   if['expr $i%2'-eq 0]
   then 
      echo $1
      sum= 'expr $sum +$i'
   fi
 done


 echo "Sum :"$sum


Output:
Enter Starting no:
2


Enter Ending no:
8


2
4
6
8


Sum :20
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script to generate prime number from 1 to n, where n is any positive integer number entered by user, Unix program for prime numbers from 1 to n


Write a Linux shell script to generate prime number from 1 to n, where n is any positive integer number entered by user, Unix program for prime numbers from 1 to n


Program


clear
echo "Enter the number upto which you want prime numbers:"
read n


for ((i=1;i<=n;++))
do
 flag=0
 for ((j=2;j<i;j++))
 do


  if [ 'expr $i % $j' -eq 0]
  then
      flag=1
  fi
  done
  
  if [$flag-eq 0]
  then
    echo $i
  fi


  done


Output:
Enter the number upto which you want prime numbers:
10


1
2
3
5
7
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script to generate fibonacci number from 1 to n, Unix program for fibonacci series


Write a Linux shell script to generate fibonacci number from 1 to n, Unix program for fibonacci series


Program
clear
echo "Enter the number upto which you want series :"
read n


n1=0
n2=1
echo $n1
echo $n2


for ((i=0;i<n;i++))
do
  n3='expr $n1 + $n2 '
  echo$n3
  n1= $n2
  n2= $n3
 done


Output:
Enter the number upto which you want series :
5


0
1
1
2
3
5
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script to wish good morning, Good afternoon and Good night as per current system time, Unix program to use current system time


Write a Linux shell script to wish good morning, Good afternoon and Good night as per current system time, Unix program to use current system time


Program
clear
date>dt
tm= 'cut-c 12-13 dt'
echo $tm


if [ $tm-lt 12]
then
  echo "Good Morning!!"
elif [$tm-gt12-a $tm-lt16]
then
  echo "Good Afternoon!!"
elif [$tm-gt16-a $tm-lt20]
then
  echo "Good Evening!!"
else
 echo "Good Night!!"
fi


Output


Good Afternoon
<<< Previous  ||  Current  ||  Next >>>  

Write a Linux shell script that performs simple arithmetic operation on given input, Unix program to performs simple arithmetic operation i.e addition, multiplication, division, subtraction, modules


Write a Linux shell script that performs simple arithmetic operation on given input, Unix program to performs simple arithmetic operation i.e  addition, multiplication, division, subtraction, modules


Program
clear
echo "Enter first no :"
read a 
echo "Enter second no :"
read b


echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "5. Modulo operation"
echo "6. Exit"


echo "Enter your choice :"
read ch


case $ch in
  1) echo Answer = 'expr $a+ $b';;
  2) echo Answer = 'expr $a- $b';;
  3) echo Answer = 'expr $a\*$b';;
  4) echo Answer = 'expr $a/$b';;
  5) echo Answer = 'expr $a%$b';;
  6) exit
esac
<<< Previous  ||  Current  ||  Next >>>  

Laptops