Shell heredocs

Not sure how many people are familiar with 'heredocs' but I find them very useful although I often hit snags with certain characters being altered when they have been output to a file...

Here is an example of a script which doesn't work properly:-
Code:
cat  <<EOF >do-this
script build_`date +"%y%m%d%H%M"`.log sh ./build
export DESTDIR=/mnt/xyz
echo '/dev/da0p2 / ufs rw 1 1'>> $DESTDIR/etc/fstab
EOF

Here is do-this :-
Code:
script build_1905212053.log sh ./build
export DESTDIR=/mnt/xyz
echo '/dev/da0p2 / ufs rw 1 1'>> /etc/fstab

This is what I was hoping for:-
Code:
script build_`date +"%y%m%d%H%M"`.log sh ./build
export DESTDIR=/mnt/xyz
echo '/dev/da0p2 / ufs rw 1 1'>> $DESTDIR/etc/fstab

I've learned that I need to use \$ to get $ but how do I specify something like `date +"%y%m%d%H%M"` ?
 
Use the other notation, instead of using the back-ticks:
Code:
script build_$(date .... ).log
Then you can escape the '$':
Code:
cat  <<EOF >do-this
script build_\$(date +"%y%m%d%H%M").log sh ./build
export DESTDIR=/mnt/xyz
echo '/dev/da0p2 / ufs rw 1 1'>> $DESTDIR/etc/fstab
EOF
This way the $(...) gets taken literally instead of being interpreted.

Edit: Oops, mixed up $(...) and ${...}. I've been staring at computer screens for 15 hours now, I should probably go watch some TV and go to bed.
 
Not sure how many people are familiar with 'heredocs' but I find them very useful although I often hit snags with certain characters being altered when they have been output to a file...

Here is an example of a script which doesn't work properly:-
...
This is what I was hoping for:-
...
I've learned that I need to use \$ to get $ but how do I specify something like `date +"%y%m%d%H%M"` ?

Better idea: man sh: ;)
If the delimiter as specified on the initial line is quoted, then the here-doc-text is treated literally, otherwise the text is subjected to parameter expansion, command substitution, and arithmetic expansion

Code:
pmc@disp:533:1~$ cat <<EOF
> `date`
> EOF
Wed May 22 04:55:30 CEST 2019
pmc@disp:534:1~$ cat <<'EOF'
`date`
EOF
`date`
pmc@disp:535:1~$
 
Back
Top