0% found this document useful (0 votes)
16 views

Unix Final

This document contains the responses to 21 questions posed to the student Ravi Singh (Roll No: 26). The responses demonstrate Ravi's proficiency with shell scripting through examples of scripts to check if a number is odd/even, swap numbers, check for leap years, and more. The scripts cover basic operations, conditional statements, loops, functions and file permissions.

Uploaded by

pzeel710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Unix Final

This document contains the responses to 21 questions posed to the student Ravi Singh (Roll No: 26). The responses demonstrate Ravi's proficiency with shell scripting through examples of scripts to check if a number is odd/even, swap numbers, check for leap years, and more. The scripts cover basic operations, conditional statements, loops, functions and file permissions.

Uploaded by

pzeel710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Name :- Ravi Singh

Roll No :-26

Name :- Ravi Singh

Roll No :- 26

Subject :- 502 UNIX and Shell Programming

SPID :- 2021057987

SEAT NO :- 2875

Class :- TY B.C.A (FIFTH SEMESTER)

1
Name :- Ravi Singh
Roll No :-26

Q1.write a shell script to check given number is odd or even

Input:-

echo "enter a number"

read no

let ans=$no%2

if [ $ans -eq 0 ]

then

echo "no is even"

else

echo "no is odd"

fi

Output:-

enter a number

10

no is even

2
Name :- Ravi Singh
Roll No :-26

Q2. Write a shell script to swap two numbers

Input:-

echo "enter first number"

read fno

echo "enter second number"

read sno

echo "before swap"

echo "first no value $fno"

echo "second no value $sno"

swno=$fno

fno=$sno

sno=$swno

echo "after swap"

echo "first no value $fno"

echo "second no value $sno"

Output:-

enter first number

10

enter second number

20

before swap

first no value 10

second no value 20

after swap

first no value 20

second no value 10

3
Name :- Ravi Singh
Roll No :-26

Q3. Write a shell script to check whether the year entered is leap year or not.

Input:-

echo "enter a year"

read year

let con1=$year%400

let con2=$year%4

let con3=$year%100

if [ $con1 -eq 0 ]

then

echo "entered year is leap year"

elif [ $con2 -eq 0 ]

then

echo "entered year is leap year"

elif [ $con3 -ne 0 ]

then

echo "entered year is not leap year"

else

echo "entered year is not leap year"

fi

Output:-

enter a year

2012

entered year is leap year

4
Name :- Ravi Singh
Roll No :-26

Q4. Write a shell script to check given number is positive or negative

Input:-

echo "enter a number"

read no

if [ $no -ge 0 ]

then

echo "enter number is positive"

else

echo "enter number is negative"

fi

Output:-

enter a number

50

enter number is positive

5
Name :- Ravi Singh
Roll No :-26

Q5. Write a shell script to print quotient and remainder of two numbers

Input:-

echo "enter a number"

read no

echo "enter divisor"

read div

let d=$no/$div

let r=$no%$div

echo "remainder = $r"

echo "quotient = $d"

Output:-

enter a number

10

enter divisor

remainder = 0

quotient = 5

6
Name :- Ravi Singh
Roll No :-26

Q6. Write a shell script to print first n even number.

Input:-

echo "enter a number"

read no

i=2

j=1

while [ $j -le $no ]

do

echo "$i"

let i=$i+2

let j=$j+1

done

output:-

enter a number

10

7
Name :- Ravi Singh
Roll No :-26

Q7. Write a shell script to print first n odd number.

Input:-

echo "enter a number"

read no

i=1

j=1

while [ $j -le $no ]

do

echo "$i"

let i=$i+2

let j=$j+1

done

Output:-

enter a number

8
Name :- Ravi Singh
Roll No :-26

Q8. Write a shell script to check number is divisible by 5.

Input:-

echo "enter a no"

read no

let ans=$no%5

if [ $ans -eq 0 ]

then

echo "no is divisible by 5"

else

echo "no is not divisible by 5"

fi

Output:-

enter a no

10

no is divisible by 5

9
Name :- Ravi Singh
Roll No :-26

Q9. Write a shell script to check number is prime or not.

Input:-

echo "enter a number"

read no

i=2

while [ $i -lt $no ]

do

let a=$no%$i

if [ $a -eq 0 ]

then

echo "entered no is not prime"

exit

fi

let i=$i+1

done

echo "enter no is prime"

output:-

enter a number

enter no is prime

10
Name :- Ravi Singh
Roll No :-26

Q10. Write a shell script to print first n prime numbers.

Input:-

echo "Enter a limit"

read limit

echo "prime numbers upto $limit are :"

echo "1"

i=2

while [ $i -le $limit ]

do

flag=1

j=2

while [ $j -lt $i ]

do

rem=$(( $i % $j ))

if [ $rem -eq 0 ]

then

flag=0

break

fi

j=$(( $j+1 ))

done

if [ $flag -eq 1 ]

then

echo "$i"

fi

i=$(( $i+1 ))

done

11
Name :- Ravi Singh
Roll No :-26

Output:-

Enter a limit

10

prime numbers upto 10 are :

12
Name :- Ravi Singh
Roll No :-26

Q11. Write a shell script to print factorial of a number.

Input:-

echo "enter a number"

read no

f=1

i=1

while [ $i -le $no ]

do

let f=$f*i

let i=$i+1

done

echo "$f"

Output:-

enter a number

10

3628800

13
Name :- Ravi Singh
Roll No :-26

Q12. Write a shell script to reverse of a number.

Input:-

echo "enter a number"

read no

rev=0

while [ $no -ne 0 ]

do

let rem=$no%10

let rev=$rev*10+$rem

let no=$no/10

done

echo "reverse $rev"

Output:-

enter a number

255

reverse 552

14
Name :- Ravi Singh
Roll No :-26

Q13. Write a shell script to print sum of digits of a given number.

Input:-

echo "enter a number"

read no

sum=0

while [ $no -ne 0 ]

do

let rem=$no%10

let no=$no/10

let sum=$sum+$rem

done

echo "sum $sum"

Output:-

enter a number

25

sum 7

15
Name :- Ravi Singh
Roll No :-26

Q14. Write a shell script to compute the sum of number passed to it as argument from
keyboard and display the result.

Input:-

echo "enter a number"

read n

if [ $n -lt 2 ]

then

echo "enter minimum two argument"

else

sum=0

i=1

while [ $i -le $n ]

do

let sum=$sum+$i

let i=$i+1

done

echo "sum of is = $sum"

fi

Output:-

enter a number

256

sum of is = 32896

16
Name :- Ravi Singh
Roll No :-26

Q15. Write a shell script to find smallest of three number that are read from keyboard

Input:-

echo "enter a first no"

read n1

echo "enter a second no"

read n2

echo "enter a third no"

read n3

if [ $n1 -lt $n2 ] && [ $n1 -lt $n3 ]

then

echo "first no is small"

elif [ $n2 -lt $n1 ] && [ $n2 -lt $n3 ]

then

echo "second no is small"

else

echo "third no is small"

fi

Output:-

enter a first no

15

enter a second no

20

enter a third no

30

first no is small

17
Name :- Ravi Singh
Roll No :-26

Q16. Write a shell script using expr command to read in a string and display a suitable
message if it does not have at least 10 character.

Input:-

echo "enter one string"

read str

#len=$#str

if [ ${#str} -gt 10 ]

then

echo "your length of character is more then 10 hence no problem"

else

echo "your length of character is less then 10 hence you may have problem"

fi

Output:-

enter one string

ravi singh hello

your length of character is more then 10 hence no problem

18
Name :- Ravi Singh
Roll No :-26

Q17. Write a shell script that compute gross salary of an employee according rule given
below

Input:-

echo "enter your basic salary"

read bsala

if [ $bsala -lt 15000 ]

then

let hra=$bsala*10/100

let da=$bsala*90/100

let ltol=$bsala+$da+$hra

echo "less then 15000 salary total $ltol"

elif [ $bsala -ge 15000 ]

then

let hra1=$bsala*5/100

let da1=$bsala*98/100

let gtol=$bsala+$da1+$hra1

echo "greater then 15000 salary total $gtol"

fi

Output:-

enter your basic salary

200520

greater then 15000 salary total 407055

19
Name :- Ravi Singh
Roll No :-26

Q18. Write a shell script that delete all lines containing a specific word for supplies as
argument to it.

Input:-

echo "enter a word"

read str

for i in $*

do

grep -v "$str""$i">temp

if [ $? -ne 0 ]

then

echo "pattern not found"

else

cp termp $i

rm temp

fi

done

Output:-

enter a word

ravi singh

20
Name :- Ravi Singh
Roll No :-26

Q19. Write a shell script that gets executed display the message either “good morning”
”good afternoon” “good evening” depand upon time at which user logs in.

Input:-

x=6

if [ $x -gt 5 ] && [ $x -lt 12 ]

then

echo "good morning"

elif [ $x -gt 12 ] && [ $x -lt 16 ]

then

echo "good afternoon"

elif [ $x -gt 16 ] && [ $x -lt 21 ]

then

echo "good evening"

fi

output:-

good morning

21
Name :- Ravi Singh
Roll No :-26

Q20. Write a shell script that accepts two integer as its argument and computer the
value of first number raised to the power of second number.

Input:-

pow()

a=$1

b=$2

c=1

res=1

if((b==0));

then

res=1

fi

if((a==0));

then

res=0

fi

if((a >= 1 && b >= 1));

then

while((c <= b))

do

res=$((res * a))

c=$((c + 1))

done

fi

echo "$1 to the power $2 is $res"

22
Name :- Ravi Singh
Roll No :-26

A=2

B=4

pow $A $B

output:-

2 to the power 4 is 16

23
Name :- Ravi Singh
Roll No :-26

Q21. Write a shell script to accept a file name and check whether it is exectable or not ,if
not make it executable.

Input:-

echo "enter a file name"

read file

if [ -x $file ]

then

echo "already file has execute mode"

else

chmod +x $file

fi

output:-

enter a file name

demo

already file has execute mode

24
Name :- Ravi Singh
Roll No :-26

Q22. Write a shell script to find the number of user currenty working.

Input:-

let user=who-u

if [ $user -eq 1 ]

then

echo "only one user loggin"

else

echo " no of user logging $user"

fi

Output:-

no of user logging 0

25
Name :- Ravi Singh
Roll No :-26

Q23. Write an awk script to compute gross salary of an employee accordingly to rule
given below.

If basic salary <10000 then HRA = 15% of basic & DA = 45% of basic.

If basic salary >= 10000 then HRA = 20% of basic & DA = 50% of basic.

Input:-

BEGIN{

printf "enter the basic salary:Rs"

getline bp<"/dev/tty"

if(bp<10000)

let hra=15*bp/100

let da=45*bp/100

else

let hra=2*bp/100

let da=5*bp/100

let gs=bp+hra+da

printf "gross salary=Rs.%.2f\n",gs

Output:-

enter the basic salary:Rs

9000

gross salary=Rs 9063

26
Name :- Ravi Singh
Roll No :-26

Q24. Write an awk script to reverse the content of the file. The first line should print
and last line should be printed first.

Input:-

awk '{line[NR]=$10}

END{

for (i=NR;i>0;i--)

print line[i]

}'demo

Output:-

Line 5

Line 4

Line 3

Line 2

Line 1

27
Name :- Ravi Singh
Roll No :-26

Q25. Write an awk script to count the no. of words in the file.

Input:-

BEGIN{print "record.\t characters \t words"}

#BODY section

len=length($0)

total_len =len

print(NR,":\t",len,":\t",NF,$0)

words =NF

END{

print("\n total")

print("characters :\t" total len)

print("lines :\t" NR)

Output:-

Record words

1: 5: 1hello

Total

Characters:5

Lines:1

28

You might also like