作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个共享主机服务器的 httpd.conf 文件,我的任务是将每个虚拟主机迁移到 vps。使用 awk 我为特定站点提取了一个虚拟主机条目 block 。并将该条目存储在变量中。但是当我回显变量的输出以附加 vps 服务器的 httpd.conf 时,它会给出类似
的错误bash:命令替换:第 1 行:意外标记附近的语法错误 newline'
echo '
bash:命令替换:第 1 行:
谁能告诉我通过回显具有多行的变量来附加简单文本文件的确切方法。
我的期望脚本如下:
#!/usr/bin/expect
set remote_ip [lindex $argv 0]
set username [lindex $argv 1]
set remote_command [lindex $argv 2]
foreach {remote_ip username remote_command} $argv {break}
spawn ssh -o "StrictHostKeyChecking no" root@$remote_ip `echo $remote_command >> /etc/httpd/conf/httpd.conf`
expect "*assword: "
send "redhat\r"
interact
变量“remote_command”包含虚拟主机条目的模式。使用 printf 但仍然存在同样的问题。 –
我的 remote_command 包含以下值
<VirtualHost>
DirectoryIndex index.php
</VirtualHost>
它仍然给出相同的错误。我使用了“”引号,但它适用于单行,而不适用于多行。
我的主脚本script.sh包含行
#!/bin/bash
some code .....
some code......
some code ......
echo "<VirtualHost $D_IPADDRESS>" > /opt/remotehttpd_conf
awk "p && /\/VirtualHost/{exit} /$SITENAME/{p=1}p" /opt/httpd.conf >> /opt/remotehttpd_conf
echo "</VirtualHost>" >> /opt/remotehttpd_conf
REMOTE_COMMANDS4=`cat /opt/remotehttpd_conf`
echo $REMOTE_COMMANDS4
./expect_remote_command_httpd.exp "$D_IPADDRESS" "$USERNAME" "$REMOTE_COMMANDS4"
some code .....
some code .....
当我回显 REMOTE_COMMANDS4 时它工作正常并给我输出所以我使用 expect_remote_command_httpd.exp 将输出传输到远程机器
我的 expect_remote_command_httpd.exp 包含
#!/usr/bin/expect
set remote_ip [lindex $argv 0]
set username [lindex $argv 1]
set remote_command [lindex $argv 2]
foreach {remote_ip username remote_command} $argv {break}
spawn ssh -o "StrictHostKeyChecking no" root@$remote_ip `echo $remote_command >> /etc/httpd/conf/httpd.conf`
expect "*assword: "
send "redhat\r"
interact
但它不起作用。可能是我使用的方法是完全错误的。我主要关心的是提取与共享 httpd 服务器中给定站点相关的所有虚拟主机 block 行,并将其传输到远程 vps 的 httpd conf 文件。如果您提出任何其他建议,我将不胜感激。
最佳答案
cat >> /path/to/existingFile.text<< EOF
some text line 1
some text line 2
some text line 3
EOF
将 cat >>
切换为 cat >
以创建文件而不是追加
cat > /path/to/newFile.text<< EOF
some text line 1
some text line 2
some text line 3
EOF
关于bash - 将多行回显到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11322807/
我是一名优秀的程序员,十分优秀!