Linux shell 脚本编程-基础篇 (五)

Linux shell 脚本编程-基础篇 (四)

5 呈现数据


到目前为止。所演示的脚本都是通过将数据打印在屏幕上或将数据重定向到文件中来显示信息。本部分内容将展开这个主题,演示如何将脚本的输出重定向
到 Linux 系统的不同位置。


5.1 理解输入和输出
-----------------------------------------------------------------------------------------------------------------------------------------
至此已经知道了两种显示脚本输出的方法:

    □ 在显示器屏幕上显示输出
    □ 将输出重定向到文件中

这两种方法要么将数据输出全部显示,要么什么都不显示。但有时将一部分数据在显示器上显示,另一部分数据保存到文件中也是不错的。对此,了解Linux
如何处理输入输出能够帮助将脚本输出放到正确位置。


5.1.1 标准文件描述符
-----------------------------------------------------------------------------------------------------------------------------------------
Linux 系统将每个对象当作文件处理。这包括输入和输出进程。Linux 用文件描述符(file descriptor)来标识每个文件对象。文件描述符是一个非负整数,
可以唯一标识会话中打开的文件。每个进程一次最多可以有九个文件描述符。出于特殊目的,bash shell 保留了前三个文件描述符(0、1和2),如下表所示

    Linux的标准文件描述符
    +---------------+-----------+-----------------------------------------------
    | 文件描述符    | 缩 写        | 描 述
    +---------------+-----------+-----------------------------------------------
    | 0                | STDIN        | 标准输入
    +---------------+-----------+-----------------------------------------------
    | 1                | STDOUT    | 标准输出
    +---------------+-----------+-----------------------------------------------
    | 2                | STDERR    | 标准错误
    +---------------+-----------+-----------------------------------------------


这三个特殊文件描述符会处理脚本的输入和输出。shell 用它们将 shell 默认的输入和输出导向到相应的位置。


■ STDIN
-----------------------------------------------------------------------------------------------------------------------------------------
STDIN 文件描述符代表 shell 的标准输入。对终端界面来说,标准输入是键盘。shell 从 STDIN 文件描述符对应的键盘获得输入,在用户输入时处理每个
字符。

在使用输入重定向符号(<)时,Linux 会用重定向指定的文件来替换标准输入文件描述符。它会读取文件并提取数据,就如同它是键盘上键入的。

许多 bash 命令能接受STDIN的输入,尤其是没有在命令行上指定文件的话。下面是个用 cat 命令处理 STDIN 输入的数据的例子:

    [devalone@devalone ~]$ cat
    this is a test
    this is a test
    this is a second test
    this is a second test

当在命令行上只输入 cat 命令时,它会从 STDIN 接受输入。输入一行,cat 命令就会显示出一行。

但也可以通过 STDIN 重定向符号强制 cat 命令接受来自另一个非 STDIN 文件的输入。

    [devalone@devalone 15]$ cat < testfile
    This is the first line.
    This is the second line.
    This is the third line.


■ STDOUT
-----------------------------------------------------------------------------------------------------------------------------------------
STDOUT 文件描述符代表 shell 的标准输出。在终端界面上,标准输出就是终端显示器。shell 的所有输出(包括 shell 中运行的程序和脚本)会被定向到
标准输出中,也就是显示器。

默认情况下,大多数 bash 命令会将输出导向 STDOUT 文件描述符。也可以用输出重定向来改变。

示例:
    [devalone@devalone 15]$ ll > test2
    [devalone@devalone 15]$ cat test2
    总用量 60
    -rwxrwxr-x. 1 devalone devalone 206 1月   4 2018 test10.sh
    -rwxrwxr-x. 1 devalone devalone 286 1月   4 2018 test11.sh
    -rwxrwxr-x. 1 devalone devalone 148 1月   7 13:15 test12.sh
    -rwxrwxr-x. 1 devalone devalone 209 1月   7 13:15 test13.sh
    -rwxrwxr-x. 1 devalone devalone 209 1月   7 13:15 test14.sh
    -rwxrwxr-x. 1 devalone devalone 303 1月   7 13:15 test15.sh
    -rwxrwxr-x. 1 devalone devalone 135 1月   7 13:15 test16.sh
    -rwxrwxr-x. 1 devalone devalone 178 1月   7 13:15 test17.sh
    -rwxrwxr-x. 1 devalone devalone 147 1月   7 13:15 test18.sh
    -rwxrwxr-x. 1 devalone devalone 354 1月   7 13:15 test19.sh
    -rw-rw-r--. 1 devalone devalone   0 7月   8 11:17 test2
    -rwxrwxr-x. 1 devalone devalone 252 1月   7 13:15 test20.sh
    -rwxrwxr-x. 1 devalone devalone 343 1月   7 13:15 test21.sh
    -rwxrwxr-x. 1 devalone devalone 239 1月   7 13:15 test22.sh
    -rwxrwxr-x. 1 devalone devalone  96 1月   4 2018 test8.sh
    -rw-rw-r--. 1 devalone devalone  72 7月   8 11:13 testfile

通过输出重定向符号,通常会显示到显示器的所有输出会被 shell 重定向到指定的重定向文件。也可以将数据追加到某个文件。这可以用 >> 符号来完成。

示例:
    [devalone@devalone 15]$ who >> test2
    [devalone@devalone 15]$ cat test2
    总用量 60
    -rwxrwxr-x. 1 devalone devalone 206 1月   4 2018 test10.sh
    -rwxrwxr-x. 1 devalone devalone 286 1月   4 2018 test11.sh
    -rwxrwxr-x. 1 devalone devalone 148 1月   7 13:15 test12.sh
    -rwxrwxr-x. 1 devalone devalone 209 1月   7 13:15 test13.sh
    -rwxrwxr-x. 1 devalone devalone 209 1月   7 13:15 test14.sh
    -rwxrwxr-x. 1 devalone devalone 303 1月   7 13:15 test15.sh
    -rwxrwxr-x. 1 devalone devalone 135 1月   7 13:15 test16.sh
    -rwxrwxr-x. 1 devalone devalone 178 1月   7 13:15 test17.sh
    -rwxrwxr-x. 1 devalone devalone 147 1月   7 13:15 test18.sh
    -rwxrwxr-x. 1 devalone devalone 354 1月   7 13:15 test19.sh
    -rw-rw-r--. 1 devalone devalone   0 7月   8 11:17 test2
    -rwxrwxr-x. 1 devalone devalone 252 1月   7 13:15 test20.sh
    -rwxrwxr-x. 1 devalone devalone 343 1月   7 13:15 test21.sh
    -rwxrwxr-x. 1 devalone devalone 239 1月   7 13:15 test22.sh
    -rwxrwxr-x. 1 devalone devalone  96 1月   4 2018 test8.sh
    -rw-rw-r--. 1 devalone devalone  72 7月   8 11:13 testfile
    devalone pts/0        2018-07-08 11:11 (192.168.1.101)

who 命令生成的输出会被追加到test2文件中已有数据的后面。

但是,如果对脚本使用了标准输出重定向,会遇到一个问题。

示例:
    [devalone@devalone 15]$ ls -al badfile > test3
    ls: 无法访问'badfile': No such file or directory
    [devalone@devalone 15]$ cat test3

当命令生成错误消息时,shell 并未将错误消息重定向到输出重定向文件。shell 创建了输出重定向文件,但错误消息却显示在了显示器屏幕上。在显示
test3 文件的内容时并没有任何错误。test3 文件创建成功了,只是里面是空的。

shell 对于错误消息的处理是跟普通输出分开的。如果创建了在后台模式下运行的 shell 脚本,通常必须依赖发送到日志文件来输出消息。用这种方法,
如果出现了错误信息,这些信息是不会出现在日志文件中的。需要换种方法来处理。


■ STDERR
-----------------------------------------------------------------------------------------------------------------------------------------
shell 通过特殊的 STDERR 文件描述符来处理错误消息。STDERR 文件描述符代表 shell 的标准错误输出。shell 或 shell 中运行的程序和脚本出错时生成
的错误消息都会发送到这个位置。

默认情况下,STDERR 文件描述符会和 STDOUT 文件描述符指向同样的地方(尽管分配给它们的文件描述符值不同)。也就是说,默认情况下,错误消息也会
输出到显示器输出中。

但从上面的例子可以看出,STDERR 并不会随着 STDOUT 的重定向而发生改变。使用脚本时,常常会想改变这种行为,尤其是当希望将错误消息保存到日志文
件中的时候。


5.1.2 重定向错误
-----------------------------------------------------------------------------------------------------------------------------------------
已经知道如何用重定向符号来重定向 STDOUT 数据。重定向 STDERR 数据也没太大差别,只要在使用重定向符号时定义 STDERR 文件描述符就可以了。有几种
实现方法。


■ 只重定向错误
-----------------------------------------------------------------------------------------------------------------------------------------
STDERR 文件描述符被设为 2。可以选择只重定向错误消息,将该文件描述符值放在重定向符号前。该值必须紧紧地放在重定向符号前,否则不会工作。

示例:
    [devalone@devalone 15]$ ls -al badfile 2> test4
    [devalone@devalone 15]$ cat test4
    ls: 无法访问'badfile': No such file or directory

现在运行该命令,错误消息不会出现在屏幕上了。该命令生成的任何错误消息都会保存在输出文件中。用这种方法,shell 会只重定向错误消息,而非普通
数据。

将 STDOUT 和 STDERR 消息混杂在同一输出中的例子:

    [devalone@devalone 15]$ ls -al test badtest test2 2> test5
    -rw-rw-r--. 1 devalone devalone 1035 7月   8 11:18 test2
    [devalone@devalone 15]$ cat test5
    ls: 无法访问'test': No such file or directory
    ls: 无法访问'badtest': No such file or directory

ls 命令的正常 STDOUT 输出仍然会发送到默认的 STDOUT 文件描述符,也就是显示器。由于该命令将文件描述符 2 的输出(STDERR)重定向到了一个输出
文件,shell 会将生成的所有错误消息直接发送到指定的重定向文件中。


■ 重定向错误和数据
-----------------------------------------------------------------------------------------------------------------------------------------
如果想重定向错误和正常输出,必须用两个重定向符号。需要在符号前面放上待重定向数据所对应的文件描述符,然后指向用于保存数据的输出文件。

示例:
    [devalone@devalone 15]$ ls -al test test2 test3 badtest 2> test6 1> test7
    [devalone@devalone 15]$ cat test6
    ls: 无法访问'test': No such file or directory
    ls: 无法访问'badtest': No such file or directory
    [devalone@devalone 15]$ cat test7
    -rw-rw-r--. 1 devalone devalone 1035 7月   8 11:18 test2
    -rw-rw-r--. 1 devalone devalone    0 7月   8 11:21 test3

shell利用 1> 符号将ls命令的正常输出重定向到了 test7 文件,而这些输出本该是进入 STDOUT 的。所有本该输出到 STDERR 的错误消息通过 2> 符号被
重定向到了 test6 文件。

可以用这种方法将脚本的正常输出和脚本生成的错误消息分离开来。这样就可以轻松地识别出错误信息,再不用在成千上万行正常输出数据中翻腾了。

另外,也可以将 STDERR 和 STDOUT 的输出重定向到同一个输出文件。为此 bash shell 提供了特殊的重定向符号 &> 。

示例:
    [devalone@devalone 15]$ ls -al test test2 test3 badtest &> test7
    [devalone@devalone 15]$ cat test7
    ls: 无法访问'test': No such file or directory
    ls: 无法访问'badtest': No such file or directory
    -rw-rw-r--. 1 devalone devalone 1035 7月   8 11:18 test2
    -rw-rw-r--. 1 devalone devalone    0 7月   8 11:21 test3

当使用 &> 符号时,命令生成的所有输出都会发送到同一位置,包括数据和错误。其中有一条错误消息出现的位置和预想中的不一样。badtest 文件(列出的
最后一个文件)的这条错误消息出现在输出文件中的第二行。

为了避免错误信息散落在输出文件中,相较于标准输出,bash shell 自动赋予了错误消息更高的优先级。这样能够集中浏览错误信息。

5.2 在脚本中重定向输出
-----------------------------------------------------------------------------------------------------------------------------------------
可以在脚本中用 STDOUT 和 STDERR 文件描述符在多个位置生成输出,只要简单地重定向相应的文件描述符就行了。有两种方法来在脚本中重定向输出:

    □ 临时重定向行输出
    □ 永久重定向脚本中的所有命令

5.2.1 临时重定向
-----------------------------------------------------------------------------------------------------------------------------------------
如果有意在脚本中生成错误消息,可以将单独的一行输出重定向到 STDERR。所需要做的是使用输出重定向符来将输出信息重定向到 STDERR 文件描述符。
在重定向到文件描述符时,必须在文件描述符数字之前加一个 &:

示例:

    echo "This is an error message" >&2

这行会在脚本的 STDERR 文件描述符所指向的位置显示文本,而不是通常的 STDOUT。

示例:
    [devalone@devalone 15]$ cat test8.sh
    #!/bin/bash
    # testing STDERR messages

    echo "This is an error" >&2
    echo "This is normal output"

如果像平常一样运行这个脚本,可能看不出什么区别:
运行:
    [devalone@devalone 15]$ ./test8.sh
    This is an error
    This is normal output

记住,默认情况下,Linux 会将 STDERR 导向 STDOUT。但是,如果在运行脚本时重定向了 STDERR,脚本中所有导向 STDERR 的文本都会被重定向。

运行:
    [devalone@devalone 15]$ ./test8.sh 2> test9
    This is normal output
    [devalone@devalone 15]$ cat test9
    This is an error

通过 STDOUT 显示的文本显示在了屏幕上,而发送给 STDERR 的 echo 语句的文本则被重定向到了输出文件。

这个方法非常适合在脚本中生成错误消息。如果有人用了的脚本,他们可以像上面的例子中那样轻松地通过 STDERR 文件描述符重定向错误消息。


5.2.2 永久重定向
-----------------------------------------------------------------------------------------------------------------------------------------
如果脚本中有大量数据需要重定向,那重定向每个 echo 语句就会很烦琐。取而代之,可以用 exec 命令告诉 shell 在脚本执行期间重定向某个特定文件
描述符。

示例:
    [devalone@devalone 15]$ cat test10.sh
    #!/bin/bash
    # redirecting all output to a file

    exec 1>testout

    echo "This is a test of redirecting all output"
    echo "from a script to another file."
    echo "without having to redirect every individual line"

运行:
    [devalone@devalone 15]$ ./test10.sh
    [devalone@devalone 15]$ cat testout
    This is a test of redirecting all output
    from a script to another file.
    without having to redirect every individual line

exec 命令会启动一个新 shell 并将 STDOUT 文件描述符重定向到文件。脚本中发给 STDOUT 的所有输出会被重定向到文件。

也可以在脚本执行过程中重定向 STDOUT:

    [devalone@devalone 15]$ cat test11.sh
    #!/bin/bash
    # redirecting output to different locations

    exec 2>testerror

    echo "This is the start of the script"
    echo "now redirecting all output to another location"

    exec 1>testout

    echo "This output should go to the testout file"
    echo "but this should go to the testerror file" >&2

运行:
    [devalone@devalone 15]$ test11.sh
    This is the start of the script
    now redirecting all output to another location

    [devalone@devalone 15]$ cat testout
    This output should go to the testout file
    [devalone@devalone 15]$ cat testerror
    but this should go to the testerror file

这个脚本用 exec 命令来将发给 STDERR 的输出重定向到文件 testerror。接下来,脚本用 echo 语句向 STDOUT 显示了几行文本。随后再次使用 exec
命令来将 STDOUT 重定向到 testout 文件。注意,尽管 STDOUT 被重定向了,但仍然可以将 echo 语句的输出发给 STDERR,在本例中还是重定向到
testerror 文件。

当只想将脚本的部分输出重定向到其他位置时(如错误日志),这个特性用起来非常方便。


5.3 在脚本中重定向输入
-----------------------------------------------------------------------------------------------------------------------------------------
可以使用与脚本中重定向 STDOUT 和 STDERR 相同的方法来将 STDIN 从键盘重定向到其他位置。exec 命令允许将 STDIN 重定向到Linux系统上的文件中:

    exec 0< testfile
    
这个命令会告诉 shell 它应该从文件 testfile 中获得输入,而不是 STDIN。这个重定向只要在脚本需要输入时就会作用。

示例:

    [devalone@devalone 15]$ cat test12.sh
    #!/bin/bash
    #
    # redirecting file input
    #

    exec 0< testfile
    count=1

    while read line
    do
        echo "Lin #$count: $line"
        count=$[ $count + 1 ]
    done

运行:
    [devalone@devalone 15]$ test12.sh
    Lin #1: This is the first line.
    Lin #2: This is the second line.
    Lin #3: This is the third line.

将 STDIN 重定向到文件后,当 read 命令试图从 STDIN 读入数据时,它会到文件去取数据,而不是键盘。

这是在脚本中从待处理的文件中读取数据的绝妙办法。Linux 系统管理员的一项日常任务就是从日志文件中读取数据并处理。这是完成该任务最简单的办法。


5.4 创建自己的重定向
-----------------------------------------------------------------------------------------------------------------------------------------
在脚本中重定向输入和输出时,并不局限于这 3 个默认的文件描述符。曾提到过,在 shell 中最多可以有 9 个打开的文件描述符。其他 6 个从 3~8 的文
件描述符均可用作输入或输出重定向。可以将这些文件描述符中的任意一个分配给文件,然后在脚本中使用它们。


5.4.1 创建输出文件描述符
-----------------------------------------------------------------------------------------------------------------------------------------
可以用 exec命令来给输出分配文件描述符。和标准的文件描述符一样,一旦将另一个文件描述符分配给一个文件,这个重定向就会一直有效,直到重新分配。

示例:
    [devalone@devalone 15]$ cat test13.sh
    #!/bin/bash
    # using an alternative file descriptor

    exec 3> test13out

    echo "This should display on the monitor"
    echo "and this should be stored in the file" >&3
    echo "Then this should be back on the monitor"

运行:
    [devalone@devalone 15]$ test13.sh
    This should display on the monitor
    Then this should be back on the monitor

    [devalone@devalone 15]$ cat test13out
    and this should be stored in the file

这个脚本用 exec 命令将文件描述符 3 重定向到另一个文件。当脚本执行 echo 语句时,输出内容会像预想中那样显示在 STDOUT上。但重定向到文件
描述符 3 的那行 echo 语句的输出却进入了另一个文件。这样就可以在显示器上保持正常的输出,而将特定信息重定向到文件中(比如日志文件)。

也可以不用创建新文件,而是使用 exec 命令来将输出追加到现有文件中。

    exec 3>>test13out

现在输出会被追加到 test13out 文件,而不是创建一个新文件。


5.4.2 重定向文件描述符
-----------------------------------------------------------------------------------------------------------------------------------------
现在介绍怎么恢复已重定向的文件描述符。可以分配另外一个文件描述符给标准文件描述符,反之亦然。这意味着可以将 STDOUT 的原来位置重定向到另一个
文件描述符,然后再利用该文件描述符重定向回 STDOUT。

示例:
    [devalone@devalone 15]$ cat test14.sh
    #!/bin/bash
    # storing STDOUT, then coming back to it

    exec 3>&1
    exec 1>test14out

    echo "This should store in the output file"
    echo "alone with this line"

    exec 1>&3

    echo "Now things should be back to normal"

运行:
    [devalone@devalone 15]$ test14.sh
    Now things should be back to normal

    [devalone@devalone 15]$ cat test14out
    This should store in the output file
    alone with this line

首先,脚本将文件描述符 3 重定向到文件描述符 1 的当前位置,也就是 STDOUT。这意味着任何发送给文件描述符 3 的输出都将出现在显示器上。

第二个 exec 命令将 STDOUT 重定向到文件,shell 现在会将发送给 STDOUT的输出直接重定向到输出文件中。但是,文件描述符 3 仍然指向 STDOUT 原来
的位置,也就是显示器。如果此时将输出数据发送给文件描述符 3,它仍然会出现在显示器上,尽管 STDOUT 已经被重定向了。

在向 STDOUT(现在指向一个文件)发送一些输出之后,脚本将 STDOUT 重定向到文件描述符 3 的当前位置(现在仍然是显示器)。这意味着现在 STDOUT又
指向了它原来的位置:显示器。

这个方法可能有点叫人困惑,但这是一种在脚本中临时重定向输出,然后恢复默认输出设置的常用方法。


5.4.3 创建输入文件描述符
-----------------------------------------------------------------------------------------------------------------------------------------
可以用和重定向输出文件描述符同样的办法重定向输入文件描述符。在重定向到文件之前,先将 STDIN 文件描述符保存到另外一个文件描述符,然后在读取
完文件之后再将 STDIN 恢复到它原来的位置。

示例:
    [devalone@devalone 15]$ cat test15.sh
    #!/bin/bash
    # redirecting input file descriptors

    exec 6<&0
    exec 0< testfile

    count=1
    while read line
    do
        echo "Line #$count: $line"
        count=$[ $count + 1 ]
    done

    exec 0<&6

    read -p "Are you done now? " answer
    case $answer in
        Y|y) echo "Goodby";;
        N|n) echo "Sorry, this is the end.";;
    esac

运行:
    [devalone@devalone 15]$ test15.sh
    Line #1: This is the first line.
    Line #2: This is the second line.
    Line #3: This is the third line.
    Are you done now? y
    Goodby

在这个例子中,文件描述符 6 用来保存 STDIN 的位置。然后脚本将 STDIN 重定向到一个文件。read 命令的所有输入都来自重定向后的 STDIN(也就是
输入文件)。

在读取了所有行之后,脚本会将 STDIN 重定向到文件描述符6,从而将 STDIN 恢复到原先的位置。该脚本用了另外一个 read 命令来测试 STDIN是否恢复
正常了。这次它会等待键盘的输入。


5.4.4 创建读写文件描述符
-----------------------------------------------------------------------------------------------------------------------------------------
可以打开单个文件描述符来作为输入和输出。可以用同一个文件描述符对同一个文件进行读写。

不过用这种方法时,要特别小心。由于是对同一个文件进行数据读写,shell 会维护一个内部指针,指明在文件中的当前位置。任何读或写都会从文件指针
上次的位置开始。如果不够小心,它会产生一些令人困惑的结果。

示例:
    [devalone@devalone 15]$ cat test16.sh
    #!/bin/bash
    # testing input/output file descriptor

    exec 3<> testfile

    read line <&3
    echo "Read: $line"
    echo "This is a test line" >&3
    
运行:
    [devalone@devalone 15]$ cat testfile
    This is the first line.
    This is the second line.
    This is the third line.
    
    [devalone@devalone 15]$ test16.sh
    Read: This is the first line.
    
    [devalone@devalone 15]$ cat testfile
    This is the first line.
    This is a test line
    ine.
    This is the third line.

这个例子用了 exec 命令将文件描述符 3 分配给文件 testfile 以进行文件读写。接下来,它通过分配好的文件描述符,使用 read 命令读取文件中的
第一行,然后将这一行显示在 STDOUT 上。最后,它用 echo 语句将一行数据写入由同一个文件描述符打开的文件中。

在运行脚本时,一开始还算正常。输出内容表明脚本读取了 testfile 文件中的第一行。但如果在脚本运行完毕后,查看 testfile 文件内容的话,会发现
写入文件中的数据覆盖了已有的数据。

当脚本向文件中写入数据时,它会从文件指针所处的位置开始。read 命令读取了第一行数据,所以它使得文件指针指向了第二行数据的第一个字符。在 echo
语句将数据输出到文件时,它会将数据放在文件指针的当前位置,覆盖了该位置的已有数据。


5.4.5 关闭文件描述符
-----------------------------------------------------------------------------------------------------------------------------------------
如果创建了新的输入或输出文件描述符,shell 会在脚本退出时自动关闭它们。然而在有些情况下,需要在脚本结束前手动关闭文件描述符。

要关闭文件描述符,将它重定向到特殊符号 &- 。脚本中看起来如下:

    exec 3>&-

该语句会关闭文件描述符 3,不再在脚本中使用它。

示例:
    [devalone@devalone 15]$ cat badtest.sh
    #!/bin/bash
    # testing closing file descriptors
    exec 3> test17file
    echo "This is a test line of data" >&3
    exec 3>&-
    echo "This won't work" >&3

运行:
    [devalone@devalone 15]$ badtest.sh
    ./badtest.sh:行6: 3: Bad file descriptor

一旦关闭了文件描述符,就不能在脚本中向它写入任何数据,否则 shell 会生成错误消息。

在关闭文件描述符时还要注意另一件事。如果随后在脚本中打开了同一个输出文件,shell 会用一个新文件来替换已有文件。这意味着如果输出数据,它就
会覆盖已有文件。

    [devalone@devalone 15]$ cat test17.sh
    #!/bin/bash
    # testing closing file descriptors

    exec 3> test17file
    echo "This is a test line of data" >&3
    exec 3>&-

    cat test17file

    exec 3> test17file
    echo "This'll be bad" >&3

运行:
    [devalone@devalone 15]$ test17.sh
    This is a test line of data
    [devalone@devalone 15]$ cat test17file
    This'll be bad

在向 test17file 文件发送一个数据字符串并关闭该文件描述符之后,脚本用了 cat 命令来显示文件的内容。到目前为止,一切都还好。下一步,脚本重新
打开了该输出文件并向它发送了另一个数据字符串。当显示该输出文件的内容时,所能看到的只有第二个数据字符串。shell 覆盖了原来的输出文件。


5.5 列出打开的文件描述符
-----------------------------------------------------------------------------------------------------------------------------------------
能用的文件描述符只有 9 个,有时要记住哪个文件描述符被重定向到了哪里很难。为了帮助理清条理,bash shell 提供了 lsof 命令。

lsof 命令会列出整个 Linux 系统打开的所有文件描述符。这是个有争议的功能,因为它会向非系统管理员用户提供 Linux 系统的信息。鉴于此,许多Linux
系统隐藏了该命令,这样用户就不会一不小心就发现了。

在很多 Linux系统中(如Fedora),要想以普通用户账户来运行它,必须通过全路径名来引用:

    [devalone@devalone 15]$ which lsof
    /usr/bin/lsof

该命令会产生大量的输出。它会显示当前 Linux 系统上打开的每个文件的有关信息。这包括后台运行的所有进程以及登录到系统的任何用户。

有大量的命令行选项和参数可以用来帮助过滤 lsof 的输出。最常用的有 -p 和 -d,前者允许指定进程ID(PID),后者允许指定要显示的文件描述符编号。
要想知道进程的当前PID,可以用特殊环境变量$$(shell会将它设为当前PID)。-a 选项用来对其他两个选项的结果执行布尔AND运算,这会产生如下输出:

    [devalone@devalone 15]$ lsof -a -p $$ -d 0,1,2
    COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    bash    2291 devalone    0u   CHR  136,0      0t0    3 /dev/pts/0
    bash    2291 devalone    1u   CHR  136,0      0t0    3 /dev/pts/0
    bash    2291 devalone    2u   CHR  136,0      0t0    3 /dev/pts/0

上例显示了当前进程(bash shell)的默认文件描述符(0、1 和 2)。lsof 的默认输出中有 7 列信息,如下表所示:


    lsof 的默认输出
    +-----------+---------------------------------------------------------------------------------------
    | 列        | 描 述
    +-----------+---------------------------------------------------------------------------------------
    | COMMAND    | 正在运行的命令名的前9个字符
    +-----------+---------------------------------------------------------------------------------------
    | PID        | 进程的PID
    +-----------+---------------------------------------------------------------------------------------
    | USER        | 进程属主的登录名
    +-----------+---------------------------------------------------------------------------------------
    | FD        | 文件描述符号以及访问类型(r代表读,w代表写,u代表读写)
    +-----------+---------------------------------------------------------------------------------------
    | TYPE        | 文件的类型(CHR代表字符型,BLK代表块型,DIR代表目录,REG代表常规文件)
    +-----------+---------------------------------------------------------------------------------------
    | DEVICE    | 设备的设备号(主设备号和从设备号)
    +-----------+---------------------------------------------------------------------------------------
    | SIZE        | 如果有的话,表示文件的大小
    +-----------+---------------------------------------------------------------------------------------
    | NODE        | 本地文件的节点号
    +-----------+---------------------------------------------------------------------------------------
    | NAME        | 文件名
    +-----------+---------------------------------------------------------------------------------------


与 STDIN、STDOUT 和 STDERR 关联的文件类型是字符型。因为 STDIN、STDOUT 和 STDERR文件描述符都指向终端,所以输出文件的名称就是终端的设备名。
所有 3 种标准文件都支持读和写(尽管向 STDIN 写数据以及从 STDOUT 读数据看起来有点奇怪)。

看一下在打开了多个替代性文件描述符的脚本中使用 lsof 命令的结果:

    [devalone@devalone 15]$ cat test18.sh
    #!/bin/bash
    # testing lsof with file descripters

    exec 3> test18file1
    exec 6> test18file2
    exec 7< testfile

    lsof -a -p $$ -d 0,1,2,3,6,7

运行:
    [devalone@devalone 15]$ test18.sh
    COMMAND    PID     USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
    test18.sh 2476 devalone    0u   CHR  136,0      0t0       3 /dev/pts/0
    test18.sh 2476 devalone    1u   CHR  136,0      0t0       3 /dev/pts/0
    test18.sh 2476 devalone    2u   CHR  136,0      0t0       3 /dev/pts/0
    test18.sh 2476 devalone    3w   REG  253,2        0 2497622 /home/devalone/study/shell-script/15/test18file1
    test18.sh 2476 devalone    6w   REG  253,2        0 2497623 /home/devalone/study/shell-script/15/test18file2
    test18.sh 2476 devalone    7r   REG  253,2       74 2497619 /home/devalone/study/shell-script/15/testfile
    [devalone@devalone 15]$

该脚本创建了 3 个替代性文件描述符,两个作为输出(3和6),一个作为输入(7)。在脚本运行 lsof 命令时,可以在输出中看到新的文件描述符。文件名
显示了文件描述符所使用的文件的完整路径名。它将每个文件都显示成 REG 类型的,这说明它们是文件系统中的常规文件。


5.6 阻止命令输出
-----------------------------------------------------------------------------------------------------------------------------------------
有时候,可能不想显示脚本的输出。这在将脚本作为后台进程运行时很常见。如果在运行在后台的脚本出现错误消息,shell 会通过电子邮件将它们发给进程
的属主。这会很麻烦,尤其是当运行会生成很多烦琐的小错误的脚本时。

要解决这个问题,可以将 STDERR 重定向到一个叫作 null 文件的特殊文件。null 文件跟它的名字很像,文件里什么都没有。shell 输出到 null 文件的任
何数据都不会保存,全部都被丢掉。

在 Linux 系统上 null 文件的标准位置是 /dev/null。重定向到该位置的任何数据都会被丢掉,不会显示。

示例:
    [devalone@devalone 15]$ ll > /dev/null
    [devalone@devalone 15]$ cat /dev/null
    [devalone@devalone 15]$

这是避免出现错误消息,也无需保存它们的一个常用方法:
    
    [devalone@devalone 15]$ ls -al badfile test16 2> /dev/null
    -rw-rw-r--. 1 devalone devalone 43 7月   8 13:27 test16

也可以在输出重定向中将 /dev/null 作为输入文件。由于 /dev/null 文件不含有任何内容,通常用它来快速清除现有文件中的数据,而不用先删除文件再
重新创建:

    [devalone@devalone 15]$ cat testfile
    This is the first line.
    This is a test line
    ine.
    This is the third line.
    [devalone@devalone 15]$ cat /dev/null > testfile
    [devalone@devalone 15]$ cat testfile
    [devalone@devalone 15]$

文件 testfile 仍然存在系统上,但现在它是空文件。这是清除日志文件的一个常用方法,因为日志文件必须时刻准备等待应用程序操作。


5.7 创建临时文件
-----------------------------------------------------------------------------------------------------------------------------------------
Linux 系统有特殊的目录,专供临时文件使用。Linux 使用 /tmp 目录来存放不需要永久保留的文件。大多数 Linux 发行版配置了系统在启动时自动删除
/tmp 目录的所有文件。

系统上的任何用户账户都有权限在读写 /tmp 目录中的文件。这个特性为用户提供了一种创建临时文件的简单方法,而且还不用操心清理工作。

有个特殊命令可以用来创建临时文件。 mktemp 命令可以在 /tmp 目录中创建一个唯一的临时文件。shell 会创建这个文件,但不用默认的 umask 值。它会
将文件的读和写权限分配给文件的属主,并将创建用户设成文件的属主。一旦创建了文件,创建用户就在脚本中有了完整的读写权限,但其他人没法访问它
(当然,root用户除外)。


5.7.1 创建本地临时文件
-----------------------------------------------------------------------------------------------------------------------------------------
默认情况下,mktemp 会在本地目录中创建一个文件。要用 mktemp 命令在本地目录中创建一个临时文件,只要指定一个文件名模板就行了。模板可以包含
任意文本文件名,在文件名末尾加上 6 个X就行了。

    [devalone@devalone 15]$ mktemp testing.XXXXXX
    testing.846nnk

mktemp 命令会用 6 个字符码替换这 6 个X,从而保证文件名在目录中是唯一的。可以创建多个临时文件,它可以保证每个文件都是唯一的。

    [devalone@devalone 15]$ mktemp testing.XXXXXX
    testing.5KFsRw
    [devalone@devalone 15]$ mktemp testing.XXXXXX
    testing.jeVY9y
    [devalone@devalone 15]$ mktemp testing.XXXXXX
    testing.sBokD1

    [devalone@devalone 15]$ ll testing.*
    -rw-------. 1 devalone devalone 0 7月   8 14:31 testing.5KFsRw
    -rw-------. 1 devalone devalone 0 7月   8 14:30 testing.846nnk
    -rw-------. 1 devalone devalone 0 7月   8 14:31 testing.jeVY9y
    -rw-------. 1 devalone devalone 0 7月   8 14:31 testing.sBokD1

mktemp 命令的输出正是它所创建的文件的名字。在脚本中使用 mktemp 命令时,可能要将文件名保存到变量中,这样就能在后面的脚本中引用了。

示例:
    [devalone@devalone 15]$ cat test19.sh
    #!/bin/bash
    # creating and using a temp file

    tempfile=$(mktemp test19.XXXXXX)

    exec 3>$tempfile

    echo "This script writes to temp file $tempfile"

    echo "This is the first line" >&3
    echo "This is the second line" >&3
    echo "This is the last line." >&3
    exec 3>&-

    echo "Done creating temp file. The content are:"
    cat $tempfile
    rm -f $tempfile 2> /dev/null

运行:
    [devalone@devalone 15]$ test19.sh
    This script writes to temp file test19.VnZtPM
    Done creating temp file. The content are:
    This is the first line
    This is the second line
    This is the last line.

脚本用 mktemp 命令来创建临时文件并将文件名赋给 $tempfile 变量。接着将这个临时文件作为文件描述符 3 的输出重定向文件。在将临时文件名显示在
STDOUT 之后,向临时文件中写入了几行文本,然后关闭了文件描述符。最后,显示出临时文件的内容,并用 rm 命令将其删除。


5.7.2 在/tmp 目录创建临时文件
-----------------------------------------------------------------------------------------------------------------------------------------
-t 选项会强制 mktemp 命令来在系统的临时目录来创建该文件。在用这个特性时,mktemp 命令会返回用来创建临时文件的全路径,而不是只有文件名。

示例:
    [devalone@devalone 15]$ mktemp -t test.XXXXXX
    /tmp/test.8kIS22
    [devalone@devalone 15]$ ll -a /tmp/test*
    -rw-------. 1 devalone devalone 0 7月   8 14:38 /tmp/test.8kIS22

由于 mktemp 命令返回了全路径名,可以在 Linux 系统上的任何目录下引用该临时文件,不管工作目录在哪里。

示例:
    [devalone@devalone 15]$ cat test20.sh
    #!/bin/bash
    # creating a temp file in /tmp

    tempfile=$(mktemp -t tmp.XXXXXX)

    echo "This is a test file." >$tempfile
    echo "This is the second line of the test." >>$tempfile

    echo "The temp file is located at: $tempfile"

    cat $tempfile

    rm -f $tempfile

运行:
    [devalone@devalone 15]$ test20.sh
    The temp file is located at: /tmp/tmp.5IljPD
    This is a test file.
    This is the second line of the test.


5.7.3 创建临时目录
-----------------------------------------------------------------------------------------------------------------------------------------
-d 选项告诉 mktemp 命令来创建一个临时目录而不是临时文件。这样就能用该目录进行任何需要的操作了,比如创建其他的临时文件。

示例:
    [devalone@devalone 15]$ cat test21.sh
    #!/bin/bash
    # using a temporary directory

    tempdir=$(mktemp -d dir.XXXXXX)
    cd $tempdir
    tempfile1=$(mktemp temp.XXXXXX)
    tempfile2=$(mktemp temp.XXXXXXXX)

    exec 7> $tempfile1
    exec 8> $tempfile2

    echo "Sending data to directory $tempdir"
    echo "This is a test line of data for $tempfile1" >&7
    echo "This is a test line of data for $tempfile2" >&8

运行:
    [devalone@devalone 15]$ test21.sh
    Sending data to directory dir.lk5PBB

    [devalone@devalone 15]$ cd dir.lk5PBB
    [devalone@devalone dir.lk5PBB]$ ll
    总用量 8
    -rw-------. 1 devalone devalone 44 7月   8 14:44 temp.msMokc
    -rw-------. 1 devalone devalone 46 7月   8 14:44 temp.NKKmb6Tf

    [devalone@devalone dir.lk5PBB]$ cat temp.msMokc
    This is a test line of data for temp.msMokc

    [devalone@devalone dir.lk5PBB]$ cat temp.NKKmb6Tf
    This is a test line of data for temp.NKKmb6Tf

这段脚本在当前目录创建了一个目录,然后它用 cd 命令进入该目录,并创建了两个临时文件。之后这两个临时文件被分配给文件描述符,用来存储脚本的
输出。


5.8 记录消息
-----------------------------------------------------------------------------------------------------------------------------------------
将输出同时发送到显示器和日志文件,这种做法有时候能够派上用场。不用将输出重定向两次,只要用特殊的 tee 命令就行。

tee 命令相当于管道的一个 T 型接头。它将从 STDIN 过来的数据同时发往两处。一处是 STDOUT,另一处是 tee 命令行所指定的文件名:

    tee filename

由于 tee 会重定向来自 STDIN 的数据,可以用它配合管道命令来重定向命令输出。

示例:
    [devalone@devalone 15]$ date | tee testfile
    2018年 07月 08日 星期日 14:53:25 CST
    [devalone@devalone 15]$ cat testfile
    2018年 07月 08日 星期日 14:53:25 CST

输出出现在了 STDOUT 中,同时也写入了指定的文件中。注意,默认情况下,tee命令会在每次使用时覆盖输出文件内容。

示例:
    [devalone@devalone 15]$ who | tee testfile
    devalone pts/0        2018-07-08 12:12 (192.168.1.101)
    [devalone@devalone 15]$ cat testfile
    devalone pts/0        2018-07-08 12:12 (192.168.1.101)

如果想将数据追加到文件中,必须用 -a 选项。

示例:
    [devalone@devalone 15]$ date | tee -a testfile
    2018年 07月 08日 星期日 14:56:46 CST
    
    [devalone@devalone 15]$ cat testfile
    devalone pts/0        2018-07-08 12:12 (192.168.1.101)
    2018年 07月 08日 星期日 14:56:46 CST

利用这个方法,既能将数据保存在文件中,也能将数据显示在屏幕上。

示例:
    [devalone@devalone 15]$ cat test22.sh
    #!/bin/bash
    # using the tee command for logging

    tempfile=test22file

    echo "This is the start of the test" | tee $tempfile
    echo "This is the second line of the test" | tee -a $tempfile
    echo "This is the end of the test" | tee -a $tempfile

运行:
    [devalone@devalone 15]$ test22.sh
    This is the start of the test
    This is the second line of the test
    This is the end of the test

    [devalone@devalone 15]$ cat test22file
    This is the start of the test
    This is the second line of the test
    This is the end of the test

5.9 实例
-----------------------------------------------------------------------------------------------------------------------------------------
文件重定向常见于脚本需要读入文件和输出文件时。这个示例脚本两件事都做了。它读取 .csv 格式的数据文件,输出 SQL INSERT 语句来将数据插入数据库

shell 脚本使用命令行参数指定待读取的 .csv文件。.csv格式用于从电子表格中导出数据,所以可以把数据库数据放入电子表格中,把电子表格保存成.csv
格式,读取文件,然后创建 INSERT 语句将数据插入 MySQL 数据库。

代码:
    [devalone@devalone 15]$ cat test23.sh
    #!/bin/bash
    # read file and create INSERT statements for MySQL

    outfile='members.sql'
    IFS=','

    while read lname fname address city state zip
    do
        cat >> $outfile << EOF
        INSERT INTO members (lname,fname,address,city,state,zip) VALUES
        ('$lname', '$fname', '$address', '$city', '$state', '$zip');
    EOF
    done < ${1}

这个脚本很短小,这都要感谢有了文件重定向!脚本中出现了三处重定向操作。while 循环使用 read语句从数据文件中读取文本。注意在 done 语句中出现
的重定向符号:

    done < ${1}

当运行程序 test23.sh 时,$1 代表第一个命令行参数。它指明了待读取数据的文件。read 语句会使用 IFS 字符解析读入的文本,我们在这里将 IFS 指定
为逗号。

脚本中另外两处重定向操作出现在同一条语句中:

    cat >> $outfile << EOF

这条语句中包含一个输出追加重定向(双大于号)和一个输入追加重定向(双小于号)。输出重定向将 cat 命令的输出追加到由 $outfile 变量指定的文件
中。cat 命令的输入不再取自标准输入,而是被重定向到脚本中存储的数据。EOF 符号标记了追加到文件中的数据的起止。

    INSERT INTO members (lname,fname,address,city,state,zip) VALUES
    ('$lname', '$fname', '$address', '$city', '$state', '$zip');

上面的文本生成了一个标准的 SQL INSERT语句。注意,其中的数据会由变量来替换,变量中内容则是由 read 语句存入的。

所以基本上 while 循环一次读取一行数据,将这些值放入 INSERT 语句模板中,然后将结果输出到输出文件中。

使用以下输入数据文件:

    [devalone@devalone 15]$ cat members.csv
    Blum,Richard,123 Main St.,Chicago,IL,60601
    Blum,Barbara,123 Main St.,Chicago,IL,60601
    Bresnahan,Christine,456 Oak Ave.,Columbus,OH,43201
    Bresnahan,Timothy,456 Oak Ave.,Columbus,OH,43201

运行脚本时,显示器上不会出现任何输出:

    [devalone@devalone 15]$ test23.sh members.csv
    [devalone@devalone 15]$

但是在members.sql输出文件中,会看到如下输出内容:

    [devalone@devalone 15]$ cat members.sql
    INSERT INTO members (lname,fname,address,city,state,zip) VALUES
    ('Blum', 'Richard', '123 Main St.', 'Chicago', 'IL', '60601');
    INSERT INTO members (lname,fname,address,city,state,zip) VALUES
    ('Blum', 'Barbara', '123 Main St.', 'Chicago', 'IL', '60601');
    INSERT INTO members (lname,fname,address,city,state,zip) VALUES
    ('Bresnahan', 'Christine', '456 Oak Ave.', 'Columbus', 'OH', '43201');
    INSERT INTO members (lname,fname,address,city,state,zip) VALUES
    ('Bresnahan', 'Timothy', '456 Oak Ave.', 'Columbus', 'OH', '43201 ');

现在可以将 members.sql 文件导入 MySQL 数据表中。

系列目录:

    Linux shell 脚本编程-基础篇 (一)

    Linux shell 脚本编程-基础篇 (二)

    Linux shell 脚本编程-基础篇 (三)

    Linux shell 脚本编程-基础篇 (四)

    Linux shell 脚本编程-基础篇 (五)

    Linux shell 脚本编程-基础篇 (六)

-----------------------------------------------------------------------------------------------------------------------------------------

参考:

    《Linux 命令行与 shell 脚本编程大全》 第 3 版 —— 2016.8(美)Richard Blum  Cristine Bresnahan

猜你喜欢

转载自blog.csdn.net/devalone/article/details/81327797