-3

Assuming I have a bash script test.sh:

#!/bin/bash

# Comment 1
echo "Hello world"

# Comment 2
echo "Hi there"

I know after making the bash script executable and running it, only the echo commands would show as output.

I'm wondering if there's a way to print the comments on execution. That is, I want the output to look like:

Comment 1
Hello world

Comment 2
Hi there
razor_chk
  • 77
  • 2
  • 9
  • 2
    Why not just replace the `#` with `echo` ? Won't be possible to print the comments unless you write a wrapper the does something like `sed 's/^#/echo /'` and then runs the modified script – git-bruh Sep 27 '21 at 04:08
  • The purpose of printing the comments is not very clear at all, if you want to print the comments just to check the code flow just print them anyway, or if you want something like comment detection, you will need some external tool or script as suggested by @git-bruh – Ankush Pandit Sep 27 '21 at 04:41
  • 3
    @razor_chk : The idea of having comments to be printed, sounds bizarre to me. There are several commands available for printing information (`echo`, `printf` and others). One idea to consider is to use the `:` command to provide the comment, i.e. `: Comment1`. If you are running your code with `-x` turned on, you see these "dummy comments"; without `-x`, you don't see them. – user1934428 Sep 27 '21 at 07:41

3 Answers3

0

if there's a way to print the comments on execution

No, there is no way.

a way

Modify bash source and modify langauge parser to parse # characters to work as ; echo.

In trivially simple cases that do not need a real blown parser, you could replace all # in the source file by echo and run it then. Like sed 's/#/echo/' test.sh | bash -s.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

Like what user1934428 said

Not exactly what you want but if you set the shebang to #!/bin/sh -x (or -v) it will print out both the comments and the commands in the script

Additional Reference: https://stackoverflow.com/a/2853811/10359765

evantkchong
  • 2,251
  • 3
  • 14
  • 29
0

A common arrangement is to have various logging levels set up, and then emit different amounts of logging depending on the logging level.

debug () { ${debug-:} "$0: $@" >&2; }
info () {  ${info-:}  "$0: $@" >&2; }
warn () {  ${warn-:}  "$0: $@" >&2; }
error () { ${error-:} "$0: $@" >&2; }

case $1 in
  --debug) debug=echo; info=echo;  warn=echo;  error=1;;
  --info)  info=echo;  warn=echo;  error=1;;
  --warn)  warn=echo;  error=echo;;
  --error) error=echo;;
  -*) echo "$0: unknown option '$1' -- aborting" >&2
      exit 127;;
esac
shift

debug here we go
info starting
echo "Hello world"
warn the end is nigh
echo "Hi there"
error we die here

This is just a dead simple demo; you would probably want for it to be somewhat more secure and versatile, and have sensible default like running with --warn (which implies --error) if no level is explicitly specified.

The "comments" are no longer comments, and the output goes to standard error in this implementation; but as you can see, this also lets you put in "comments" which are actually useful when you are debugging, not just while reading the code.

You can make the commands (debug, warn etc) look more like comments by renaming them, perhaps to _dbg_, _warn_ etc.

tripleee
  • 175,061
  • 34
  • 275
  • 318