分享

Learning_the_bash_Shell_Third_Edition 14/n

 新进小设计 2022-11-08 发布于北京

Chapter 7

Input/Output and Command-Line Processing

 I/O Redirectors

The redirector <> is mainly meant for use with device files (in the /dev directory), i.e., files that correspond to hardware devices such as terminals and communication lines. Low-level systems programmers can use it to test device drivers; otherwise, it’s not very useful.

 

Redirector

Function

cmd1 | cmd2 Pipe; take standard output of cmd1 as standard input to cmd2.
> file Direct standard output to file.
< file Take standard input from file.
>> file Direct standard output to file; append to file if it already exists.
>| file Force standard output to file even if noclobber is set.
n>| file Force output to file from file descriptor n even if noclobber is set.
<> file Use file as both standard input and standard output.
n<> file Use file as both input and output for file descriptor n.
<< label Here-document; see text.
n> file Direct file descriptor n to file.
n< file Take file descriptor n from file.
n>> file Direct file descriptor n to file; append to file if it already exists.
n>& Duplicate standard output to file descriptor n.
n<& Duplicate standard input from file descriptor n.
n>&m File descriptor n is made to be a copy of the output file descriptor.
n<&m File descriptor n is made to be a copy of the input file descriptor.
&>file Directs standard output and standard error to file.
<&- Close the standard input.
>&- Close the standard output.
n>&- Close the output from file descriptor n.
n<&- Close the input from file descriptor n.
n>&word

If n is not specified, the standard output (file descriptor 1) is used. If the digits in word do not specify a file descriptor open for output, a redirection error occurs. As a special case, if n is omitted, and word does not expand to one or more digits, the standard output and standard error are redirected as described previously.

n<&word

If word expands to one or more digits, the file descriptor denoted by n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to -, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.

 

n>&digit-

Moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified.

n<&digit-

Moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.

Here-documents 

The << label redirector essentially forces the input to a command to be the shell’s standard input, which is read until there is a line that contains only label. The input in between is called a here-document. Here-documents aren’t very interesting when used from the command prompt. In fact, it’s the same as the normal use of standard input except for the label. We could use a here-document to simulate the mail facility. When you send a message to someone with the mail utility, you end the message with a dot (.). The body of the message is saved in a file, msgfile:

$ cat >> msgfile << .
> this is the text of
> our message.
> .

  bashbug script

MACHINE="i586"
OS="linux-gnu"
CC="gcc"
CFLAGS=" -DPROGRAM='bash' -DHOSTTYPE='i586' -DOSTYPE='linux-gnu' -DMACHTYPE='i586-pc-linux-gnu' -DSHELL -DHAVE_CONFIG_H
-I. -I. -I./lib -g -O2"
RELEASE="2.01"
PATCHLEVEL="0"
RELSTATUS="release"
MACHTYPE="i586-pc-linux-gnu"
TEMP=/tmp/bbug.$$
case "$RELSTATUS" in
alpha*|beta*)
BUGBASH=chet@po.cwru.edu ;;
*)
BUGBASH=bug-bash@prep.ai.mit.edu ;;
esac
BUGADDR="${1-$BUGBASH}"
UN=
if (uname) >/dev/null 2>&1; then
UN=`uname -a`
fi
cat > $TEMP <<EOF
From: ${USER}
To: ${BUGADDR}
Subject: [50 character or so descriptive subject here (for reference)]
Configuration Information [Automatically generated, do not change]:
Machine: $MACHINE
OS: $OS
Compiler: $CC
Compilation CFLAGS: $CFLAGS

name output: $UN
Machine Type: $MACHTYPE
bash Version: $RELEASE
Patch Level: $PATCHLEVEL
Release Status: $RELSTATUS
Description:
[Detailed description of the problem, suggestion, or complaint.]
Repeat-By:
[Describe the sequence of events that causes the problem
to occur.]
Fix:
[Description of how to fix the problem. If you don't know a
fix for the problem, don't include this section.]
EOF
vi $TEMP
mail $BUGADDR < $TEMP

  

File Descriptors

 

Task 7-2

You want to start a long job in the background (so that your terminal is freed up) and
save both standard output and standard error in a single log file. Write a script that
does this.

We’ll call this script start. The code is very terse:

"$@" > logfile 2>&1 &

  This line executes whatever command and parameters follow start. (The command cannot contain pipes or output redirectors.) It sends the command’s standard output to logfile.Then, the redirector 2>&1 says, “send standard error (file descriptor 2) to the same place as standard output (file descriptor 1).” Since standard output is redirected to 

logfile, standard error will go there too. The final & puts the job in the background so that you get your shell prompt back.

As a small variation on this theme, we can send both standard output and standard error into a pipe instead of a file: command 2>&1 | ... does this. (Make sure you understand why.) Here is a script that sends both standard output and standard error to the logfile (as above) and to the terminal:

 

"$@" 2>&1 | tee logfile &

  

These scripts have one shortcoming: you must remain logged in until the job completes. Although you can always type jobs (see Chapter 1) to check on progress, you can’t leave your terminal until the job finishes, unless you want to risk a breach of security.  We’ll see how to solve this problem in the next chapter.

 Before we leave this topic, we should just note that 1> is the same as >, and 0< is the same as <. If you understand this, then you probably know all you need to know about file descriptors.

String I/O

echo|printf|read

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多