我有一个复杂的命令通过 ssh 传递到远程服务器。我正在尝试解压缩一个文件,然后在第二个 ssh 命令中更改其命名结构和扩展名。我的命令是:
ssh root@server1 "gzip -d /tmp/file.out-20171119.gz; echo file* | awk -F'[.-]' '{print $1$3".log"}'"
显然, print 语句的 .log 部分周围的 " 让我失望了。我的想法是,我将从文件名中删除 .out 部分,并以 file20171119.log 作为最终结果。我只是有点困惑语法或如何正确转义以便 bash 正确解释 .log。
处理这个问题最简单的方法就是避免它。不要费心尝试转义脚本以进入命令行:而是将其传递到标准输入上。
ssh root@server1 bash -s <<'EOF'
gzip -d /tmp/file.out-20171119.gz
# note that (particularly w/o a cd /tmp) this doesn't do anything at all related to the
# line above; thus, probably buggy as given in the original question.
echo file* | awk -F'[.-]' '{print $1$3".log"}'
EOF
引用的heredoc——其中一个<<'EOF'
or <<\EOF
代替<<EOF
-- 按字面意思传递,没有任何 shell 扩展;因此,$1
or $3
不会像使用不带引号的定界文档那样被调用 shell 替换。
如果您不想走回避路线,您可以让 shell 自己为您进行引用。例如:
external_function() {
gzip -d /tmp/file.out-20171119.gz
echo file* | awk -F'[.-]' '{print $1$3".log"}'
}
ssh root@server1 "$(declare -f external_function); external_function"
declare -f
打印函数的定义。将该函数直接放入 SSH 命令中可确保它远程运行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)