You can redirect the standard output from a program to a file using the shell operator > in Bash (other shells may have slightly different syntax). Here's an example:
$ program1 > file1
This results in program1 being executed and its standard output stream being written to file1, replacing any existing data in file1. If you'd wanted to append data to the end of the file instead you could use the shell operator >> in Bash:
$ program1 >> file1
The shell operator < can be used to read data from a file and send it to the standard input stream of a program:
$ program1 < file1
Again, program1 is executed but this time file1 is the source of the data for standard input instead of the keybaord.
You can actually combine shell operators to achieve more sophisticated results. In the example below program1 is executed and data from file1 is sent to its standard input. The standard output from running program1 with the input from file1 is then written to file2:
$ program1 < file1 > file2
There may be times when you want the output from one program to be read as the input to another program. You can achieve this using temporary files like this:
$ program1 > tempfile1
$ program2 < tempfile1
$ rm tempfile1
This is a bit cumbersome though so shells provide a facility called piping.
$ program1 > file1
This results in program1 being executed and its standard output stream being written to file1, replacing any existing data in file1. If you'd wanted to append data to the end of the file instead you could use the shell operator >> in Bash:
$ program1 >> file1
The shell operator < can be used to read data from a file and send it to the standard input stream of a program:
$ program1 < file1
Again, program1 is executed but this time file1 is the source of the data for standard input instead of the keybaord.
You can actually combine shell operators to achieve more sophisticated results. In the example below program1 is executed and data from file1 is sent to its standard input. The standard output from running program1 with the input from file1 is then written to file2:
$ program1 < file1 > file2
There may be times when you want the output from one program to be read as the input to another program. You can achieve this using temporary files like this:
$ program1 > tempfile1
$ program2 < tempfile1
$ rm tempfile1
This is a bit cumbersome though so shells provide a facility called piping.
Comments
Post a Comment
https://gengwg.blogspot.com/