81

I need to configure a server with a few files and I want to do it programmatically.

I need to create files say /home/a.config, /var/spool/b.config, /etc/c.config

Files above have some contents (multi lines).

I want to create ONE shell script which can create all three file with multiple lines (around 10). I would like to know the how can I use CAT command to do that. (inside shell script).

I am looking something like this

echo " going to create  /home/a.config"

cat "HOW CAN I HAVE MULTIPLE LINES HERE?? " >  /home/a.config 

thanks

6 Answers 6

148

You can use a here document:

cat <<EOF >filename
first line
second line
third line
EOF

You can place several of these in the same script.

Sign up to request clarification or add additional context in comments.

6 Comments

Note that $ has to be escaped if it's to be included in the actual text.
@Nagev: If you don't want any parameter expansion, command substitution, and arithmetic expansion in the here document you can quote the beginning delimiter 'EOF' instead of having to escape each individual dollar sign.
And if I want to use a parameter value in my text?
How do I access an external variable?
@JúlioFalbo: If you don't quote the here doc delimiter (e.g. EOF) then parameters (variables) in the text will be expanded.
|
35
file="/tmp/test.txt"
echo "Adding first line" > $file
echo "Adding first line replaced" > $file
echo "Appending second line " >> $file
echo "Appending third line" >> $file
cat $file

> to add/replace the content ( here actual content got replaced by the 2nd line)
>> to append

Result
Adding first line replaced
Appending second line
Appending third line

2 Comments

I wonder why no one gave you an up vote. Your answer is pretty good too. Thanks works for me.
I thought that your answer is pretty much spot on...
11

Like so:

#!/bin/bash

var="your text"
echo "simply put,
just so: $var" > a.config

For further info, see Input/Output part of abs.
Hope, this helps.

Comments

7

If you've got variables like $1 or $HOMEDIR in your text then these normally get evaluated and substituted with actual values. If you want to prevent these from getting substituted then you need to quote the opening limit string (EOF in example below) with single quote 'EOF', double quote "EOF" or precede it with backslash \EOF

Closing limit string stays as is. EOF

This is especially useful if you are writing shell scripts to a file.

cat << 'EOF' >/etc/rc.d/init.d/startup
case $1 in
    start)
        start 
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
       pid=$(tomcat_pid)
        if [ -n "$pid" ]
        then
           echo "Tomcat is running with pid: $pid"
        else
           echo "Tomcat is not running"
        fi
        ;;
esac

EOF

Refer Example 19.7 Parameter Substitution Turned off in Here Documents

1 Comment

Perfect ! this is exactly what I was looking for
1
>\#!/bin/bash
>
>var="your text" <br>
>echo "simply put, <br>
>just so: $var" > a.config

Note that you also need to escape out certain characters to avoid them interfering with what you're trying to do, for example $ ` and " will all break such a statement unless preceded with a backslash, i.e. \` \$ or \"

so if we define the following:

var="100"

the following would not behave as expected:

echo "simply put,
just "lend" me US$ $var" > a.config

but the following would work correctly:

echo "simply put,
just \"lend\" me US\$ $var" > a.config

Comments

0

I would use tee command like this:

echo "My 
long
multiline
text
here!
" | sudo tee -a /etc/config.conf

Comments

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.