[LINUX] Let's understand the standard input / output of bash together and write a shell script.

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).

What is standard input / output?

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.

Let's redirect

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.

Standard input redirect

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.

Standard output redirection

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 ofcat, 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 >> ←.

Error standard output

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.

Afterword

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

Let's understand the standard input / output of bash together and write a shell script.
Understand the number of input / output parameters of a convolutional neural network
Let's try a shell script
Process the contents of the file in order with a shell script
Write a script in Shell and Python to notify you in Slack when the process is finished
Conditional branch due to the existence of a shell script file
Write standard output to a file
Read the standard output of a subprocess line by line in Python
Notes on standard input / output of Go
Creating a shell script to write a diary
Let's run a Bash script in Java
Let's play with Python Receive and save / display the text of the input form
How to count the number of elements in Django and output to a template
Python script to get a list of input examples for the AtCoder contest
Let's write a Python program and run it
Output in the form of a python array
A discussion of the strengths and weaknesses of Python
[Python] Change the text color and background color of a specific keyword in print output
A memorandum about the warning of the pylint output result
A memo to visually understand the axis of pandas.Panel
Receives and outputs standard output of Python 2 and Python 3> C implementations
I checked the default OS and shell of docker-machine
[Ev3dev] Let's understand the mechanism of LCD (screen) control
Output the output result of sklearn.metrics.classification_report as a CSV file
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
[Note] A shell script that checks the CPU usage of a specific process in a while loop.
Give the history command a date and time and collect the history files of all users with a script
What seems to be a template of the standard input part of the competition pro in python3
A story about porting the code of "Try and understand how Linux works" to Rust