Linux Assignment[1]

21
Q1. Write a script to display calendar by inputting date and year. Ans. $ vi calendar.sh echo Enter the month: read m echo Enter the year: Read n cal $m $n Output: sh.calendar.sh Enter the month: 10 Enter the year: 2010 October 2010 Mon Tues Wed Thur Fri Sat Sun 1 2 3

Transcript of Linux Assignment[1]

Page 1: Linux Assignment[1]

Q1. Write a script to display calendar by inputting date and year.

Ans.

$ vi calendar.sh

echo Enter the month:

read m

echo Enter the year:

Read n

cal $m $n

Output:

sh.calendar.sh

Enter the month: 10

Enter the year: 2010

October 2010

Mon Tues Wed Thur Fri Sat Sun

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30 31

Page 2: Linux Assignment[1]

Q2. Write a program to print multiplication of a number.

Ans.

$ vi table.sh

read n

for i in 1 2 3 4 5 6 7 8 9 10

do

t = `expr $n \* $i`

echo $t

done

Output:

sh.table.sh

2

2 4 6 8 10 12 14 16 18 20

Q3. Write a program to display the following series:-

Page 3: Linux Assignment[1]

*

* *

* * *

* * * *

Ans.

$ vi.starseries.sh

i = 1

while [ $i –le 4 ]

do

j = 1

while [ $j –le $i ]

do

echo –n ‘ * ’

j = ` expr $j + 1`

done

echo

i = ` expr $i + 1`

done

Page 4: Linux Assignment[1]

Q4. (a)Write a program to swap two numbers without using third variable.

Ans.

$ vi swapwithoutvariable.sh

Read a

Read b

a = ` expr $a + $b`

b = ` expr $a - $b`

a = `expr $a - $b`

Output:

a = 2

b = 3

a = a + b = 2 +3 = 5

b = a – b = 5-3 = 2

a = a – b = 5-2 = 3

Page 5: Linux Assignment[1]

Q4. (b) Write a program to swap two numbers using a third variable.

Ans.

$ vi swapwithvariable.sh

Read a

Read b

c = $a

b = $c

a = $b

Output:

Read:

a = 2

b = 3

Swap:

c = 2

b = 2

a = 3

Page 6: Linux Assignment[1]

Q5. Write a program to implement case statement.

Ans.

$ vi.switchcase.sh

Read x

case x in

1) echo “HELLO”echo “WORLD”

2) echo “ GOOD”echo “BYE”

*) echo “WRONG”;;

esac

Output

$ sh case.sh

Enter a Number:

5

Good

Bye

Page 7: Linux Assignment[1]

Q6. Script to perform basic math operation using case statement

Ans.

$ vi math.sh

echo “Enter First Number: “

read a

echo “Enter Second Number: “

read b

echo “ Enter a Math Operator ( + , - , \ * , / ) : “

read x

case x

+) echo “The Subtraction is: “

echo -n ` expr $a - $b `

- ) echo “The Addition is: “

echo -n `expr $a + $b `

\ *) echo “The Multiplication is: “

echo -n ` expr $a \* $b `

/) echo “The Division is: “

echo -n ` expr $a / $b `

*) echo “Invalid Math Operator!! “

esac

Page 8: Linux Assignment[1]

Output

$ sh math.sh

Enter First Number:

10

Enter Second Number:

20

Enter a Math Operator ( + , - , \ * , / ) :

+

The Addition is: 30

Page 9: Linux Assignment[1]

Q7. Script to print given number in reverse order

Ans.

$ vi reverse.sh

echo “Enter a Number: “

read n

echo “The Reversed Number is: “

while [ $n -ge 0 ]

do

x = ` expr $n % 10 `

y = ` expr $y + ( $x \ * 10 ) `

n = ` expr $n / 10 `

done

Output

$ sh reverse.sh

Enter a Number:

123

The Reversed Number is:

321

Page 10: Linux Assignment[1]

Q8. Script to print sum of all digits of a given number.

Ans.

$ vi sum.sh

echo “Enter a Number: “

read n

while [ $n -ge 0 ]

do

x = ` expr $n % 10 `

y = ` expr $y + $x `

n = ` expr $n / 10`

done

echo “The Sum of Digits is: “

echo -n $y

Output

$ sh sum.sh

Enter a Number:

1 2 3

The Sum of Digits is: 6

Page 11: Linux Assignment[1]

Q9. Write a script to find out biggest number from three numbers.

Numbers are supplied as command line argument.

Ans.

$ vi biggest.sh

echo “Enter Three Numbers: “

read a

read b

read c

if [ $a -ge $b -a $a -ge $c ]

then echo “The Biggest Number is: “

echo $a

elif [ $b -ge $a -a $b -ge $c ]

then echo “The Biggest Number is: “

echo $b

elif [ $c -ge $a -a $c -ge $b ]

then echo “The Biggest Number is: “

echo $c

Page 12: Linux Assignment[1]

Output

$ sh biggest.sh

Enter Three Numbers:

1

2

3

The Biggest Number is:

3

Page 13: Linux Assignment[1]

Q10. Script to print numbers 5 4 3 2 1 using while loop

Ans.

$ vi series.sh

i = 5

while [ $i –ge 1 ]

do

echo –n $i

i = `expr $i - 1`

done

Output

$ sh series.sh

5 4 3 2 1

Page 14: Linux Assignment[1]

Q11. Write a program to display Fibonacci series till n.

Ans.

$ vi fabonacci.sh

Read n

f = 0

s = , echo $f echo $n

i = 3

while [ $ i –le $n ]

do

z = `expr $f + $s`

echo $z

i = `expr $i + 1`

f = $s

s = $z

done

Page 15: Linux Assignment[1]

Q12. Write a shell script to display factorial.

Ans.

vi fact.sh

read n

f = 1

while [ $n –ge 1 ]

do

f = `expr $f /* $n`

n = `expr $n – 1`

done

echo $f

Output:

n = 5

120

Page 16: Linux Assignment[1]

Q13. How to write shell script that will add 2 numbers which are supplied as command line argument?

Ans.

Read a

Read b

c = ` expr $a + $b`

Output:

a = 20

b = 10

c = 30

Page 17: Linux Assignment[1]

Q14. Write a program using while loop to display:

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Ans.

i = 1

while [ $i –le 5 ]

do

j = 1

while [ $j –le $i ]

do

echo –n $j

j = ` expr $j + 1`

done

echo

i = ` expr $i + 1`

done

Page 18: Linux Assignment[1]

Q15. Write shell script using for loop to print:

1

2 23 3 34 4 4 45 5 5 5 5

Ans. i = 1

for i in 1 2 3 4 5

do

j = 1

while [ $j –le $i ]

do

echo –n $ i

j = ` expr $j + 1`

done

echo

done