what is output redirection?
I am reading a UNIX book and in one of the chapters it mentioned using echo and output redirection, could you explain what output redirection is?
It was similar to this: echo "sample text" >> file This is probably very simple but I'm not familiar with it. |
That could be, for instance, when you run, say the 'ls' command.
Say you have a directory with a lot of .conf files. You can run 'ls -l *.conf' to see them. Then, if you wanted to, you could redirect the output of 'ls' to another text file, like 'ls -l *.conf > conf_list.txt', such that the conf_list.txt file is created from the output of ls. Example: Code:
bs@bsubuntu:/etc$ ls -l *.conf |
Or it could be like this:
You can run ps -A and get output on the screen. Or you could run ps -A and redirect the ouput to grep: Code:
bs@bsubuntu:/etc$ ps -A |
Be carefull with redirecting output to printers though. A fellow student of mine mindlessly executed something like "cat book.pdf > printer".
By the time he'd realized his stupid mistake and stopped the printer, he'd allready wasted a great deal of his printing quota for that month. |
I am taking your question to literally mean that you are asking for an understanding about what redirection is and does. If I am incorrect and you already know this information, then please accept my apology.
In UNIX, Linux, etc the shell (does not matter which one) automatically opens three data streams for you. The first stream, stdin (standard in) accepts data from the keyboard and passes it to a program. The second stream stdout (standard out) takes text data from a program and displays it in a screen window. The third stream stderr (standard error) also takes text data from a program and also displays it in a screen window. These are the default behaviors for any program running from a shell. The operating system allows you to "redirect" any one, or all, of the data streams to either obtain data from something other than the keyboard, or to output text to something other than the screen window. This is accomplished by giving the shell special instructions when you execute a command. "<" causes the program's stdin to be redirected to the new source instead of the keyboard. ">" causes the program's stdout to be redirected to the new target instead of the screen window. "2>" causes the program's stderr to be redirected like the stdout redirection. ">>" causes the program's stdout to also be redirected to a new target, but causes the new information to be a continuation of whatever was originally in the target output. Your example "echo "sample text" >> file" simply means using the program "echo" (a utility to simply echo the contents of the first argument passed to it out to stdout) the text "sample text" is sent to a file called "file", and the output text is concatenated onto the end of the file if it preexisted. If it did not preexist, then a new file called "file" will be created with the contents of a single text line of "sample text". |
note the difference btw
> and >> |
All times are GMT -5. The time now is 07:04 PM. |