Creating files with some content with shell script

ShellUnixScripting

Shell Problem Overview


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

Shell Solutions


Solution 1 - Shell

You can use a here document:

cat <<EOF >/home/a.config
first line
second line
third line
EOF

You can place several of these in the same script.

On OS-X the command should be:

cat > filename <<- "EOF"
file contents
more contents
EOF

Solution 2 - Shell

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

Solution 3 - Shell

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.

Solution 4 - Shell

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

Solution 5 - Shell

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMahesView Question on Stackoverflow
Solution 1 - ShellDennis WilliamsonView Answer on Stackoverflow
Solution 2 - ShellAllwinView Answer on Stackoverflow
Solution 3 - ShellVictor SorokinView Answer on Stackoverflow
Solution 4 - ShellDeepak RaoView Answer on Stackoverflow
Solution 5 - Shellscotjam1981View Answer on Stackoverflow