linux I and o redirection USES elaboration
- 2020-05-07 20:45:07
- OfStack
1. Output redirection:
By default, both standard output and error output are terminals, and the standard output and error contents can be redirected:
[~]# echo "hello\!"
hello\!
[~]# echo "hello!"
-bash: !": event not found "
Redirect standard output to a file
[~]# echo "hello" > test.sh
[~]# cat test.sh
hello
'>' Output mode is equivalent by default '1>'
[~]# echo "hello" 1> test.sh
[~]# cat test.sh
hello
But the error is still displayed on the screen:
[~]$cat edit.sql /root/test.sh > temp.sh
cat: /root/test.sh: Permission denied
Error contents can also be output to a file (using the file descriptor) :
[~]$cat edit.sql /root/test.sh 1> temp.sh 2> error.sh
[~]$cat temp.sh
select dbms_rowid.rowid_object('AAAZdQAAGAAATxjAAk') data_object_id#,
dbms_rowid.rowid_relative_fno('AAAZdQAAGAAATxjAAk') rfile#,
dbms_rowid.rowid_block_number('AAAZdQAAGAAATxjAAk') block#,
dbms_rowid.rowid_row_number('AAAZdQAAGAAATxjAAk') row# from dual
/
[~]$cat error.sh
cat: /root/test.sh: Permission denied
Write standard output and error messages to the same file:
[~]$cat edit.sql /root/test.sh > temp.sh 2>&1
[~]$cat temp.sh
select dbms_rowid.rowid_object('AAAZdQAAGAAATxjAAk') data_object_id#,
dbms_rowid.rowid_relative_fno('AAAZdQAAGAAATxjAAk') rfile#,
dbms_rowid.rowid_block_number('AAAZdQAAGAAATxjAAk') block#,
dbms_rowid.rowid_row_number('AAAZdQAAGAAATxjAAk') row# from dual
/
cat: /root/test.sh: Permission denied
This might seem like a bit of a hassle, but in practice it's probably used the most:
[~]$cat edit.sql /root/test.sh &>temp.sh
[~]$cat temp.sh
select dbms_rowid.rowid_object('AAAZdQAAGAAATxjAAk') data_object_id#,
dbms_rowid.rowid_relative_fno('AAAZdQAAGAAATxjAAk') rfile#,
dbms_rowid.rowid_block_number('AAAZdQAAGAAATxjAAk') block#,
dbms_rowid.rowid_row_number('AAAZdQAAGAAATxjAAk') row# from dual
/
cat: /root/test.sh: Permission denied
&> Write all the output to the same 1 A file
If you don't want to output to a file or display it on the screen, you can use /dev/null as a special device file (bit bucket)
[~]$cat edit.sql /root/test.sh &
>
/dev/null
If you write standard output to a file, you can't use the pipe symbol '|' to pass the contents to the following command. You can use the tee command to solve this problem:
[~]$cat edit.sql /root/test.sh | tee temp.sh | cat -n
cat: /root/test.sh: Permission denied
select dbms_rowid.rowid_object('AAAZdQAAGAAATxjAAk') data_object_id#,
dbms_rowid.rowid_relative_fno('AAAZdQAAGAAATxjAAk') rfile#,
dbms_rowid.rowid_block_number('AAAZdQAAGAAATxjAAk') block#,
dbms_rowid.rowid_row_number('AAAZdQAAGAAATxjAAk') row# from dual
/
The tee command is equivalent to writing a copy of stdout to a file and passing stdout to the next command, but the error content cannot be passed using tee, as used
>
>
To append content,tee can append with the -a option:
[~]$cat edit.sql /root/test.sh | tee -a temp.sh | cat -n
cat: /root/test.sh: Permission denied
select dbms_rowid.rowid_object('AAAZdQAAGAAATxjAAk') data_object_id#,
dbms_rowid.rowid_relative_fno('AAAZdQAAGAAATxjAAk') rfile#,
dbms_rowid.rowid_block_number('AAAZdQAAGAAATxjAAk') block#,
dbms_rowid.rowid_row_number('AAAZdQAAGAAATxjAAk') row# from dual
/
[~]$cat temp.sh
select dbms_rowid.rowid_object('AAAZdQAAGAAATxjAAk') data_object_id#,
dbms_rowid.rowid_relative_fno('AAAZdQAAGAAATxjAAk') rfile#,
dbms_rowid.rowid_block_number('AAAZdQAAGAAATxjAAk') block#,
dbms_rowid.rowid_row_number('AAAZdQAAGAAATxjAAk') row# from dual
/
select dbms_rowid.rowid_object('AAAZdQAAGAAATxjAAk') data_object_id#,
dbms_rowid.rowid_relative_fno('AAAZdQAAGAAATxjAAk') rfile#,
dbms_rowid.rowid_block_number('AAAZdQAAGAAATxjAAk') block#,
dbms_rowid.rowid_row_number('AAAZdQAAGAAATxjAAk') row# from dual
/
2. Input redirection :
Input redirection, in some cases, such as the database monitoring used more often, is the use of inline redirection
[~]$cat < edit.sql
select dbms_rowid.rowid_object('AAAZdQAAGAAATxjAAk') data_object_id#,
dbms_rowid.rowid_relative_fno('AAAZdQAAGAAATxjAAk') rfile#,
dbms_rowid.rowid_block_number('AAAZdQAAGAAATxjAAk') block#,
dbms_rowid.rowid_row_number('AAAZdQAAGAAATxjAAk') row# from dual
/
For example, the following operation, put
<
<
EOF
>
The content between temp.sh and the last EOF is treated as stdin, then stdout is written to the file temp.sh
[~]$cat <<EOF > temp.s
this is my log
EOF
[~]$cat temp.sh
this is my log
3. Custom file descriptor:
The principle behind the custom file descriptor is to use the basic three file open modes
Read-only (
<
)
Truncation (
>
)
Additional,
>
>
)
Create a file descriptor 3 to open the file
[~]# exec 3
<
test.sh
Now you can open the file directly using the file descriptor, but only once:
[~]# cat <&3
hello
hello
Create file descriptor 4 for writing to files (reusable) :
[~]# exec 4>test.sh
[~]# echo okok >&4
[~]# cat test.sh
okok
This is actually similar to the previous one:
[~]$cat edit.sql /root/test.sh > temp.sh 2>&1
Create file descriptor 5, which is used to append content to the file (it can also be reused, not used once as input file descriptor) :
[~]# exec 5>>test.sh
[~]# echo okokok >&5
[~]# cat test.sh
okokok
[~]# echo okokok >&5
[~]# cat test.sh
okokok
okokok