Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create file with contents from shell script

Tags:

bash

shell

How do I, in a shell script, create a file called foo.conf and make it contain:

NameVirtualHost 127.0.0.1  # Default <VirtualHost 127.0.0.1> ServerName localhost DocumentRoot "C:/wamp/www" </VirtualHost> 
like image 841
UKB Avatar asked Nov 29 '12 19:11

UKB


People also ask

How do I create a contents file in a bash script?

To create a new file, run the "cat" command and then use the redirection operator ">" followed by the name of the file. Now you will be prompted to insert data into this newly created file. Type a line and then press "Ctrl+D" to save the file.

How do you create a file with content in Unix?

To create a new file run the cat command followed by the redirection operator > and the name of the file you want to create. Press Enter type the text and once you are done press the CRTL+D to save the files.


2 Answers

You can do that with echo:

echo 'NameVirtualHost 127.0.0.1  # Default <VirtualHost 127.0.0.1> ServerName localhost DocumentRoot "C:/wamp/www" </VirtualHost>' > foo.conf 

Everything enclosed by single quotes are interpreted as literals, so you just write that block into a file called foo.conf. If it doesn't exist, it will be created. If it does exist, it will be overwritten.

like image 41
sampson-chen Avatar answered Oct 13 '22 08:10

sampson-chen


Use a "here document":

cat > foo.conf << EOF NameVirtualHost 127.0.0.1  # Default <VirtualHost 127.0.0.1> ServerName localhost DocumentRoot "C:/wamp/www" </VirtualHost> EOF 
like image 68
ams Avatar answered Oct 13 '22 08:10

ams