Objective: Use a shell script to read a year and check whether it is a leap year or not.
A leap year comes every 4 years. But it is not a leap year if it is divisible by 100 and not by 400. So, 1700, 1800, 1900, 2100, and 2200 are not leap years. But 1600, 2000, and 2400 are leap years.
Below is simple bash shell script that can be used to determine if a given year is a leap year or not.
1 2 3 4 5 6 7 8 |
#!/bin/bash year=0 leapyear=0 read -p "Enter year: " year [ $(($year % 4)) -eq 0 ] && ([ $(($year % 100)) -ne 0 ] || [ $(($year % 400)) -eq 0 ]) && leapyear=1 echo -n "Is it a leap year? " ; [ $leapyear -eq 0 ] && echo "No" || echo "Yes" |