> &> >& >> < <> ,标准输入、输出、错误输出
①标准输入、输出、错误输出的使用方式
输入0: < (或<< 追加输入) /dev/stdin
输出1: > (或>> 追加输出) /dev/stdout
错误输出2: 2> (或2>>) /dev/stderr
②重定向
&> stdout and the stderr,标准输出和错误输出一起输出
>& redirects stdout of command to stderr,将标准输出重定向到错误输出
cmd &> file 等同于 cmd > file 2>&1 (cmd > file 1>&2 等同于cmd )
find /usr/home -name .profile 2>&1 | more
③重定义文件标识符,exec
重新定义文件标识符可以用i>&j命令,表示把文件标识符i重新定向到j,可以把"&"理解为"取地址"
#exec 5>&1 文件标识符5重定向到标准输出
#exec 3<>filename 把文件filename打开,并指定文件标识符为3
#exec 3>&- 关闭文件标识符3
命令j<>filename表示把文件打开,并指明文件标识符为j
"&-"表示关闭文件标识符
n<&- 关闭输入文件标识符n
0<&-或<&- 关闭标准输入stdin
n>&- 关闭输出文件标识符n
1>&-或>&-关闭标准输出stdou
exec < filename 重定向输入为一个文件,而不是从标准输入(键盘等)。用于操作文件。
#!/bin/bash
# Redirecting stdin using 'exec'.
exec 6<&0 # Link file descriptor #6 with stdin.
# Saves stdin.
exec < data-file # stdin replaced by file "data-file"
read a1 # Reads first line of file "data-file".
read a2 # Reads second line of file "data-file."
echo
echo "Following lines read from file."
echo "-------------------------------"
echo $a1
echo $a2
echo; echo; echo
exec 0<&6 6<&-
# Now restore stdin from fd #6, where it had been saved,
#+ and close fd #6 ( 6<&- ) to free it for other processes to use.
#
# <&6 6<&- also works.
echo -n "Enter data "
read b1 # Now "read" functions as expected, reading from normal stdin.
echo "Input read from stdin."
echo "----------------------"
echo "b1 = $b1"
echo
exit 0
exec > filename 重定向输出到一个具体的文件,使得所有的输出都保存在文件中
exec N > filename 影响着整个或者当前的脚本
exec > filename 影响着新创建的脚本
#!/bin/bash
# reassign-stdout.sh
LOGFILE=logfile.txt
exec 6>&1 # Link file descriptor #6 with stdout.
# Saves stdout.
exec > $LOGFILE # stdout replaced with file "logfile.txt".
# ----------------------------------------------------------- #
# All output from commands in this block sent to file $LOGFILE.
echo -n "Logfile: "
date
echo "-------------------------------------"
echo
echo "Output of \"ls -al\" command"
echo
ls -al
echo; echo
echo "Output of \"df\" command"
echo
df
# ----------------------------------------------------------- #
exec 1>&6 6>&- # Restore stdout and close file descriptor #6.
echo
echo "== stdout now restored to default == "
echo
ls -al
echo
exit 0
同时重定向输入和输出
#!/bin/bash
# upperconv.sh
# Converts a specified input file to uppercase.
E_FILE_ACCESS=70
E_WRONG_ARGS=71
if [ ! -r "$1" ] # Is specified input file readable?
then
echo "Can't read from input file!"
echo "Usage: $0 input-file output-file"
exit $E_FILE_ACCESS
fi # Will exit with same error
#+ even if input file ($1) not specified (why?).
if [ -z "$2" ]
then
echo "Need to specify output file."
echo "Usage: $0 input-file output-file"
exit $E_WRONG_ARGS
fi
exec 4<&0
exec < $1 # Will read from input file.
exec 7>&1
exec > $2 # Will write to output file.
# Assumes output file writable (add check?).
# -----------------------------------------------
cat - | tr a-z A-Z # Uppercase conversion.
# ^^^^^ # Reads from stdin.
# ^^^^^^^^^^ # Writes to stdout.
# However, both stdin and stdout were redirected.
# Note that the 'cat' can be omitted.
# -----------------------------------------------
exec 1>&7 7>&- # Restore stout.
exec 0<&4 4<&- # Restore stdin.
# After restoration, the following line prints to stdout as expected.
echo "File \"$1\" written to \"$2\" as uppercase conversion."
exit 0
OM_STDIO
最新推荐文章于 2024-07-12 18:42:17 发布