Echo multiple lines to file

I have a httpd.conf file for a shared hosting server, and I have the task of moving each virtual host to vps. using awk, I retrieve the virtual host write block for a specific site. and save this entry in a variable. but when I return out to add httpd.conf for the vps server it gives an error like

bash: command substitution: line 1: syntax error near unexpected token newline' bash: command substitution: line 1: echo'

Can anyone say what is the exact way to add a simple text file by echoing a variable with multiple lines.

My expectation script looks like this:

 #!/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 the host virtual entry template. used printf, but still the same problem exists. -


my remote_command contains the following values

 <VirtualHost> DirectoryIndex index.php </VirtualHost> 

he still gives the same error. I used quotation marks, but it worked for a single line, not multiple lines.

My main script script.sh contains lines

 #!/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 gives me output, so I use the expect_remote_command_httpd.exp function to send the output to the remote machine.

my expect_remote_command_httpd.exp contains

 #!/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 does not work. Maybe what approach I use is completely wrong. My main task is to extract all the lines of the virtual host block associated with these sites on the shared httpd server and pass them to the remote vps httpd conf file.i, if you suggest any other way.

+4
source share
2 answers

It seems that you are trying to write some characters that are treated as special characters. You should avoid them with help. '' If you just want to add lines line1 , line2 , line3 , separated by newline characters, try

 echo -e 'line1\nline2\nline3\n' 

or (more portable)

 printf 'line1\nline2\nline3\n' 

If something else, could you provide more information?

Thank you for additional information. You change backticks ` to double quotes " " in the spawn ssh`. This is command substitution here, but should be a normal (interpolated) string.

Update

So you want to run $remote_command and write its reuls to a file. OK. Then you should do it as follows:

 spawn ssh -o "StrictHostKeyChecking no" root@ $remote_ip $remote_command >> /etc/httpd/conf/httpd.conf 
+13
source

You can do it like this, 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 "\04" interact 

if you don't like "echo ready, cat ...", you can just call a script that reads from stdin and does your work. Or call bash directly and submit the script.

0
source

All Articles