bash - 嵌套的 Herodoc 操作顺序

标签 bash shell

我有以下代码-

ssh host sh -s << EOF
cd /backup/

ls

psql -U user -d db << SQL
$(sed 's/${previous_quarter}/${current_quarter}/' table_quarters.sql);
$(sed 's/${previous_quarter}/${current_quarter}/' plans.sql);
SQL

EOF

这是我执行它时遵循的执行顺序 -

  1. table_quarters 脚本
  2. 计划脚本
  3. ls 命令。

为什么不遵循这个执行顺序 -

  1. ls 命令
  2. table_quarters 脚本
  3. 计划脚本

最佳答案

您正在将字符串发送到 ssh 的标准输入。仅当首先运行 $(...) 中的内容时才能构造此字符串,因为这些部分将被所包含命令的输出替换。

字符串展开后,它会通过 ssh 发送到远程计算机,远程计算机按顺序运行结果命令。

如果您想在远程端运行扩展,则需要正确转义美元符号。

关于bash - 嵌套的 Herodoc 操作顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67199110/

相关文章:

linux - read -p 与2>/dev/null 组合时不提示字符串,这是为什么呢?

bash shell 脚本 for 循环中的两个变量

bash - 在bash中为$@添加前缀和后缀

bash - bash 脚本中奇怪的并行执行

regex - 合并两个 sed 命令

linux - grep 命令反转选项不起作用

java - 从 shell 读取 jar 的 MANIFEST.MF

linux - 自定义 vi 命令 : How to insert current date with a variable's content?

bash - 在 bash 中添加两个变量

c++ - 满足退出条件时程序不退出?