You are on page 1of 4

Lab 5 (31-10-2023)

Shell Scripting

Student Name: ______________________Roll No________________

Task 1:
Create a Bash script that asks the user for their age. If the age is less than 18, the script should
print "You are a minor," otherwise, it should print "You are an adult."
#! /usr/bin/bash
read -p "Enter your age" age
if (($age >= 18))
then
echo "You are adult"

else
echo "You are kid"
fi
Task 2:
Write a Bash script that uses a for loop to print the numbers from 1 to 10 on separate lines.
#! /usr/bin/bash
for n in {1..10};
do
echo $n
done
Task 3:
Create a Bash script that uses a while loop to count down from 10 to 1, printing each number on
a new line. Once it reaches 1, it should print "Blastoff!".
#! /usr/bin/bash
num1=10
while (($num1 > 0));
do
echo $num1
((num1--))
done
echo "blast"
Task 4:
Write a Bash script that uses an until loop to ask the user to enter their password. Keep asking
until the correct password "secret123" is entered.
num1=0
while num1=0
do
read -p "Entr password= " inp
if ((inp=="secret123"))
then
echo "correct password"

break

else
echo "Wrong password"
fi
done

Task 5:
Create a Bash script that asks the user for a number. If the number is even, the script should print
"The number is even." If it's odd, the script should print "The number is odd." Use a loop to
repeatedly ask the user for numbers until they enter 0, at which point the script should exit.
#! /usr/bin/bash
num1=0
while num1=0
do
read -p "Entr number= " inp
if ((inp==0))
then
echo "Program exit"
break
fi
if ((inp%2==0))
then
echo "it is even"

else
echo "It is odd"
fi
done
Task 6:
Create a Bash script that calculates and prints the sum of all numbers from 1 to 100 using a for
loop.
#! /usr/bin/bash
total=0
for n in {1..100}
do
((total=total+n))
done
echo $total
Task 7:
Create a Bash script that uses a loop to generate a sequence of numbers from 1 to 20. For each
number, check if it's divisible by both 3 and 5, and if so, print "FizzBuzz." If it's only divisible by
3, print "Fizz." If it's only divisible by 5, print "Buzz." Otherwise, print the number itself.
#! /usr/bin/bash
for n in {1..20}
do
echo "Number= " $n
if ((n%3==0 && n%5==0))
then
echo "Result= FizzBuzz"
elif ((n%3==0))
then
echo "Result= Fizz"
elif ((n%5==0))
then
echo "Result= Buzz"
else
echo "No result"
fi
done

Task 8:
Write a Bash script that allows the user to perform basic arithmetic operations (addition,
subtraction, multiplication, division) on two numbers. Ask the user for the operation to perform
and then calculate and display the result.
#! /usr/bin/bash
total=0
while total=0
result=0
do
echo "Enter1 for addition"
echo "Enter2 for subtraction"
echo "Enter3 for multiplication"
echo "Enter4 for divison"
echo "Enter 5 for exit"
read -p "Enter 1st number" num1
read -p "Enter 2nd number" num2
read -p "Enter Operation [1,2,3,4]= " choice
if ((choice==5))
then
echo "Program exit"
break
case $choice in

1)
result=$((num1+num2))
echo "Result of addition = " $result
2)
result=$((num1-num2))
echo "Result of subtraction = " $result

3)
result=$((num1*num2))
echo "Result of multiplication= " $result

4)
if ((num2==0))
then
echo "Divison not possible"
else
result=$((num1/num2))
echo "Result of divison = " $result
fi
;;
*)
echo "invalid"
;;
esac
done

You might also like