The Building Block Primitives of UNIX

List the building block primitives of UNIX with examples.

Unix enables to Write Small modular Program that can be Used as building block to build more complex programs. There 2 Primitives:

1. I/O redirection
2. Pipe(|)

I/O REDIRECTION 

Most unix command read input, such as a file or another attribute for the command, and write output. By default, input is being given with the keyboard, and output is displayed on your screen. Your keyboard is your standard input (stdin) device, and the screen or a particular terminal window is the standard output (stdout) device.

Output redirection with > and | : 

Sometimes you will want to put output of a command in a file, or you may want to issue another command on the output of one command. This is known as redirecting output. Redirection is done using either the “>” (greater-than symbol), or using the “|” (pipe) operator which sends the standard output of one command to another command as standard input. The file descriptor 0 is assigned to the standard output file.

Eg: cat1 test1 > test2

the cat command concatenates files and puts them all together to the standard output. By redirecting this output to a file, this file name will be created - or overwritten if it already exists, so take care.

Input redirection 

In another case, you may want a file to be the input for a command that normally wouldn't accept a file as an option. This redirecting of input is done using the “<” (less-than symbol) operator. The file descriptor 1 is assigned to the standard input file.

Eg: cat <test1

In this the cat will take each line of file test1 as input and display it on the monitor.

Error Redirection 

When invalid command is typed at the $prompt the shell displays error message on the monitor.Thus the monitor is reffered as standard error file. The file descriptor 2 is assigned to the standard error file.

PIPES 

Unix has a feature by which filters and other commands can be combined in such a way that the standard output of 1 filter command can be sent as standard input to another filter or command.

$ ls –l|wc –l

It is the command to display the no of total lines in current directory.

$ grep –n `and’ test

Command to display the no of lines where the word ‘and’ occurs in a file test.

See Also

Have a Unix Problem
Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.