![]() Then, we will use the cat < $var-EOF block to enclose the sample content. In that bash script, we created the variable named “var” and equalized it to a file path, i.e., the name and path of the file to which we want the contents to be copied. In this step, we will simply open the bash file that we created for demonstrating our first scenario. Step 1: Modify Bash Script Created in Case # 1 Follow the steps provided below to print the contents of any file to another file using the bash script. After executing this bash script, you can navigate to the specified path and check the contents of the file. Otherwise, the script will first create a file at the specified path, then copy the contents to the newly created file. If a file with the specified name already exists, then our bash script will simply copy our sample text to that file. When this bash script executes, it will save the text enclosed within our bash script in the specified file. This bash script will also contain some sample text. For this purpose, we will modify the bash script that we created in the previous scenario, Case # 1. #Script guide editra how to#In this scenario, we will show you how to print the contents of one file to another file. When our bash script is ready, we will execute it with the following command:Īfter executing the command in Step 2, you will see the contents enclosed within the cat << EOF-EOF block in your bash script in the terminal, as shown in the following image:Ĭase # 2: Printing File Contents to Another File We have written some random text within this block to be printed in the terminal. This script uses the cat << EOF-EOF block to print the contents enclosed within this block in the terminal. Then, we will type the script shown in the following image in this file. This saves us the hassle of providing the file path every time we want to access it.Īfter creating this file, we will open the file with a text editor in Linux Mint 20. Also, you can create this bash file in any directory of your choice however, it is always convenient to create files in the Home Directory for demonstration purposes. You may choose any other name you would like for this bash file. Step 1: Create Bash Scriptįirst, we will create a file named EOF.sh in our Home directory. Follow the steps provided below to print the contents of any given file in your terminal. When this bash script executes, it will display the text enclosed within our bash script in the terminal. For this purpose, we will first create a bash script that will contain some sample text. In this scenario, we will show you how to use the EOF operator to print the contents of a file in the terminal. Case # 1: Printing File Contents in the Terminal We will be going through these scenarios one by one. To explain the usage of the Cat EOF operator in bash script, we have designed two simple example scenarios using this operator in Linux Mint 20. Two Cases of using Cat EOF in Bash Script in Linux Mint 20 This article shows you the usage of the Cat EOF operator in a bash script in Linux Mint 20 with examples. However, instead of performing this step to view the contents of a file, we can simply incorporate this step into our bash script to serve the same purpose. The “cat” command, followed by the file name, allows you to view the contents of any file in the Linux terminal. It is generally used either to print the text of a file in the terminal or to copy the contents of a file to another specified location. When this operator is paired with the “cat” command in bash, it can be used to serve various other purposes. Similarly, in bash, the EOF operator is used to specify the end of the file. This means that wherever a compiler or an interpreter encounters this operator, it will receive an indication that the file it was reading has ended. This operator stands for the end of the file. The EOF operator is used in many programming languages. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |