简体   繁体   English

Bash Heredoc中没有这样的文件或目录

[英]No such file or directory in Heredoc, Bash

I am deeply confused by Bash's Heredoc construct behaviour. 我对Bash的Heredoc构造行为深感困惑。

Here is what I am doing: 这是我在做什么:

#!/bin/bash
user="some_user"
server="some_server"
address="$user"@"$server"

printf -v user_q '%q' "$user"

function run {
    ssh "$address" /bin/bash "$@"
}

run << SSHCONNECTION1
    sudo dpkg-query -W -f='${Status}' nano 2>/dev/null | grep -c "ok installed" > /home/$user_q/check.txt

    softwareInstalled=$(cat /home/$user_q/check.txt)

SSHCONNECTION1

What I get is 我得到的是

cat: /home/some_user/check.txt: No such file or directory cat:/home/some_user/check.txt:没有这样的文件或目录

This is very bizarre, because the file exists if I was to connect using SSH and check the following path. 这非常奇怪,因为如果我要使用SSH连接并检查以下路径,则该文件存在。

What am I doing wrong? 我究竟做错了什么? File is not executable, just a text file. 文件不是可执行文件,只能是文本文件。 Thank you. 谢谢。

If you want the cat to run remotely, rather than locally during the heredoc's evaluation, escape the $ in the $(...) : 如果您希望cat在Heredoc评估期间是远程运行,而不是在本地运行,请在$(...)转义$

softwareInstalled=\$(cat /home/$user_q/check.txt)

Of course, this only has meaning if some other part of your remote script then refers to "$softwareInstalled" (or, since it's in an unquoted heredoc, "\\$softwareInstalled" ). 当然,这仅在远程脚本的其他部分引用"$softwareInstalled" (或因为在未引用的heredoc中为"\\$softwareInstalled" )时才有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM