[go: up one dir, main page]

0% found this document useful (0 votes)
35 views2 pages

Script 30

The document provides a shell script to determine whether a given date falls on a weekday or weekend. It includes multiple methods for inputting the date and checking the day of the week using the `date` command. The script also includes error handling for invalid date formats.

Uploaded by

for11trade
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)
35 views2 pages

Script 30

The document provides a shell script to determine whether a given date falls on a weekday or weekend. It includes multiple methods for inputting the date and checking the day of the week using the `date` command. The script also includes error handling for invalid date formats.

Uploaded by

for11trade
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
You are on page 1/ 2

Script No .

Definition:
Write a shell script to find a given date fall on a weekday or a weekend.
Script:
clear
echo Enter the date in mm/dd/yyyy format
read d
day= `date -d "$d" +%u`
case $day in
1) echo Weekday Monday;;
2) echo Weekday Tuesday;;
3) echo Weekday Wednesday;;
4) echo Weekday Thursday;;
5) echo Weekday Friday;;
6) echo Weekday Saturday;;
7) echo Weekday Sunday;;
*) echo Provide correct date;;
esac

OR
d=`date +"%u"`
if [ $d -eq 7 ]
then
echo "It is weekend"
else
echo "It is a weekday"
fi

OR
read -p "Enter a date (YYYY-MM-DD): " input_date
if [[ ! $input_date=~^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]];
then
echo "Invalid date formate Plz use YYYY-MM-DD."
exit 1
fi
day=$(date -d "$input_date" +%u)
if [ $day -ge 1 ] && [ $day -le 5 ];
then
echo "$input_date is a weekday(Monday to Friday)."
else
echo "$input_date is weekend day(Saturday or Sunday)."
fi

You might also like