0% found this document useful (0 votes)
396 views18 pages

Q1. Write A Script To Display Calendar by Inputting Date and Year

The document contains 15 questions about writing shell scripts in Bash to perform various tasks like displaying calendars, multiplication tables, patterns of stars and numbers, reversing numbers, calculating sums and largest numbers, Fibonacci series, factorials, and adding numbers passed as command line arguments. For each question, it provides the code to write a shell script to produce the given output.

Uploaded by

rajaisha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
396 views18 pages

Q1. Write A Script To Display Calendar by Inputting Date and Year

The document contains 15 questions about writing shell scripts in Bash to perform various tasks like displaying calendars, multiplication tables, patterns of stars and numbers, reversing numbers, calculating sums and largest numbers, Fibonacci series, factorials, and adding numbers passed as command line arguments. For each question, it provides the code to write a shell script to produce the given output.

Uploaded by

rajaisha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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
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 4 6 8 10 12 14 16 18 20

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


*
* *
* * *
* * * *
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
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
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
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:

Good

Bye
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
Output

$ sh math.sh

Enter First Number:

10

Enter Second Number:

20

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

The Addition is: 30


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
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:

123

The Sum of Digits is: 6


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
Output

$ sh biggest.sh

Enter Three Numbers:

The Biggest Number is:

3
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

54321
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
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
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
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
Q15. Write shell script using for loop to print:
1
2 2
3 3 3
4 4 4 4
5 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

You might also like