Bash scripts: start
Bash scripts, part 2: loops
Bash scripts, part 3: command line options and keys
Bash scripts, part 4: input and output
Bash Scripts, Part 5: Signals, Background Tasks, Script Management
Bash scripts, part 6: functions and library development
Bash scripts, part 7: sed and word processing
Bash scripts, part 8: awk data processing language
Bash scripts, part 9: regular expressions
Bash scripts, part 10: practical examples
Bash scripts, part 11: expect and automate interactive utilities


0 , STDIN — standard input stream.1 , STDOUT — standard output stream.2 , STDERR — standard error stream.STDIN — the standard shell input stream. For the terminal, the standard input is the keyboard. When an input redirection character is used in scripts - < , Linux replaces the standard input file descriptor with the one specified in the command. The system reads the file and processes the data as if they were entered from the keyboard.STDIN if no file is specified on the command line from which to take data. For example, this is true for the cat .cat command on the command line without specifying any parameters, it accepts input from STDIN . After you enter the next line, cat simply displays it on the screen.STDOUT — standard shell output stream. By default this is the screen. Most bash commands output data to STDOUT , which causes it to appear in the console. The data can be redirected to a file by attaching it to its contents, the >> command serves for this. pwd >> myfile pwd will print will be added to the file myfile , while the data already in it will not go anywhere.xfile , conceiving all this so that an error message xfile into the file myfile . ls –l xfile > myfile STDERR is a standard shell error stream. By default, this descriptor points to the same thing as STDOUT , which is why we see a message on the screen when an error occurs.STDERR — file descriptor STDERR — 2. We can redirect errors by placing this descriptor in front of the redirect command: ls -l xfile 2>myfile cat ./myfile myfile . ls –l myfile xfile anotherfile 2> errorcontent 1> correctcontent ls normally sends to STDOUT to the correctcontent file correctcontent to the 1> construction. Error messages that would fall into STDERR appear in the errorcontent file due to the redirection command 2> .STDERR and STDOUT can be redirected to the same file using the &> command:STDERR and STDOUT is in the content file.STDERR . In order to do this, it is enough to use the redirection command, specifying the STDERR descriptor, and you should put the ampersand symbol ( & ) before the descriptor number: #!/bin/bash echo "This is an error" >&2 echo "This is normal output" STDERR gets to the file. ./myscript 2> myfile echo call is inconvenient. Instead, you can set output redirection to a specific descriptor for the duration of the script execution, using the exec command: #!/bin/bash exec 1>outfile echo "This is a test of redirecting all output" echo "from a shell script to another file." echo "without having to redirect every line" echo commands got into this file.exec command can be used not only at the beginning of the script, but also in other places: #!/bin/bash exec 2>myerror echo "This is the start of the script" echo "now redirecting all output to another location" exec 1>myfile echo "This should go to the myfile file" echo "and this should go to the myerror file" >&2 exec command sets the output redirection from STDERR to the file myerror . Then the output of several echo commands is sent to STDOUT and displayed on the screen. After that, the exec command sets the sending of what gets into STDOUT to the file myfile , and finally, we use the redirect command to STDERR in the echo command, which causes the corresponding line to be written to the myerror. file myerror.exec command allows you to make a file a data source for STDIN : exec 0< myfile myfile , not the usual STDIN . Let's look at input redirection in action: #!/bin/bash exec 0< testfile count=1 while read line do echo "Line #$count: $line" count=$(( $count + 1 )) done read command to read user input from the keyboard. If you redirect the input, making the file a data source, then the read command, when you try to read data from STDIN , will read it from the file, not from the keyboard.exec command: #!/bin/bash exec 3>myfile echo "This should display on the screen" echo "and this should be stored in the file" >&3 echo "And this should be back on the screen" 3 .STDIN in another descriptor before redirecting the input.STDIN and use it as usual: #!/bin/bash exec 6<&0 exec 0< myfile count=1 while read line do echo "Line #$count: $line" count=$(( $count + 1 )) done exec 0<&6 read -p "Are you done now? " answer case $answer in y) echo "Goodbye";; n) echo "Sorry, this is the end.";; esac STDIN . Then the input was redirected, the data source for STDIN was the file. After that, the input to the read command came from the redirected STDIN , that is, from the file.STDIN to its original state, redirecting it to handle 6 . Now, in order to check that everything works correctly, the script asks the user a question, waits for input from the keyboard and processes what is entered.&- . It looks like this: #!/bin/bash exec 3> myfile echo "This is a test line of data" >&3 exec 3>&- echo "This won't work" >&3 lsof command. On many distributions, like Fedora, the lsof utility is in /usr/sbin . This command is very useful as it displays information about each descriptor that is open in the system. This includes what the processes running in the background have opened, and what has been opened by the logged in users.-p Allows you to specify the process ID .-d Allows you to specify the descriptor number to get information about.PID current process, you can use the special $$ environment variable in which the shell writes the current PID .-a switch is used to perform the logical operation on the results returned by using two other keys: lsof -a -p $$ -d 0,1,2 STDIN , STDOUT and STDERR — CHR (character mode). Since they all point to the terminal, the file name corresponds to the device name assigned to the terminal. All three standard files are readable and writeable.lsof from a script in which other descriptors are open, in addition to the standard ones: #!/bin/bash exec 3> myfile1 exec 6> myfile2 exec 7< myfile3 lsof -a -p $$ -d 0,1,2,3,6,7 3 and 6 ) and one for input ( 7 ). The paths to the files used to configure the descriptors are also displayed here./dev/null . This is something like a black hole. ls -al badfile anotherfile 2> /dev/null cat /dev/null > myfile 
Source: https://habr.com/ru/post/326594/
All Articles