0% found this document useful (0 votes)
10 views8 pages

Practical 10

Uploaded by

Ekrama Ansari
Copyright
© © All Rights Reserved
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)
10 views8 pages

Practical 10

Uploaded by

Ekrama Ansari
Copyright
© © All Rights Reserved
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
You are on page 1/ 8

DEPARTMENT OF COMPUTER ENGINEERING

Subject: Operating System Subject Code:22516


Semester: 5th Semester Course: Computer Engineering
Laboratory No: L004 Name of Subject Teacher: Prof. Natasha
Bhrahme
Name of Student: Ekrama Ansari Roll Id: 22203C0010
Experiment No: 10
Title of Experiment Execute shell script by using if statement
.
Practical Set questions

1. Write a shell script to count the number of vowels in the given string.

Ans:
#!/bin/bash echo

"Enter a string:" read

input_string

vowel_count=0

input_string=$(echo "$input_string" | tr '[:upper:]' '[:lower:]')

for (( i=0; i<${#input_string}; i++ )); do

char="${input_string:$i:1}"

if [[ "$char" == "a" || "$char" == "e" || "$char" == "i" || "$char" == "o" || "$char" == "u" ]];

then vowel_count=$

((vowel_count+1))

fi

done

Output:

2. Write a shell script to check whether the enter age is eligible for vote or
not.

Ans:

Page|1
#!/bin/bash

echo “Enter your age”

read num if [num -le

18 ]

then

echo “$num age is valid to vote”

else

echo “$num age is invalid to vote”

fi

Output:

X. Program code.

1. Execute shell script by considering example to find passing grades of


students using if statement:

1. Single Decision.

2. Double Decision.

3. Multiple if statements.

Ans:

1. Single Decision:

Page|2
#!/bin/bash echo "Enter the student's

grade (0-100):" read grade if [ $grade -

ge 50 ];

then echo "The student has

passed."

fi

Output:

2. Double Decision:
#!/bin/bash echo "Enter the student's

grade (0-100):" read grade if [ $grade -

ge 50 ];

then echo "The student has

passed."

else

echo "The student has failed."

Fi

Output:

Page|3
3. Multiple if statement:
#!/bin/bash echo "Enter the student's

grade (0-100):" read grade if [ $grade -

ge 90 ];

then

echo "The student got an A."

elif [ $grade -ge 80 ]; then

echo "The student got a B."

elif [ $grade -ge 70 ]; then

echo "The student got a C."

elif [ $grade -ge 60 ]; then

echo "The student got a D."

else

echo "The student has failed."

fi

Output:

XII. Practical related Questions

1. Write and execute script for nested if statement.

Ans:
#!/bin/bash

Page|4
echo "Enter a number:"

read number if

[ $number -gt 0 ];

then echo "The number is

positive." if [ $number -gt 100

];

then

echo "The number is greater than 100."

else

echo "The number is less than or equal to 100."

fi

elif [ $number -lt 0 ];

then else

fi

echo "The number is negative." echo "The number is zero."

Output:

2. Write difference between if [condition], if ((condition)) Ans:


if[condition] If((condition))

The single brackets […] is the command The double parentheses ((…)) is the format for
bash arithmetic expansion.

It is used to create commands in statements. It is used to test an arithmetic operation.

Page|5
Syntax:if Syntax:if

[condition] then ((condition)) then

### series of code fi Statement goes here fi

Example:if [“X” -lt “0”] Example:if (($num -eq 42)) then

then echo “X is less than echo “num is actually equal to 42”

zero” else

fi echo “num is not equal to 42” fi

3. Write script for finding greatest number among the given


numbers.

Ans:
#!/bin/bash

echo "Enter number1: " read num1

echo "Enter number2: " read num2

echo "Enter number3: " read num3 if

[ "$num1" -ge "$num2" ] &&

[ "$num1" -ge "$num3" ]

then

echo "$num1 is greatest among 3 numbers." elif [ "$num2"

-ge "$num1" ] && [ "$num2" -ge "$num3" ]

then

echo "$num2 is greatest among 3 numbers."

else

echo "$num3 is greatest among 3 numbers."

Fi

Output:

Page|6
XIII. Exercise:

1. Correct the following script and write its output.


i. if [! -r “$l”] then echo “File $l is not readable-skipping.”; fi
ii. if [“$X” -nt “/etc/passwd”]; then echo “X is a file
which is newer than/etc/passwd” f Ans:

i. if [! -r “$l”] then echo “File $l is not readable-skipping.”;

fi Error:

Correct code:

read l if [! -r “$l”] then echo “File $l is

not readable-skipping.”

Fi

ii. if [“$X” -nt “/etc/passwd”]; then echo “X is a file which is


newer than/etc/passwd” f Error:

Correct code:

Page|7
read x if [$X -nt “/etc/passwd”]; then echo “$X is

a file which is newer than/etc/passwd”

fi

Conclusion: We successfully executed shell script by using if statement (Single decision,


Double decision, Multiple if conditions)

Grade and Process Related Product Related Dated Sign


Dated (35) (15)
Signature of
Teacher

Page|8

You might also like