0

When using here script in a linux bash to run commands on a remote server all the lines are printed. How can the comments be suppressed?

The output of the below code should be:

ls
... (whatever is in this folder)

echo -e this is a test\ndone
this is a testndone

exit

Is this possible? The reason for doing this is that the commands and comments are more complex making the output hard to read. That should be prettier.

#!/bin/bash

ssh -tt hogan@123.123.123.123 <<EOF
  # this line get printed
  ls

  # and this comment also
  echo -e this is a test\ndone

  # exit ssh
  exit
EOF

#end of script
hogan
  • 1,434
  • 1
  • 15
  • 32
  • 2
    why not pass in a cmd line argument, which if set, echo's your debug info. you then surround your debug comments with an if statement, checking if some flag was set... say "--debug" or something. – SnakeDoc Apr 06 '16 at 19:44

3 Answers3

2

I usually use sed to filter out comments and blank lines. The following will also strip comments that follow a command on the same line:

#!/bin/bash

sed 's/[[:blank:]]*#.*//; /^$/d' <<EOF | ssh -tt hogan@123.123.123.123
  # this line get printed
  ls

  # and this comment also
  echo -e "this is a test\ndone"

  # exit ssh
  exit
EOF 
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
0

After the command try typing

| grep -v "^[[:space:]]*#"

so for example,

cat temp.txt | grep -v "^[[:space:]]*#"
Jayson
  • 940
  • 8
  • 14
  • When I use cat, it won't execute the commands. It should but the comments should not be sent / executed. – hogan Apr 06 '16 at 20:00
  • cat is just a command to read text from a file and output that to stdout. That was just for an example. You'd actually run something like ./my_script.sh | grep -v "^[[:space:]]*#" – Jayson Apr 06 '16 at 20:14
0

I think you might be asking for this

grep -v '^[[:space:]]*#' <<EOF | ssh -tt hogan@123.123.123.123  
  # this line get printed
  ls

  # and this comment also
  echo -e this is a test\ndone

  # exit ssh
  exit
EOF
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134