[Shell] Bash basic functions: Input/Output redirection, shellbash
/* Configure /*-----------------------------------------------------------------------------------------------
@ Blackeye poet <www. chenwei. ws>
Optional -----------------------------------------------------------------------------------------------*/
Role of input/output redirection:
Output redirection saves the command execution result to a file for easy viewing.
Input redirection is to change the original keyboard input to file input.
First, let's take a look at the standard input and output:
[Device] [device file name] [file descriptor] [type]
Keyboard/dev/stdin 0 standard input
Display/dev/stdout 1 standard output
Display/dev/stderr2 Standard Error output
Concepts (> Overwrite,> append)
[Correct output redirection]
Command> file # overwrite the command and output it to a file or device.
Command> file # append the command to the correct output to a file or device.
[Error output redirection]
Error command 2> file # overwrite the output of command errors to the specified file or device
Error command 2> file # output the command error to a specified file or device in append Mode
(Note: To save the error message, you must add 2 before the single sign or double sign, with no space in the middle)
[Correct output and error output are saved at the same time]
1. Command> File2> & 1# Store both the correct and error outputs to the same file in overwrite Mode
2. Command> file 2> & 1 # Save both the correct and error outputs to the same file in append mode.
3. Commands&> File # is equivalent to the first one (& the role is equivalent to 2> & 1)
4. Command &> file # is equivalent to the second
5.Command> file A 2> file B# Append the correct output to file A and append the wrong output to file 2 (useful during regular backup)
Ls&>/Dev/null# Save the execution result to/dev/null (devices that do not exist are about to clear meaningless results)
Input redirection:
[Wc for keyboard input statistics]
Wc [-c] file name # count the number of bytes
Wc [-w] file name # count the number of words
Wc [-l] file name # Number of Statistics rows
(After wc is input, other characters will be counted. ctrl + d ends)
(4 rows, 7 words, 40 characters)
[Wc Statistical File: a unit smaller than the number]
SHELL programming output redirection
Grep-v $ bbb xxx.txt | cat> xxx.txt
Operations on the same file before and after the MPs queue are dangerous. In this way, two programs can read and write the same file at the same time, which leads to competition. The final result is hard to determine. It is best to process them separately.
Rep-v $... xxx.txt> xxx.txt does not work well either. After redirection, the same file will be read and written simultaneously. But at least it is controlled by the same program (if not shell ). Grep may have considered this situation, or coincidentally (read and write before processing), so the result is correct. However, with the cooperation of other programs, the situation is more complicated and problems may occur.
Depends on grep, shell, and cat code.
However, we do not recommend this. This is a very dangerous action. It is best to use a backup file to avoid simultaneous read/write operations on a file.
Bash output redirection and tabulation Problems
Not very clear...
Declare-a folder = (folder1 folder2 );
For x in $ {folder [*]}; do
Find "$ x"-mtime-1-type f-exec \
Awk'
Function fun () {printf "output "}
NR = 1
{"Yourscript" FILENAME | getline var; print var;
Print $0 "\ t" fun ()
'{}\;
Done