Articles

Shell Script to Check Leap Year

Shell Script to Check Leap Year


In this shell script program, we will learn to write:

  • shell script to check leap year or not

To solve leap year in shell script more easily, you have to know some basic concepts like:

  1. Clear command
  2. Echo command
  3. Taking input from user.
  4. If-else statements
  5. Module operation

LEAP YEAR RULES:

Every four years, a leap year come once. Per year contain 365.24 days but to correct and keep track of the solar calendar, we introduced leap year concept. Unlike regular years( 365 days), leap year has 366 days.

  • A year will be leap year if it is divided by 4 and if it’s remainder is equal to 0.
  • If above result is equal to 0, then we will consider that given year by the user is a leap year otherwise it is not.

ALGORITHM TO CHECK LEAP YEAR:

  • STEP 1: START SHELL SCRIPT
  • STEP 2: CLEAR THE SCREEN.
  • STEP 3: TAKE A YEAR INPUT BY THE USER.
  • STEP 4: IMPLEMENT IF-ELSE STATEMENT.
  • STEP 5: GIVE CONDITION #YEAR % 4
  • STEP 6: IF RESULT ==0 THEN IT IS LEAP YEAR OTHERWISER IT IS NOT
  • STEP 7: STOP SHELL SCRIPT

Shell script to check leap year or not

Given below is a shell script for checking leap year:

clear
echo "LEAP YEAR SHELL SCRIPT"
echo -n "Enter a year:"
read year_checker
if [ `expr $year_checker % 4` -eq 0 ]
then
	echo "$year_checker is a leap year"
else
	echo "$year_checker is not a leap year"
fi

CODE IMAGE:

shell script to check leap year or not

OUTPUT:

LEAP YEAR SHELL SCRIPT
Enter a year:2020
2020 is a leap year


shell script to check leap year

 


Shell Script Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Suraj Maity
Page Views :    Published Date : Jan 23,2021  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!