Shell Script Comment code blocks in a bash source file

Just began to learn on Shell Script. I got an exercise from my friend. I know how to make this happen in C, but I'm not familiar with Shell Script. Hope I can get some help from all of you.
I want to write a bash script to comment code blocks in a bash source file. What I mean comment is '#', I know that there is a build in command : would help. But I still have no idea how should I start this.
Should I read the source file first, Then output it as another file?

Here is an example:

Original Source File:

Final=$(date -d "2016-12-15 14:00" "+%j")
while true ; do
  Today=$(date "+%j")
  Days=$((Final - Today))
  if (( Days >= 14 )) ; then
    echo party
  elif (( Days >= 2 )) ; then
    echo study
  elif (( Days == 1 )) ; then
    for Count in 1 2 3   # loop 2
      do
      echo panic
    done   # loop 2
  else
    break
  fi
  sleep 8h
done

After Script:

Final=$(date -d "2016-12-15 14:00" "+%j")
while true ; do   # loop 1
  Today=$(date "+%j")
  Days=$((Final - Today))
  if (( Days >= 14 )) ; then   # selection 1
    echo party
  elif (( Days >= 2 )) ; then
    echo study
  elif (( Days == 1 )) ; then
    for Count in 1 2 3   # loop 2
      do
      echo panic
    done   # loop 2
  else
    break
  fi   # selection 1
  sleep 8h
done   # loop 1

Here is some other requirement:
Read from standard input, write to standard output.
At the end of any line containing while until for add the comment: # loop n
At the end of any line containing done add the comment: # loop n
At the end of any line containing if case add the comment: # selection n
At the end of any line containing fi esac add the comment: # selection n

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.

1 Like