shell Bash脚本不能使用双引号

iszxjhcz  于 2023-03-03  发布在  Shell
关注(0)|答案(2)|浏览(171)

当我运行下面的脚本时,它工作正常,但是我必须在脚本中输入PublicDNS。这个脚本有单引号。

sh '''
    ssh -o StrictHostKeyChecking=no \
        -i my-key.pem \
        ec2-user@ec2-xxx-xxx.amazonaws.com \
        "sudo yum update -y && \
        sudo yum install docker -y && \
        sudo systemctl start docker && \
        sudo docker pull image/test:be && \
        sudo docker run -d -p 8080:8080 image/test:be"
'''

但是,当我尝试在第二个脚本中添加DNS变量并将脚本更改为双引号时,将抛出如下所示的错误

sh """
    ssh -o StrictHostKeyChecking=no \
        -i my-key.pem \
        ec2-user@$DNS \
        "sudo yum update -y && \
        sudo yum install docker -y && \
        sudo systemctl start docker && \
        sudo docker pull image/test:be && \
        sudo docker run -d -p 8080:8080 image/test:be"
"""

错误:
/var/jenkins_home/workspace/www.example.com:3:sudo yum更新-y && sudo yum安装Docker-y && sudo systemctl启动Docker && sudo Docker提取图像/测试:be && sudo Docker运行-d-p 8080:8080图像/测试:be:script.sh: 3: sudo yum update -y && sudo yum install docker -y && sudo systemctl start docker && sudo docker pull image/test:be && sudo docker run -d -p 8080:8080 image/test:be: not found
你知道我做错了什么吗?谢谢

krugob8w

krugob8w1#

100%工作

只需使用heredoc即可:

sh <<EOF
    ssh -o StrictHostKeyChecking=no \
        -i my-key.pem \
        ec2-user@$DNS \
        "sudo yum update -y && \
        sudo yum install docker -y && \
        sudo systemctl start docker && \
        sudo docker pull image/test:be && \
        sudo docker run -d -p 8080:8080 image/test:be"
EOF
nle07wnf

nle07wnf2#

使用以下文档:

sh <<EOF
ssh -o StrictHostKeyChecking=no \
    -i my-key.pem \
    ec2-user@$DNS \
    "sudo yum update -y && \
    sudo yum install docker -y && \
    sudo systemctl start docker && \
    sudo docker pull image/test:be && \
    sudo docker run -d -p 8080:8080 image/test:be"
EOF

相关问题