Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
How to append output at the end of a text file
2.1.
Using >> Operator
2.2.
Example
2.3.
Output
3.
Frequently Asked Questions
3.1.
How do you put a line at the last of a file in Shell?
3.2.
What is the use of the file test operator in Korn shell?
3.3.
What is cat << EOF?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Shell Scripting - How to append output to the end of text file

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Let's ensure we understand the foundational concepts before delving further into the subjects. Here is a brief introduction if you are unfamiliar with the Shell script.

Append to a file Shell Scripting

shell script is a short piece of software created to be executed by the Unix shell, a command-line interpreter. A shell script is essentially a set of instructions that the shell in an operating system based on Unix follows.

In this blog, we will discuss how to append output at the end of a text file by using cat command in Shell Scripting.

Without further ado, let's get started.

How to append output at the end of a text file

On Linux, we may need to append some data of a command output or file content when working with files in a terminal. The term "append" simply refers to adding new data to an existing file without destroying it. We'll explain today how to append text to a file on the terminal.

Using >> Operator

💁 The >> operator sends output to a file for storage. The text is added to the file once it has been created if the given file doesn't already exist.

The cat command can be used to append text to a ninjas.sh file.

Command

cat>> $file_name

Example

#!/bin/sh
echo -e “Enter the name of the file. \c”
read file_name


if [ -f $file_name ]
then
   if [ -w $file_name ]
   then
    echo "Type some text data. To quit press ctrl+d."
    cat>> $file_name
   else
    echo "The file do not have write permission.".
   fi
else
echo "$file_name not exists."
fi

Output

Enter the name of the file.
ninjas.sh
Type some text data. To quit press ctrl+d.
Hey Ninjas ! ! Happy Coding.
Output shell scripting
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

How do you put a line at the last of a file in Shell?

The ">>" redirection operator and the tee command are the two commonly used methods for adding lines to the end of a file. Although they can both be used interchangeably, tee's syntax is verbose and supports additional operations.

What is the use of the file test operator in Korn shell?

The Korn shell offers a built-in set of options for evaluating file properties, such as whether it is a directory, a plain file (not a directory), a readable file, and so on. It uses the test command to evaluate conditional expressions.

What is cat << EOF?

This operator stands for the end of the file. A compiler or interpreter will therefore be informed that the file it was reading has ended whenever it sees this operator.

Conclusion

Congratulations on finishing the blog! We have discussed how to append output at the end of a text file by using cat commands in Shell Scripting with an example and their output.

We hope this blog has helped you enhance your knowledge of how to append output at the end of a text file in shell scripting. 

Check out the following links if you want to learn more:

🔥 Introduction to Linux Shell & Scripting Shell

🔥 Shell Scripting Interview Question

🔥 Types of Unix Operating System

 

Please refer to our guided pathways on Code studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enrol in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

Please upvote 🏆 our blogs if you find them helpful and informative!

Live masterclass