0% found this document useful (0 votes)
4 views4 pages

Shell

The document contains multiple Bash scripts that perform various tasks such as displaying the current date and time, performing arithmetic operations, checking if numbers are even or odd, determining if a number is positive or negative, calculating the sum of natural numbers, finding the factorial of a number, summing odd and even digits, and checking if a number is prime. Each script uses basic Bash commands and control structures to achieve its functionality. The scripts are designed for user interaction through input prompts.

Uploaded by

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

Shell

The document contains multiple Bash scripts that perform various tasks such as displaying the current date and time, performing arithmetic operations, checking if numbers are even or odd, determining if a number is positive or negative, calculating the sum of natural numbers, finding the factorial of a number, summing odd and even digits, and checking if a number is prime. Each script uses basic Bash commands and control structures to achieve its functionality. The scripts are designed for user interaction through input prompts.

Uploaded by

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

#!

/bin/bash

echo "Today is $(date +"%B %d %Y")"


echo "Today is $(date +"%A")"
echo "Time is $(date +"%I:%M:%S %p")"

#!/bin/bash

# Numbers
num1=5
num2=10

sum=$((num1 + num2))
echo "Sum of $num1 and $num2 is: $sum"

#!/bin/bash

read -p "Enter first number: " num1


read -p "Enter second number: " num2

sum=$((num1 + num2))
sub=$((num1 - num2))
mul=$((num1 * num2))
div=$((num1 / num2))

echo "Addition: $sum"


echo "Subtraction: $sub"
echo "Multiplication: $mul"
echo "Division: $div"

#!/bin/bash

# Numbers
num1=5
num2=10

if [ $num1 -gt $num2 ]; then


echo "$num1 is greater than $num2"
else
echo "$num2 is greater than $num1"
fi
#!/bin/bash

read -p "Enter a number: " num

if [ $((num % 2)) -eq 0 ]; then


echo "$num is even"
else
echo "$num is odd"
fi

#!/bin/bash

read -p "Enter a number: " num

if [ $num -gt 0 ]; then


echo "$num is positive"
elif [ $num -lt 0 ]; then
echo "$num is negative"
else
echo "$num is zero"
fi

#!/bin/bash

read -p "Enter a number: " n


sum=0
i=1

while [ $i -le $n ]; do
sum=$((sum + i))
i=$((i + 1))
done

echo "The sum of the first $n natural numbers is: $sum"

#!/bin/bash

read -p "Enter a number: " num


fact=1
i=1

while [ $i -le $num ]; do


fact=$((fact * i))
i=$((i + 1))
done
echo "The factorial of $num is: $fact"

#!/bin/bash

read -p "Enter a number: " num


sum_odd=0
sum_even=0

# Loop to calculate the sum of odd and even digits


while [ $num -gt 0 ]; do
digit=$((num % 10))
if [ $((digit % 2)) -eq 0 ]; then
sum_even=$((sum_even + digit))
else
sum_odd=$((sum_odd + digit))
fi
num=$((num / 10))
done

echo "Sum of even digits: $sum_even"


echo "Sum of odd digits: $sum_odd"

#!/bin/bash

read -p "Enter a number: " num


is_prime=1
i=2

if [ $num -lt 2 ]; then


is_prime=0
else
while [ $i -le $((num / 2)) ]; do
if [ $((num % i)) -eq 0 ]; then
is_prime=0
break
fi
i=$((i + 1))
done
fi

if [ $is_prime -eq 1 ]; then


echo "$num is a prime number"
else
echo "$num is not a prime number"
fi

You might also like