如何在linux ssh脚本中压制注释

时间:2016-04-06 19:39:15

标签: linux bash ssh heredoc

在linux bash中使用此脚本在远程服务器上运行命令时,将打印所有行。如何抑制评论?

以下代码的输出应为:

ls
... (whatever is in this folder)

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

exit

这可能吗? 这样做的原因是命令和注释更复杂,导致输出难以阅读。那应该更漂亮。

#!/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

3 个答案:

答案 0 :(得分:2)

我通常使用sed过滤掉评论和空白行。以下内容还将删除同一行上命令后面的注释:

#!/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 

答案 1 :(得分:0)

命令后尝试输入

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

所以,例如,

cat temp.txt | grep -v "^[[:space:]]*#"

答案 2 :(得分:0)

我想你可能会要求这个

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