Example 1
#! /bin/bash
#this program gets two numbers from the user and compare them
read -p "Enter two numbers : " num1 num2
echo "The numbers are $num1 and $num2"
if [ "$num1" -eq "$num2" ]; then
echo "the numbers are equal"
elif [ $num1 -gt $num2 ]; then
echo "num1 is greater than num2"
else
echo "num1 is less than num2"
fi
Example 2
#! /bin/bash
if [ -z $1 ]; then
echo "Error: filename not given"
echo "This utility backups a given file with an extension .bk"
echo "Usage: if-demo.sh "
exit 1
elif [ -d $1 ]; then
echo "Error: "
echo "directory found, instead of a file. This utility does not backup directory"
exit 1
fi
cp "$1" "$1.bk"
Another example
#! /bin/bash
read -p "Enter the age" age
read -p "Enter the price" price
if [ $age -gt 65 ]; then
echo "Senior discount is applied."
echo "Please pay:"
echo "scale=2; $price*0.95" | bc
elif [ $age -gt 17 ]; then
echo "Please pay: $price"
else
echo "You cannot alcohol"
fi
Comments
Post a Comment