I have an encrypt() function that goes like this:
encrypt -a aes -k key **-i input.file** -o output.file
which take in 1 input file and output 1 file as well.
By using pipe:
echo "abc" | encrypt -a aes -k key -o output.file
Q1) How does the encrypt() function know or the OS know that the output for the pipe | is suppose to be the input for the encrypt function (so that i didn't need to specify the "-i input.file" parameter) ?
Q2 Why does redirection works in this case ? echo "abc" > encrypt -a aes -k key -o output.file
When you use a pipe, conventional files are not involved. When you invoke
the
encryptprogram does not open a file at all, instead it reads its standard input. The standard input is whatever you set up on the command line when you invoked it. Standard input can be a pipe, as in your example. If you use input redirection, standard input can be a file:Finally, if you don't use any pipes or input redirection at all, that is, if you invoke
then the
encryptprogram will read from the keyboard.Your second question Q2 is meaningless, though. You will end up creating an output file named "encrypt", and you won't run the
encryptprogram at all. If you want to use output redirection to control where theencryptprogram's output goes, you could useor