Вы находитесь на странице: 1из 32

1.Wap to print fibonacci series.

echo "Enter the number"


read n
a=1
b=-1
while [ $n -ne 0 ]
do
s=`expr $a + $b`
b=$a
a=$s
echo $s
n=`expr $n - 1`
done

2. Wap to take string as an input for finding its length.

echo "Enter any string to find the length"


read s
echo "The length of the string is:"
expr "$s" : '.*';

3.Wap by using a switch case to construct a calculator.


echo "Enter two number"
read a
read b
echo "press 1 for add"
echo "press 2 for subtract"
echo "press 3 for multiplication"
echo "press 4 for division"
read o
case $o in
1)
echo `expr $a + $b` ;;
2)
echo `expr $a - $b`;;
3)
echo `expr $a * $b` ;;
4)
echo `expr $a / $b` ;;
*) echo "wrong choice" ;;
esac

4.Wap to find whether the number is armstrong or not.

echo 'Enter armstrong number='


read n
a=$n
r=0
s=0
while [ $a -gt 0 ]
do
r=`expr $a % 10`
s=`expr $s + $r \* $r \* $r`
a=`expr $a / 10`
done
if [ $s -eq $n ]
then
echo 'armstrong'
else
echo 'not armstrong'
f

5. Wap by using grep command with both logical operators && and ||.
grep -e 'hod \| hi' compsc
grep -e 'study .* student' compsc

6. Write and execute the following UNIX Commands


Create two files with the name of name.txt, which contains only names, and reg.txt
with the content of register number respectively.

Combine the two files in the form of register number followed by name

Sort the two files in ascending order

Count the number of lines in the files

Rename the two files

7. Write and execute the following UNIX Commands


Create two files

Display the contents of both the files

Count the number of characters in both the files

Rename the two files

Combine the two files without duplicate

8. Write and execute the following UNIX Commands


Create a file

Print the specified number of lines of a file from start to end of the file

Display the file content with line number

Update the file content with I/O redirection

Rename the file

9. Write and execute the following UNIX Commands


Create a directory

Create a file

Rename the directory

Rename the file

View the file

10) Write and execute the following UNIX Commands


Create a file

Update the file

Display the file content

Display the file with line number

Count the number of words in the file

11) Write and execute the following UNIX Commands


Create a file

Rename a file

Change the mode of the file to read only

View the content of a file

Count the number of words in file

12) Write and execute the following UNIX Commands

Create a Directory called main

Create a sub-directory called sub in the main directory

Create a file in the main directory

Create the file to the sub directory

Delete the file in the main directory

13) Write and execute the following UNIX Commands.

Display the calendar

Display the date and time

Display the present working directory

Display your user name

Create a file using cat command

14) Write and execute the following UNIX Commands


Create two directories called CS1101 and CS1102

Create a file called CS1101.txt in the CS1101 directory

Create the file CS1101.txt from CS1101 to CS1102

Update the file content of CS1101.txt in the CS1102 directory

Rename the file in the CS1102 with CS1102.txt

15) Write and execute the following UNIX Commands


Create a directory with the name of Exam

Change the Exam directory as the working directory

Create a file called exam.txt in the Exam directory

View the content of the exam.txt file

Rename the file exam.txt to test.txt

16) Write and execute the following UNIX Commands

Create a file

View only files from a directory

Rename a file

View all files starting with a specific character

View all the files which has a specific extension

17) Write and execute the following UNIX Commands


Demonstrate pipe command

Demonstrate tee command

Use more than one command at a time ( who and date )

Create a file called create.txt

Move the file create.txt to move.txt

18. Write script to find out biggest number from given three numbers. Numbers are supplies as
command line argument. Print error if sufficient arguments are not supplied.
if [ $# -ne 3 ]
then
echo "Please give 3 command line arguments"
exit 1;
fi
if [ $1 -gt $2 ] && [ $1 -gt $3 ]
then

echo " $1 is the greatest number "


elif [ $2 -gt $3 ] && [ $2 -gt $1 ]
then
echo " $2 is the greatest number "
else
echo "$3 is the greates number"
fi

19. How to write shell script that will add two numbers, which are supplied as command
line argument, and if this two numbers are not given show error and its usage.

if [ $# -ne 2 ] ; then
echo -e " please provide correct number of arguments"
else
# $1 is first argument and $2 is second .
echo " sum of $1 + $2 is `expr $1 + $2` "
fi

20. Find a given file and show its permission and last modified Date 04.03.2016

21. Write a shell script to check whether a given file is exist or not?

22. Enter a number of names and sort them then display on the screen.

23. Write a Shell program to check the given number is even or odd
echo -n "Enter numnber : "
read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is even number"
else
echo "$n is odd number"
fi

24. Write a Shell program to check the given year is leap year or not
echo "Enter the year in 4 digits"
read a
if test `expr $a % 4` -eq 0
then
if test `expr $a / 100 ` -ne 0 -o `expr $a % 400` -eq 0
then
echo "The year is a leap year"
fi
else
echo "the year is not a leap year"
fi

25. Write a Shell program to find the area and circumference of a circle
echo "Enter the radius of the circle"
read r
pi=3.14
c=$(echo "scale=2;$pi * ($r * $r)" | bc)
echo "The area of cirlce is"
echo $c
d=$(echo "scale=2;$pi * (2 * $r)" | bc)
echo "The circumference is =$d"
26. Write a Shell program to check the given integer is prime or not

27. Write a Shell program to find the sum of square of individual digits of a number
echo "Enter the number"
read n
j=$n
s=0
while [ $j -gt 0 ]
do
r=`expr $j % 10`
j=`expr $j / 10`

s=`expr $s + $r \* $r`
done
echo "sum of square of the number is : $s"

28. Write a Shell program to find the largest among three numbers
echo Enter 3 numbers with spaces in between
read a b c
l=$a
if [ $b -gt $l ]
then
l=$b
fi
if [ $c -gt $l ]
then
l=$c
fi
echo Lagest of $a $b $c is $l

29. Write a Shell program to find the factorial of a number


echo "Enter the value for n"
read n

s=1
for(( i=1; i<=$n; i++ ))
do
s=$((s * i))
done
echo "The factorial is = $s"

30. Write a Shell program to find the sum of digits in an integer


echo -n "Enter number : "
read n
sd=0
sum=0
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
sum=$(( $sum + $sd )) # calculate sum of digit
done
echo "Sum of all digit is $sum"

31. Write a Shell program to swap the two integers ( i ) with temporary variable and
without using temporary variable.
echo "Enter two numbers to swap"
read a

( ii )

read b
echo "before swap"
echo "a=$a"
echo "b=$b"
temp=$a
a=$b
b=$temp
echo "after swap"
echo "a=$a"
echo "b=$b"

echo "enter two numbers to swap without using temp. variable"


read a
read b
c=`expr $a \* $b`
a=`expr $c / $a`
b=`expr $c / $b`
echo "after swap"
echo "a=$a"
echo "b=$b"

32. Write a Shell program to find the sum of all numbers between 50 and 100, which are
divisible by 3 and not divisible by 5.
echo "the sum of numbers from 50 to 100 which are divisible bye 3 but not divisible by 5"
s=0
for(( i=50; i<=100; i++ ))
do
j=$i
if test `expr $j % 3` -eq 0 -a `expr $j % 5` -ne 0
then
s=`expr $s + $j`
fi
done
echo "The sum of numbers is: $s"

33. Write script to see current date , time , shell and directory.
echo "Hello, $LOGNAME"
echo "Current date is `date`"
echo "User is `who i am`"
echo "Current direcotry `pwd`"

34. How to calculate 5.12 + 5.2 real number calculation at dollar prompt in shell.

35. How to perform real number calculation in shell script and store the result in third variable,let
say a=5.66, b=8.67 , c=a+b?
a=5.66

b=8.67
c=`echo $a + $b | bc`
echo "$a + $b = $c"

25. Write a Shell program to check the given integer is prime or not
echo "Enter a number: "
read num
i=2
f=0
while [ $i -le `expr $num / 2` ]
do
if [ `expr $num % $i` -eq 0 ]
then
f=1
fi
i=`expr $i + 1`
done
if [ $f -eq 1 ]
then
echo "The number is composite"
else
echo "The number is Prime"
fi

Вам также может понравиться