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

Essential Bash Scripts with Outputs

Linux program

Uploaded by

aruna25825
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)
4 views4 pages

Essential Bash Scripts with Outputs

Linux program

Uploaded by

aruna25825
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

Linux (Bash) Programs with Outputs

1. Print Message
----------------
#!/bin/bash
echo "Hello Linux"

Output:
Hello Linux

2. Read Two Numbers and Print


-----------------------------
#!/bin/bash
read -p "Enter number1: " a
read -p "Enter number2: " b
echo "You entered: $a and $b"

Output:
Enter number1: 5
Enter number2: 10
You entered: 5 and 10

3. Sum of Two Numbers


---------------------
#!/bin/bash
read -p "Enter a: " a
read -p "Enter b: " b
sum=$((a+b))
echo "Sum is $sum"

Output:
Enter a: 5
Enter b: 7
Sum is 12

4. Area of Rectangle Using Parameters


-------------------------------------
#!/bin/bash
l=$1
b=$2
area=$((l*b))
echo "Area = $area"

Output:
$ ./[Link] 5 6
Area = 30

5. Swapping Two Numbers


------------------------
#!/bin/bash
read -p "Enter a: " a
read -p "Enter b: " b
temp=$a
a=$b
b=$temp
echo "After swap: a=$a b=$b"

Output:
Enter a: 4
Enter b: 9
After swap: a=9 b=4

6. Check Even or Odd


---------------------
#!/bin/bash
read -p "Enter number: " n
if (( n % 2 == 0 ))
then
echo "Even"
else
echo "Odd"
fi

Output:
Enter number: 7
Odd

7. Grade Calculation Using Nested If


------------------------------------
#!/bin/bash
read -p "Enter subject1: " s1
read -p "Enter subject2: " s2
read -p "Enter subject3: " s3
total=$((s1+s2+s3))
avg=$((total/3))

if (( avg >= 80 ))
then
echo "Grade A"
else
if (( avg >= 60 ))
then
echo "Grade B"
else
echo "Grade C"
fi
fi

Output:
Enter marks: 78 67 89
Grade A

8. Greatest of Three Numbers (Nested if)


-----------------------------------------
#!/bin/bash
read -p "Enter three numbers: " a b c
if (( a > b ))
then
if (( a > c ))
then echo "Greatest is $a"
else echo "Greatest is $c"
fi
else
if (( b > c ))
then echo "Greatest is $b"
else echo "Greatest is $c"
fi
fi

Output:
Enter three numbers: 10 99 34
Greatest is 99

9. Calculator Using Case Statement


----------------------------------
#!/bin/bash
echo "[Link] [Link] [Link] [Link]"
read -p "Choose: " ch
read -p "Enter a: " a
read -p "Enter b: " b

case $ch in
1) echo "Sum = $((a+b))" ;;
2) echo "Sub = $((a-b))" ;;
3) echo "Mul = $((a*b))" ;;
4) echo "Div = $((a/b))" ;;
*) echo "Invalid" ;;
esac

Output:
Choose: 1
Enter a: 5
Enter b: 6
Sum = 11

10. While Loop – Sum of Digits


-------------------------------
#!/bin/bash
read -p "Enter number: " n
sum=0
while (( n > 0 ))
do
r=$((n%10))
sum=$((sum+r))
n=$((n/10))
done
echo "Sum of digits: $sum"

Output:
Enter number: 123
Sum of digits: 6

Reverse Digits:
---------------
#!/bin/bash
read -p "Enter number: " n
rev=0
while (( n > 0 ))
do
r=$((n%10))
rev=$((rev*10+r))
n=$((n/10))
done
echo "Reverse: $rev"

Output:
Enter number: 456
Reverse: 654

11. Fibonacci Series and Factorial Using For Loop


-------------------------------------------------
#!/bin/bash
read -p "Enter n: " n

# Fibonacci
a=0; b=1
echo "Fibonacci:"
for ((i=0;i<n;i++))
do
echo -n "$a "
fn=$((a+b))
a=$b
b=$fn
done

# Factorial
fact=1
for ((i=1;i<=n;i++))
do
fact=$((fact*i))
done
echo -e "\nFactorial = $fact"
Output:
Enter n: 5
Fibonacci:
0 1 1 2 3
Factorial = 120

You might also like