bash 将多行回显到一个文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11322807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
echo multiple lines into a file
提问by aditya
I have a httpd.conf file of shared hosting server and I have a task to migrate every virtual host to a vps. using awk I extract a block of virtual host entry for a particular site. and store that entry in a variable. but when i echo the out put of the variable to append httpd.conf of vps server it gives a error like
我有一个共享托管服务器的 httpd.conf 文件,我有一项任务是将每个虚拟主机迁移到 vps。使用 awk,我为特定站点提取了一块虚拟主机条目。并将该条目存储在变量中。但是当我回显变量的输出以附加 vps 服务器的 httpd.conf 时,它给出了一个错误
bash: command substitution: line 1: syntax error near unexpected token newline'
bash: command substitution: line 1:
echo '
bash:命令替换:第 1 行:意外标记附近的语法错误newline'
bash: command substitution: line 1:
echo '
Could anyone tell what is exact way to append simple text file by echo a variable having multiple lines.
任何人都可以通过回显具有多行的变量来告诉附加简单文本文件的确切方法是什么。
My expect script is as follows:
我的期望脚本如下:
#!/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
and the variable "remote_command" contains a pattern of virtual host entries. used printf but still same problem exists. –
并且变量“remote_command”包含虚拟主机条目的模式。使用 printf 但仍然存在同样的问题。——
my remote_command contains following values
我的 remote_command 包含以下值
<VirtualHost>
DirectoryIndex index.php
</VirtualHost>
it still giving same error.I used "" quotes but it worked for a single line , and not for multiple lines.
它仍然给出相同的错误。我使用了 "" 引号,但它适用于单行,而不适用于多行。
My main script script.sh contains lines
我的主脚本 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 .....
when I echo REMOTE_COMMANDS4 it works fine and give me output so i use expect_remote_command_httpd.exp to transfer the output to remote machine
当我回显 REMOTE_COMMANDS4 它工作正常并给我输出所以我使用 expect_remote_command_httpd.exp 将输出传输到远程机器
my expect_remote_command_httpd.exp contains
我的 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
but its not working. May be what approach i am using is totally wrong. My main concern is to extract all lines of virtual host block related to given sites in shared httpd server and trnsfer it to remote vps's httpd conf file.i will appreciate if you will suggest any other way.
但它不工作。可能是我使用的方法是完全错误的。我主要关心的是提取与共享 httpd 服务器中给定站点相关的所有虚拟主机块行,并将其传输到远程 vps 的 httpd conf 文件。如果您提出任何其他建议,我将不胜感激。
回答by Igor Chubin
It seems so that you try to write some characters that are treated by like special characters. You must escape them with ''
. If you want just add lines line1
, line2
, line3
seprated with newlines try
似乎您尝试编写一些被视为特殊字符的字符。你必须用 来逃避它们''
。如果您只想添加行line1
, line2
,line3
用换行符分隔,请尝试
echo -e 'line1\nline2\nline3\n'
or (more portable)
或(更便携)
printf 'line1\nline2\nline3\n'
If something else, could you please give additional information?
如果还有别的,你能提供更多的信息吗?
Thank you for additional information. You mst change backticks ` to double quotes
""in the
spawn ssh` command. That is a command substitution here, but must be a normal (interpolated) string.
感谢您提供更多信息。您必须更改反引号` to double quotes
"" in the
spawn ssh` 命令。这是这里的命令替换,但必须是普通(内插)字符串。
Update
更新
Ok, so you want to run $remote_command
and write its reuls to the file. Ok. Then you must do it this way:
好的,所以您要运行$remote_command
并将其 reuls 写入文件。好的。那么你必须这样做:
spawn ssh -o "StrictHostKeyChecking no" root@$remote_ip $remote_command >> /etc/httpd/conf/httpd.conf
回答by snassr
cat >> /path/to/existingFile.text<< EOF
some text line 1
some text line 2
some text line 3
EOF
switch cat >>
to cat >
to create a file instead of append
切换cat >>
到cat >
创建文件而不是追加
cat > /path/to/newFile.text<< EOF
some text line 1
some text line 2
some text line 3
EOF
回答by pizza
You can do it like this, it is easier
你可以这样做,它更容易
spawn ssh -o "StrictHostKeyChecking no" root@$remote_ip "echo ready;cat >> /etc/httpd/conf/httpd.conf"
expect "assword:"
send "redhat\n"
expect "ready"
send "line 1 of file\n"
send "line 2 of file\n"
send "line 3 of file\n"
# send control-D to end
send ""
interact
if you don't like "echo ready; cat ...", you can just call a script that reads from stdin and do your work. Or call bash directly and send in a script.
如果你不喜欢“echo ready; cat ...”,你可以调用一个从 stdin 读取的脚本并完成你的工作。或者直接调用 bash 并发送脚本。