Reputation: 198
I have a script which is something like:
for k in {1..100..1}
do
cat << EOF > script${k}.sh
do some things
cat << EOF > somefile
text
EOF
do some things
EOF
done
So, briefly put, I want 100 scripts that looks like:
do some things
cat << EOF > somefile
text
EOF
do some things
The problem is that when I run this, Bash associates the first EOF with the third one and stops writing and it starts trying to run the next commands.
How can I fix this?
Thanks in advance!
EDIT: I corrected a typo pointed out by Aaron below. Also, I will expand a bit my original question. Following Aaron's indication, I used END for the first and last EOF. It works fine, but I still have some trouble. Being more specific, my code looks like this:
for k in {1..100..1}
do
cat << END > script${k}.sh
mkdir folder${k}
cd folder${k}
cat << EOF > somefile
text
EOF
for j in {1..80..1}
cat << EOF > program${j}
text
EOF
execute some stuff
END
done
I guess you see the trouble. Now the code works almost fine. The problem is with the variables. The ${k} that are between the ENDs, in the script${k}
are not corrected substituted by the corresponding number!
Thanks for all the help :-)
Upvotes: 0
Views: 44
Reputation: 24802
EOF
can be any token that will be found at the end of the heredoc, so you can use another one on the outer heredoc :
for k in {1..100..1}
do
cat << END > script${1}.sh
do some things
cat << EOF > somefile
text
EOF
do some things
END
done
A few additional notes :
you're not using $k
, so you're basically overwriting the same file 100 times. Did you meant script${k}.sh
rather than script${1}.sh
?
creating 100 versions of the same script seems useless, even if the script's behaviour depends on its name. Couldn't you instead pass parameters to an unique script?
Upvotes: 2