Làm Thế Nào Để Bạn Đọc Từ Stdin Là Gì ? Stdin,Stdout,Stderr
Today, on the occasion of someone asking about how to run a command “silently” ie without printing anything to the screen, I wrote a small tutorial about this. The example given here is the command curl, details about the command syntax, use “man curl” on Linux or on Google. First, we will go straight to the point and then explain. In order for a command to run in such a “silent” mode, we add the following to the end of the command:> /dev/null 2>&1 Specifically, if we want to do it with the curl command, then as follows: $ curl http: //osg.vnu.edu.vn/ > /dev/null 2>&1Why do that? Sign “>” what do they mean? “/dev/null“What’s so terrible? “2>&1“What is it that looks so strange? Those are also my questions when entering the Linux/Unix world.
Watching: What is Stdin
1. IntroductionOn most operating systems in general and Linux/Unix in particular, there are 3 standard I/O lines: STDIN, STDOUT and STDERR whose functions are standard input, standard output, and standard error, respectively. These are called open files and the system assigns each of these files a number called a file descriptor. The three numbers corresponding to the three standard input and output lines above are 0, 1 and 2. Specifically:
standard input -> stdin -> 0standard output -> stdout -> 1>standard error -> stderr -> 2>In C++, these 3 standard input and output lines correspond to 3 objects cin, cout and cerr.Note: In this tutorial, I use Bourne shell where the $ sign represents the normal user and the # represents the root user. However, most of the content in this article can be applied to some other shells such as sh, csh, tcsh… With C chell (csh, tcsh), numbers (file descriptor) cannot be used.2. Export/ImportIn the command line mode of most operating systems, “” is used for output redirection. Why redirect? Because there are many times when we want the output to the screen to be saved to a file and the input data instead of the keyboard is from a file.
See also: What is the Unit of Weight Measurement? What Is Volume? Category:Measurement Units
2.1. STDINSTDIN refers to standard input lines in general and it is usually from the keyboard. When we type on the keyboard, we are entering STDIN. For the input data to be a file, we use the “$ cat /etc/passwd” sign, we can use: $ cat /etc/passwd or $ cat 0 /etc/passwd on one’s own? That’s because every time a process is initialized, the system has assigned a standard input line for that process, which is STDIN or 0.2.2. STDOUTSTDOUT is standard output in general and it is usually output to the screen, to the console or to the terminal. For output data to be written to a file, we use the “>” sign. For example, if we want the list of files in a directory to be written to the file dir.txt, we use the following command: $ ls -al > dir.txt or $ ls -al 1> dir.txt 1 goes similar to STDIN, ie when initializing a process, the system has attached a stdout line to that process, which here is STDOUT or 1. Here we can combine parallel use of STDIN and STDOUT to do file copy operation. For example, if we want to backup the file /etc/passwd, we can do the following: $ cat /etc/passwd > ~/passwd.bak This command is equivalent to the command: $ cp /etc/passwd ~/passwd.bak There is an application The most useful of these combinations is converting text files between Windows and Unix. As you all know, in Windows text files, line breaks are represented by a pair of characters
in Linux/Unix it’s just
. Anyone who has to code on both environments finds the switch inconvenient. The solution given here is to use the tr command, specifically as follows: tr -d ”
” win.cpp > unix.cpp This command will take the standard input and then remove the characters
and then write the standard output. The input and output streams here are redirected to come from a file and write to a file. However, if “>” is used, the contents of the file will be erased before writing new content. If we want the new content to be serialized to the file, we use 2 larger signs, i.e. “>>”. For example, if you want to append the contents of the /home directory to the end of the passwd.bak file above, you do the following: $ ls /home >> ~/passwd.bak Now if we want to get the HTML code of the OSG homepage and write to the file osg.html, then we use the following command: $ curl http://osg.vnu.edu.vn/ > osg.html Do you see anything strange when you execute the above command? Although the HTML code instead of output to the screen is included in the osg.html file, there is still information showing the download status displayed on the screen. How can it be like that? How to completely silence curl command? After going clear back
.2.3. STDERRSTDERR is the standard error output stream in general, and it also often outputs directly to the screen, console or terminal. The syntax is similar to STDOUT, ie using “>” to output a file and “>>” to append to an existing file (if not, the system will create it). However, the difference is that you must specify the number 2, i.e. “2>” or “2>>”. The reason is because there is only 1 stdout and 1 stdin for each process which normally the system specifies as STDOUT and STDIN. So in the case of the curl command in section 2.2 above, if we want to write both types that output to a file, we do the following: $ curl http://osg.vnu.edu.vn/ > osg.html 2> osg.log How? Nothing shows up on the screen, right? Because the web page content has been saved to the osg.html file and the download status lines have been written to the osg.log file. But that takes up disk space and risks causing disk damage because the file has to be written. Humans are really greedy
. Then we have to create something like a bottomless barrel or better call it a “black hole” or a “black hole”, that is, a place where anything you put in will disappear. Linux/Unix has that for you, it’s /dev/null. 2.4. /dev/nullAccording to the Wikipedia definition of /dev/null:
In Unix-like operating systems, /dev/null or the null device is a special file that discards all data written to it (but reports that the write operation succeeded), and provides no data to any process that reads from it (it returns EOF). In Unix programmer jargon, it may also be called the bit bucket or black hole.
In Unix-style operating systems, /dev/null or the null device is a special file that ignores any data written to it (but reports successful writes) and does not provide any data. any data when read from it (returns EOF). In Unix programmer lingo, it’s called a “bit bucket” or a “black hole.” Then that’s what we need. So the above curl command can be made silent by: $ curl http://osg.vnu.edu.vn/ > /dev/null 2> /dev/null Nothing is output to the screen Also, nothing is recorded. But… but, people are still very greedy, how to make the above command more concise, look more technical, in general, so that anyone who does not know will not understand anything (sometimes that is the hobby of technical people) . We will use “2>&1” here, ie: $ curl http://osg.vnu.edu.vn/ > /dev/null 2>&1 The above statement means that stdout (1) will be included /dev/null and the standard error line (2) will be included in the stdout (1) which here is /dev/null. In particular, with the syntax using a &, the & and the > sign must go. contiguous, no space. In addition to the file descriptor 0, 1, 2 above, there are also from 3 -> 9. However, this article is only for the newbie màn chơi, so you can learn more about it yourself on the Internet or in books about shell programming.
See also: Dreaming of a Viper – Dreaming of a Green Snake
3. PipeSo we know how to redirect the input/output flow of a command or a process. In this way we can convert the output of one command into the input of another command through an intermediate file. However, we do not want that intermediate file, partly because of writing to the hard disk, the other part is due to… greed. That is the problem that pipe solves. In Linux, we use the “|” sign. For example, when we want to review the contents of the /etc directory but its results are too long and we want to review it, we do the following$ ls -al /etc | moreor$ ls -al /etc | less(quit with q).or we want to count the number of users in the system using the default bash shell, we do the following:$ cat /etc/passwd | grep “/bin/bash” | wc -l This command is meant to output the contents of the /etc/passwd file to stdout; this stdout becomes the grep command’s stdout, and this command only filters out lines that do not contain the string “/bin/bash” to give stdout; this stdout becomes the standard input line of the wc command -l is the command that counts the number of lines of the standard input and outputs the number of lines to the stdout; finally this stdout will be shown directly to the screen because it doesn’t become the standard input of any command anymore.
Bạn thấy bài viết Làm Thế Nào Để Bạn Đọc Từ Stdin Là Gì ? Stdin,Stdout,Stderr có khắc phục đươc vấn đề bạn tìm hiểu ko?, nếu ko hãy comment góp ý thêm về Làm Thế Nào Để Bạn Đọc Từ Stdin Là Gì ? Stdin,Stdout,Stderr bên dưới để yt2byt.edu.vn có thể thay đổi & cải thiện nội dung tốt hơn cho độc giả nhé! Cám ơn bạn đã ghé thăm Website Trường Cao đẳng Kỹ thuật Y tế II
Phân mục: Hỏi đáp
Nguồn: yt2byt.edu.vn