【问题标题】:How to suppress comments in linux ssh here script如何在 linux ssh here 脚本中禁止注释
【发布时间】:2023-03-15 07:07:01
【问题描述】:

在 linux bash 中使用 here 脚本在远程服务器上运行命令时,所有行都会打印出来。 cmets如何被抑制?

下面代码的输出应该是:

ls
... (whatever is in this folder)

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

exit

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

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

【问题讨论】:

  • 为什么不传入一个命令行参数,如果设置,echo 就是你的调试信息。然后你用 if 语句包围你的调试 cmets,检查是否设置了一些标志......说“--debug”或其他东西。

标签: linux bash ssh heredoc


【解决方案1】:

我通常使用sed 来过滤掉 cmets 和空行。以下内容也将删除同一行命令后面的 cmets:

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

【讨论】:

  • 这很接近,但它似乎也过滤掉了命令,以便只显示输出。它不在你身边吗?
  • 你是对的!我有 ssh 和 sed 倒退。我已经编辑了我的答案。
【解决方案2】:

命令后尝试输入

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

例如,

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

【讨论】:

  • 当我使用 cat 时,它不会执行命令。它应该但不应该发送/执行 cmets。
  • cat 只是一个从文件中读取文本并将其输出到标准输出的命令。这只是一个例子。你实际上会运行类似 ./my_script.sh | grep -v "^[[:space:]]*#"
【解决方案3】:

我想你可能会问这个

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

【讨论】:

    猜你喜欢
    • 2011-11-30
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2022-08-18
    • 2010-10-10
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    相关资源
    最近更新 更多