Note: The example of this article comes from the collection and collation of the network, with my comments attached. Common uses of ①i/o redirection
Symbolic meaning
cmd1 | cmd2 pipe character, the standard output of CMD1 as the standard input for CMD2.
> FileName writes the standard output to the file filename.
< filename reads the contents of the file filename into the standard input.
>>filename writes the standard output to the file filename, and if the filename file already exists, appends the standard output to the existing content of filename.
>|filename Even if the noclobber option is turned on, the standard output is still forced to be written to the file filename, overwriting the filename file.
n>|filename Use FD to N to open the file filename in truncate mode even if the Noclobber option is turned on.
n> filename uses FD as n to open the file filename in truncate mode.
n< filename uses FD as n to open the file filename in read mode.
n>>filename Opens the file filename using FD for n in append mode.
<< delimiter here Document (Here-document).
Introduction to the ②exec command
1. Open or close the file by file descriptor.
2, redirect the file to standard input, and redirect the standard output to a file. The
3,exec command resets the I/O redirection, to revert to the original I/O point, and to use the EXEC command to display the specified.
exec usage:
&n : Represents a file representing a descriptor.
> <: Represents the form in which descriptors are used.
EXEC 8<&2: Descriptor 8 Opens the file corresponding to the standard error in read mode.
exec &>log: Open the file log with the standard input error.
EXEC 8<&-: Close descriptor 8.
1): run.sh script uses exec to redirect stdin (standard input) to a file
root@37c:~# cat run.sh
#!/bin/bash
exec 8<&0 # Standard input is pointing to the keyboard by default, copying one to 8, and 8 pointing to the keyboard.
exec 0< hfile # 1. Open the file hfile using standard input. Read
a # 2. Read reads the command from the stdin.
Read B
echo "---------------------------"
echo $a
echo $b echo
"Close FD 8:"
exec 0< &8 8<&- #将FD-8 Copy to standard input (restores to the keyboard) otherwise, the standard input is still point to hfile.
echo-n "Pls. Enter Data:" #这里, the standard input is re-pointing to the keyboard.
read C #需要我们手工键入字符.
echo $c
exit 0
Result output:
# hfile The contents of the file.
root@37c:~# cat hfile
value1
value2
root@37c:~#./run.sh
---------------------------
value1
value2
Close FD 8:
Pls. Enter data:value3
value3
root@37c:~#
2): run.sh script redirects stdout to file
root@37c:~# cat run.sh
#!/bin/bash
exec 8>&1 # Opens the FD-8 and makes it point to the monitor.
exec 1> Log # points the standard output to the log file
echo '------Redirect to the log file-------'
# This part of the output will be stored in the log.
echo "Output of date command:"
date
echo "Output of DF command:"
df
sleep 5s # sleep 5 seconds first to differentiate between two The resulting output.
exec 1>&8 8>&-
echo "------termincal display:-------"
echo "Output of Date command"
date
echo "Output of DF Command"
DF
Result output:
root@37c:~#./run.sh------termincal Display:-------Output of date command September 20, 2016 Tuesday 16:02:08 CST Output of DF C Ommand file system 1k-block used available% mount point udev 355032 0 355032 0%/dev Tmpfs 75824 3020 72804 4%/run/dev/sda1 17673004 6600160 10152064 40%/TMPFS 379108 132 378976 1%/dev/shm tmpfs 5120 4 5116 1%/run/lock tmpfs 379108 0 379108 0%/sys/f S/cgroup tmpfs 75824 75776 1%/run/user/1000 root@37c:~# Cat log------Redirect to log file----- --Output of date command:2016 September 20 Tuesday 16:02:03 CST Output of DF command: File system 1k-block used available% mount point UD EV 355032 0 355032 0%/dev tmpfs 75824 3020 72804 4%/run/dev/sda1 17673 004 6600160 10152064 40%/Tmpfs 379108 the 378976 1%/dev/shm TMPFS 5120 4 5 1%/run/lock TmpfS 379108 0 379108 0%/sys/fs/cgroup tmpfs 75824-75776 1%/run/user/1000 ro ot@37c:~#
3): &>file redirect stdout and stderr to file
#!/bin/bash
exec 8>&1 9>&2 #FD 1 copy to FD 8,fd 2 copy to FD 9
exec &> log #&> Symbols redirect stdout and stderr to file log
ls z* #错误写入文件log
date #输出写入文件log
exec 1>&8 2>&9 8< &-9<&- #恢复关闭操作
echo "-----------------"
echo "Close FD 8 and 9:"
ls z*
date
4): General examples
_1): Use file descriptor 3 to open the file in read mode run.py
root@37c:~# exec 3<run.py
root@37c:~# cat <&3
#!/usr/bin/python3
#-*-Coding:utf-8 -* -
Import Logging
logging.basicconfig (level=logging.info)
s = ' 0 '
n = Int (s)
logging.info (' n =%d '% n)
print (10/n)
_2): Use file descriptor 4 to open files in truncated mode log
root@37c:~# exec 4>log
root@37c:~# echo "I think befor i am" 1>&4
root@37c:~# cat log
I think befor I am
_3): Use file descriptor 5 to open file in Append mode log
root@37c:~# exec 5>>log
# here echo uses ' > ' rather than ' >> ' to append.
root@37c:~# Echo ' say again:i think before I am! ' >&5
root@37c:~# cat log
I think befor I am
say Again:i Think before I am!
_4): After exec performs the observation.
# FD 8,9 also points to the file that FD 0, 1 points to.
root@37c:~# exec 9>&1
root@37c:~# exec 8<&0
root@37c:~# ls-l/proc/self/fd/
Total usage 0
lrwx------1 root root 64 September 21:03 0-/DEV/PTS/2
lrwx------1 root root 64 September 21:03 1-/dev/pt S/2
lrwx------1 root root 64 September 21:03 2/dev/pts/2
lr-x------1 root root 64 September 21:03 3-&G T /PROC/20455/FD
lrwx------1 root root 64 September 21:03 8/dev/pts/2
lrwx------1 root root 64 September 2 1 21:03 9-/DEV/PTS/2
③ try a question
# Try the results of the operation.
root@37c:~# exec 0<run.sh