Shell Script Comment code blocks in a bash source file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script Comment code blocks in a bash source file
# 1  
Old 12-01-2016
Bug 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:
Code:
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:
Code:
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

Last edited by HiFuture0801; 12-01-2016 at 03:49 PM.. Reason: Provide Example
# 2  
Old 12-01-2016
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.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dividing tab blocks in bash script

Hi everyone, I have a data.xml file which only contains thousands of data (tag) blocks. A part of the file looks exactly like this; <data> Line Line Line </data> <data> Line Line Line </data> the rest of the file is simply a repetition of this part. Here each data block contains a... (8 Replies)
Discussion started by: hayreter
8 Replies

2. Shell Programming and Scripting

Need to comment some code via script

Hi, I am writing a shell script, which should comment a if-fi loop in a file. From google i found that, we can use below qualifier to comment a section of code, :<<COMMENT COMMENT But i could not place these keywords before and after if-fi loop. Any help would be appreciated. Finally the... (2 Replies)
Discussion started by: successlin
2 Replies

3. Shell Programming and Scripting

HOW to run a bash-code in a c-shell script ??

Is there a way to run some code in a C-shell script by different shell, like bash? I have that situation. I have prepared and perfectly workable bash-skript that now I have to execute under C-shell script, divide it on steps and without creating a new files (with the bash-code parts.) For... (6 Replies)
Discussion started by: alex_5161
6 Replies

4. Shell Programming and Scripting

how to split this file into blocks and then send these blocks as input to the tool called Yices?

Hello, I have a file like this: FILE.TXT: (define argc :: int) (assert ( > argc 1)) (assert ( = argc 1)) <check> # (define c :: float) (assert ( > c 0)) (assert ( = c 0)) <check> # now, i want to separate each block('#' is the delimeter), make them separate files, and then send them as... (5 Replies)
Discussion started by: paramad
5 Replies

5. Shell Programming and Scripting

best way for removing comment from shell scripts -- bash

Again a comment removal requirement from me, refer my previous problem & solution for removing comment from ruby scripts: https://www.unix.com/shell-programming-scripting/118296-best-way-removing-comment-ruby-program.html This time, it is for stripping of comments from Shell Script. I search for... (2 Replies)
Discussion started by: thegeek
2 Replies

6. Shell Programming and Scripting

Shell Script to display function names (called & defined) in a C++ Source Code

Hello to all, I am looking for a way to display only the names of function (calls & definition) of a C++ source code.There is already a post related to this, but the script is to find the functions using a specific variable, and the replies are not that convincing since they cannot be used for... (2 Replies)
Discussion started by: frozensmilz
2 Replies

7. Shell Programming and Scripting

Source Env file in the Shell Script

Hi I am having a script which sets the application environment. In this script i am sourcing the applications env file, when i am debugging the script i see its executing all the environment values and all the variable values are set properply. Once this main shell script executes, then... (4 Replies)
Discussion started by: umakanthly
4 Replies

8. Shell Programming and Scripting

Tidy shell script source file

Hi all, Is there a tool to tidy up shell script source file and reformat it. Preferably I am looking for a perfect tool to do this. Also I have heard it is possible to do with emacs editor but unfortunately it is looking very different and new from vi. Emacs is new to me and I could not find... (1 Reply)
Discussion started by: meharo
1 Replies

9. Shell Programming and Scripting

Protect shell script source code

Hello, Is there any way where a shell scripts source code can be protected? Basically, I'm after to convert shell scripts into to some form of binaries so that my shell script source code can be protected. Forgot to mention, this is on solaris. Many thx, kam (6 Replies)
Discussion started by: jkkstar
6 Replies

10. Shell Programming and Scripting

Bash Source Code

Can somebody give me the link to get the source code of BASH? (1 Reply)
Discussion started by: bhargava
1 Replies
Login or Register to Ask a Question