Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 596
Mahes Avatar asked Feb 02 '11 19:02

Mahes


People also ask

How do you create a file in a shell 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.

How do I add content to a file in bash?

To make a new file in Bash, you normally use > for redirection, but to append to an existing file, you would use >> . Take a look at the examples below to see how it works. To append some text to the end of a file, you can use echo and redirect the output to be appended to a file.

How do I create a file with specific content in Linux?

You can create a file using $ cat > a.txt. If you need to have a file with specific content, type $ echo content > filename. Show activity on this post. Show activity on this post. Instead of using filenames a..z i prefer using numbers. Show activity on this post. echo CONTENT | tee PATH…

What is shell scripting in Linux?

Most of the time, we use shell scripting to interact with the files. Shell scripting offers some operators as well as some commands to check and perform different properties and functionalities associated with the file.

How do I create a simple text file with PowerShell?

Creating a Simple Text File with PowerShell. In our first example, we'll create a text file with text in a location where the running user can create and add text to the file. In our first line, we'll actually create the file with a sentence and then call our next function to add content in the form of another sentence to the same file.

How to create a file with specific content in Python?

You can create a file using $ cat > a.txt. If you need to have a file with specific content, type $ echo content > filename. Show activity on this post. Show activity on this post.


1 Answers

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 
like image 66
Dennis Williamson Avatar answered Sep 19 '22 13:09

Dennis Williamson