First page Back Continue Last page Overview Image
Pipes and redirection
- Every process need input and output channels. A child process uses the same input and output as its parent, but a user can change this. There are two output channels stdout and stderr. In the terminal both appear mixed.
- use “> fn” to redirect the stdout to the file named fn, if it does no exist it is created.
- example: %echo “1 + 2 - 3” > myfile #note the spaces
- use “<” to read input from a file, the file should exist.
- example: %expr < myfile # result is 0
- use “> fn 2>&1” to send both stdout and stderr to fn.
- example: %ls nofile > fn 2>&1 #check fn’s content with cat fn
- use “>> fn “ to append the output at the end of the file
- example: %ls fn >> fn #check fn’s content with cat fn
- use “| command” to connect the stdout of a file to the input of a new program
- example: %cat fn | wc -c # result 41; “wc -c” counts characters in its input.
Notes:
Redirección.
Los procesos reciben su entrada de la entrada estándar y escriben en la salida estádar, o stdout. Los mensajes de error se envían al error estándar o stderr.
Uno puede redirigir estos flujos a un archivo o tomarlos de un archivo.
La salida se redirige con “mayor qué” y el nombre del archivo. Para redirigir la entrada se usa “menor qué” el el nombre de archivo.
Para añadir el error estándar a un archivo de salida se añade el comando 2>&1 a la línea después de la redirección.
OJO: estos símbolos constituyen una unidad, no los separe ni los ponga incompletos.
Para encadenar dos comandos, a menudo hay que enviar la salida de uno a la entrada de otro, eso se hace con la barra vertical “|”.
Analice aquí los ejemplos.