How to write script in bash.

I am very new to Linux/Unix. Kindly assist the following:

I wish to write a bash shell script called how_many_to_go that calculates and prints the number of days, hours, minutes and/or seconds until the end of the current month (based on the output of the date command).

  • Do not make any assumptions about what month it is - the script should run properly in any month.
  • Assume that it is not a leap year, so the number of days in any month is known (February has 28 days; April, June, September, and November have 30 days; All the rest have 31 days).
  • The output should read "There are d days, h hours, m minutes, and s seconds until the end of the month" except as modified by the command line arguments below.
  • The script should accept command line arguments as follows (assume that it's now 11:00:00 PM on January 30):
  • [list]
  • how_many_to_go - print the number of days, hours, minutes, and seconds until the end of the month (e.g. "There are 1 days, 1 hours, 0 minutes, and 0 seconds until the end of the month")
  • how_many_to_go h - print the number of hours, minutes, and seconds until the end of the month (e.g. "There are 25 hours, 0 minutes, and 0 seconds until the end of the month")
  • how_many_to_go m - print the number of minutes and seconds until the end of the month (e.g. "There are 1500 minutes and 0 seconds until the end of the month")
  • how_many_to_go s - print the number of seconds until the end of the month (e.g. "There are 90000 seconds until the end of the month")
    [/list]

Kenyatta University Kenya. Lecturer: Dr. Waweru Michael. Course Code. BBIT 202.

Regards

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.

Hi alobi,
Here is the script -

num=$#
option=$1
typeset -R2 lastday=$(cal)
currdate=`date "+%d"`
days_to_go=`expr $lastday - $currdate`
hrs_gone=`date "+%H"`
mins_gone=`date "+%M"`
secs_gone=`date "+%S"`
secs_gone=`expr $secs_gone + $mins_gone \* 60`
secs_gone=`expr $secs_gone + $hrs_gone \* 60 \* 60`
secs_to_go=`expr $days_to_go \* 24 \* 60 \* 60`
secs_to_go=`expr $secs_to_go - $secs_gone`
if [ $num -gt 0 ]
then
if [ $option == "-s" ]
then
echo "Time left is 0 : 0 : 0 : $secs_to_go"
elif [ $option == "-m" ]
then
secs=`expr $secs_to_go % 60`
mins=`expr $secs_to_go / 60`
echo "Time left is 0 : 0 : $mins : $secs"
elif [ $option == "-h" ]
then
secs=`expr $secs_to_go % 60`
mins=`expr $secs_to_go / 60`
hrs=`expr $mins / 60`
mins=`expr $mins % 60`
echo "Time left is 0 : $hrs : $mins : $secs"
fi
else
secs=`expr $secs_to_go % 60`
mins=`expr $secs_to_go / 60`
hrs=`expr $mins / 60`
mins=`expr $mins % 60`
days=`expr $hrs / 24`
hrs=`expr $hrs % 24`
echo "Time left is $days : $hrs : $mins : $secs"
fi