Home > database >  concatenate heredocs with output of a command
concatenate heredocs with output of a command

Time:05-23

I want to write into a file with a pre-HEREDOC, a post-HEREDOC and in the middle I want the output of a command. Is that possible in a concise way?

I can do

(
    echo '#ifndef FOO_H'
    echo '#define FOO_H'
    echo
    echo

    sed foo.c -e '/=\|}/d' -e 's/ {/;/'

    echo '#endif'
) > foo.h

But I don't like it, because I t is intended and uses many echos.

I want to try to use cat and HEREDOCS:

My idea is

cat > foo.h <<EOF
#ifndef FOO_H
#define FOO_H


EOF
sed foo.c -e '/=\|}/d' -e 's/ {/;/'
<<EOF2

#endif
EOF2

But I don't know how get them pipe syntactially into cat.

I also tried using different file descriptors. Telling cat to concatenate fd=3 .. 5 and having fd=3 the HEREDOC before, fd=4 the output from sed and fd=5 the second HEREDOC, but the problem is that 5<<EOF does not go to cat.

cat >"${dir}/foo.h" /dev/fd/3 /dev/fd/4 /dev/fd/5 4<(
  sed foo.c -e '/=\|}/d' -e 's/ {/;/'
) \
  3<<EOF
#ifndef FOO_H
#define FOO_H

EOF
5<<EOF

#endif
EOF

CodePudding user response:

cat <<EOF > foo.h
#ifndef FOO_H
#define FOO_H

$(sed -e '/=\|}/d' -e 's/ {/;/' foo.c)

#endif
EOF
  • Related