val=Mumbai
cat << 'EOF' > pkll1.htm
<html>
<head>
<title>HTML</title>
</head>
<body>
<h1>$val</h1>
while read line
do
val1=`echo $line`
<p>$val1</p>
done<myfile.dat
</body>
</html>
EOF
Above code showing output as below instead of actual value:
$val
while read line do val1=`echo $line`
$val1
done
How to execute it in correct way?Help me please.
asked Sep 8, 2012 at 10:39
par181
4011 gold badge12 silver badges30 bronze badges
1 Answer 1
Do it in separate blocks, with the code in the middle.
val=Mumbai
file=pkll1.htm
cat <<EOF1 > $file
<html>
<head>
<title>HTML</title>
</head>
<body>
<h1>$val</h1>
EOF1
while read line; do
val1=`echo $line`
echo "<p>$val1</p>" >> $file
done < myfile.dat
cat <<EOF2 >> $file
</body>
</html>
EOF2
answered Sep 8, 2012 at 10:45
ghoti
47.2k8 gold badges71 silver badges109 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
reece
Note that the
cat << EOF syntax treats any content up to the EOF line as input to the cat command (this will work for other commands as well). Therefore, no commands or unescaping is being performed on the text. This is like the """...""" syntax in python -- python does not execute any code within that block. This explains why @pkawar's shell script does not work as intended.ghoti
Yes, and it's why I used separate chunks terminated by EOF1 and EOF2 instead.