Kay
Kay

Reputation: 2077

How can I read a file in cat << EOF >

I would like to print a file in cat << EOF>. e.g.

$cat file
ad3
c43
34e
se3
we3

My script is:

$cat run.sh
cat << EOF > test.sh
#!/bin/bash
some commands
cat file #I would like to print the content of the file here
some commands
EOF

I can't able to print as desire with ./run.sh

Desire output

$cat test.sh
#!/bin/bash
some commands
ad3
c43
34e
se3
we3
some commands

Upvotes: 1

Views: 2119

Answers (3)

Mark Setchell
Mark Setchell

Reputation: 207465

You could use a compound command:

{ cat << EOF ; cat file ; cat << EOF2; } > test.sh
> start
> EOF
> more
> EOF2

That gives:

start
ad3
c43
34e
se3
we3
more

Upvotes: 3

wildplasser
wildplasser

Reputation: 44250

You could use backticks, or write the file in chunks:


#!/bin/bash

cat <<OMG >zfile
ad3
c43
34e
se3
we3
OMG

# Method1 : backticks

cat << EOF > test.sh
#!/bin/bash
some commands1
`cat zfile`
some commands2
EOF

# Method2: append

cat << EOF1 > test2.sh
#!/bin/bash
some commands1
EOF1

cat zfile >> test2.sh

cat << EOF2 >> test2.sh
some commands2
EOF2

Upvotes: 2

Exciter
Exciter

Reputation: 94

I think you are searching for something like that?

cat > test.sh <<EOF
#!/bin/bash
some commands
ad3
c43
34e
se3
we3
some commands
EOF

Upvotes: 1

Related Questions