Browse Resources

Placement Partners

  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
File Indirection | Print |  Tell-a-friend

In continuation to our discussion on scripting, in this article we shall discuss a couple of more shell commands and the power of file indirection. The ‘cat’ command is a short form for concatenation, which is its basic functionality. This command prints the concatenated file in the standard output.

It is best explained by an example: $ cat f1 f2

This command will print the contents of the File f1 on the standard output, immediately followed by the contents of the File f2. Note that the ‘$’ in the above commands is not something that you type; it is just a notation that you are typing commands on the shell and it signifies the shell prompt. From now onwards, we will be using it whenever we write a command.

Piping is one of the input/output indirection methods that gives real ‘power of expression’ to scripting

Anything that is an output on the standard can be very easily dumped into a file just by redirecting it using ‘>’. So, the above output of cat can be put in File f3 as follows: $ cat f1 f2 > f3 This command overwrites the existing contents in File f3. If that is not desired, and you want to append to the existing contents, what you may want to use is ‘>>’ redirection. Note that there is no limit to the number of files to be given to the cat command. It could vary from 1 to any number (limited only by system resources). So, for example, if we want to concatenate the contents of all the .txt files in the current folder into a single file all.txt (which does not exist), we would type the following: $ cat *.txt > all.txt Just the way ‘>’ and ‘>>’ redirect standard output to a file; similarly, we can obtain standard input also from a file, instead of taking it via a standard input. This makes an important component of automation. For example, cat can also read from a standard input and just print it on the standard output by simply typing: cat. And if File f contains what I want to give as input, type: $ cat < f In fact, most of the shell commands that take file arguments for input also have an option for taking the input from the standard input. And, this input redirection could be used there, straight away.

A complementary command to cat is split, which splits a given file with a number of bytes, characters, or lines per file, as per the command line options given to it. The default names for the output files are xaa and xab. These can be changed by providing a prefix to the command. For example: $ split -b=1024k our_file our_ ...will split the file our_file in chunks of 1 Megabytes with names our_aa, our_ab and so on. There is one other common option ‘-’ for most file input commands, which tells the command to take the standard input as its input file. So, the above command is equivalent to: $ split -b=1024k - our_ < our_file

For further details, refer to the man pages.

In scripting, it is not uncommon to have an output of one command as an input to another. This operation is called piping and is done using a | (pipe). Piping is one of the input/output indirection methods that gives real ‘power of expression’ to scripting. To illustrate its usage, the above command could be re-written as follows: $ cat our_file | split -b=1024k - our_

Till now, we have been talking about the shell commands, which are provided as external programs along with the shell, not the in-built ones. Coming next in our ongoing scripting series is the discussion on the shell in-built constructs and commands, which gives the basis for writing any shell script.