learn linux and unix commands - unix shell scripting
4 stars based on
The name is derived from its function to con cat enate files. Cat was part of the early versions of Unixe. The Single Unix Specification defines the operation of cat to read files in the sequence given in its arguments, writing their contents to the standard output in the same sequence.
The specification mandates the support of one option flag, u for unbuffered output, meaning that each byte is written after it has been read. Some operating systems, like the ones using GNU Core Utilitiesdo this by default and ignore the flag.
If one of the input filenames is specified as a single hyphen -then cat reads from standard input at that point in the sequence. If no files are specified, cat reads from standard input only. As such, its two main use cases are text files and certain format-compatible types of binary files.
For many structured binary data sets, the resulting combined file may not be valid; for example, if a file has a unique header or footer, the result will spuriously duplicate these. However, for some multimedia digital container formatsthe resulting file is valid, and so cat provides an effective means of appending files. Handle binary data in shell script streams can be a significant example of files that cat can concatenate without issue, e.
The Jargon File version 4. Among Unix fans, cat 1 is considered an excellent example of handle binary data in shell script design, because it delivers the file contents without such verbosity as spacing or headers between the files, and because it does not require the files to handle binary data in shell script of lines of text, but works with any sort of data. Among Unix critics, cat 1 is considered the canonical example of bad user-interface design, because of its woefully unobvious name.
It is far more often used to blast a single file to standard output than to concatenate two or more files. The name cat for the former operation is handle binary data in shell script as unintuitive as, say, LISP 's cdr. UUOC  from comp. If it is only one file, concatenating it with nothing at all is a waste of time, and costs you a process. The activity of fixing instances of UUOC is sometimes called demoggification.
This can be rewritten using redirection of stdin instead, in either of the following forms the first is more traditional:. Beyond other benefits, the input redirection forms allow command to perform random access on the file, whereas the cat examples do not. This is because the redirection form opens the file as the stdin file descriptor which command can fully access, while the cat form simply provides the data as a stream of bytes.
Another common case where cat is handle binary data in shell script is where a command defaults to operating on stdin, but will read from a file, if the filename is given as handle binary data in shell script argument. This is the case for many common commands; the following examples:. A common interactive use of cat for a single file is to output the content of a file to standard output.
Creates a file called newfile. The text will be in file newfile.