Red_badger
Red_badger

Reputation: 139

Enter lines at beginning and end of file in bash with variables

i am creating a script that will take a csv file and make an XML file. The script will also take user entered data to go in the xml as well. I have created a script that will make my XML, but i am having problems entering my read in variable at the start of the file.

Ultimately there will be a longer line being entered, with 3 variables in the line - i am just writing the basics at the minute.

My problem is i am unable to read in the variable $BUS into ed command. I have read that it is possible, but i have been unable to make it work.

i tried my EOF with and without the single quote.

 echo "Enter BUS name"
        read BUS

echo $BUS

awk -f xmlbody $FILE  > result.xml
file=result.xml

ed -s $file <<'EOF'
0a
<?xml version="1.0" encoding="utf-8"?>
$BUS
<Data>
.
$a
</Data>
.
w
EOF

exit 0

Upvotes: 0

Views: 148

Answers (1)

Aaron
Aaron

Reputation: 24802

From bash's manual :

If any characters in [the delimiter] are quoted, [...] the lines in the here-document are not expanded

You must avoid quoting your heredoc delimiter (here EOF). The following should work :

ed -s $file <<EOF
0a
<?xml version="1.0" encoding="utf-8"?>
$BUS
<Data>
.
\$a
</Data>
.
w
EOF

Upvotes: 1

Related Questions