Yes, I think the title is perfect except for the puns. Yes. Standard I / O is very important for shell scripts, but I won't touch on shell script notation here (title scam).
You may be familiar with the word standard input / output. In Linux, when any command is executed, three channels for input / output are automatically opened. That is the three below.
The function to change the standard input / output destination is called redirect **. I think that redirecting the standard output destination to a file is often done.
For example, if you execute the cat
command with no arguments, the command line will be waiting for input from the keyboard. So, if you input redirect an appropriate file to the cat
command with no arguments, it will be as follows. By the way, use the symbol ** <** for input redirection.
qiita:~/qiita$ vim ore.txt
qiita:~/qiita$ cat < ./ore.txt
I'm a genius. No one said that, and the results didn't prove it, but I know. Because I'm me(Unknown)
qiita:~/qiita$
It is like this. In the case of the cat
command, the same result can be obtained by specifying a file as an argument.
This time too, I will show an example of output redirection using the simple and easy cat
command.
qiita:~/qiita$ cat ./ore.txt > watashi.txt
qiita:~/qiita$ cat ./watashi.txt
I'm a genius. No one said that, and the results didn't prove it, but I know. Because I'm me(Unknown)
qiita@pumpkin:~/qiita$
With ʻore.txtas the argument of
cat,
watashi.txt is specified in the output redirect. When I check it, the contents of ʻore.txt
are output to watashi.txt
.
By the way, the output redirect destination file will be overwritten if it exists, and will be automatically generated even if it does not exist. You can change it to append at the end by using two redirect symbols like >>
←.
At the very end, the standard error output is a different channel from the standard output, so even if you change the standard output destination to a file, the error output will be displayed on the display.
qiita@pumpkin:~/qiita$ cat < nainai.txt > watashi.txt
bash: nainai.txt:There is no such file or directory
qiita@pumpkin:~/qiita$
Here, we use the symbol 2>
for standard error output.
qiita@pumpkin:~/qiita$ cat ./nainai.txt 2> error.txt
qiita@pumpkin:~/qiita$ cat ./error.txt
cat: ./nainai.txt:There is no such file or directory
qiita@pumpkin:~/qiita$
It is like this. Certainly the error content is redirected to error.txt.
There is still more content on standard I / O, but I look forward to updating this article. I'm sure I'll do it.
Recommended Posts