Mahes
Mahes

Reputation: 4125

Creating files with some content with shell script

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

Upvotes: 80

Views: 170358

Answers (6)

Hamid Reza
Hamid Reza

Reputation: 173

I would use tee command like this:

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

Upvotes: 0

Dennis Williamson
Dennis Williamson

Reputation: 360035

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.

Upvotes: 147

Allwin
Allwin

Reputation: 2216

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

Upvotes: 35

Deepak Rao
Deepak Rao

Reputation: 710

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

Upvotes: 7

scotjam1981
scotjam1981

Reputation: 11

>\#!/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

Upvotes: 1

Victor Sorokin
Victor Sorokin

Reputation: 11996

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.

Upvotes: 11

Related Questions