1
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.

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Yes, and it's why I used separate chunks terminated by EOF1 and EOF2 instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.