分享

exec command

 Lewis1234 2014-04-30
  An exec <filename command redirects stdin  to a file. From that point on, all stdin  comes from that file, rather than its normal source (usually keyboard input). This provides a method of reading a file line by line and possibly parsing each line of input using sed and/or awk.

Example 16-1. Redirecting stdin  using exec

   1 #!/bin/bash
   2 # Redirecting stdin using 'exec'.
   3
   4
   5 exec 6<&0          # Link file descriptor #6 with stdin.
   6                    # Saves stdin.
   7
   8 exec < data-file   # stdin replaced by file "data-file"
   9
  10 read a1            # Reads first line of file "data-file".
  11 read a2            # Reads second line of file "data-file."
  12
  13 echo
  14 echo "Following lines read from file."
  15 echo "-------------------------------"
  16 echo $a1
  17 echo $a2
  18
  19 echo; echo; echo
  20
  21 exec 0<&6 6<&-
  22 #  Now restore stdin from fd #6, where it had been saved,
  23 #+ and close fd #6 ( 6<&- ) to free it for other processes to use.
  24 #
  25 # <&6 6<&-    also works.
  26
  27 echo -n "Enter data  "
  28 read b1  # Now "read" functions as expected, reading from normal stdin.
  29 echo "Input read from stdin."
  30 echo "----------------------"
  31 echo "b1 = $b1"
  32
  33 echo
  34
  35 exit 0

Similarly, an exec >filename command redirects stdout  to a designated file. This sends all command output that would normally go to stdout  to that file.


Example 16-2. Redirecting stdout  using exec

   1 #!/bin/bash
   2 # reassign-stdout.sh
   3
   4 LOGFILE=logfile.txt
   5
   6 exec 6>&1           # Link file descriptor #6 with stdout.
   7                     # Saves stdout.
   8
   9 exec > $LOGFILE     # stdout replaced with file "logfile.txt".
  10
  11 # ----------------------------------------------------------- #
  12 # All output from commands in this block sent to file $LOGFILE.
  13
  14 echo -n "Logfile: "
  15 date
  16 echo "-------------------------------------"
  17 echo
  18
  19 echo "Output of \"ls -al\" command"
  20 echo
  21 ls -al
  22 echo; echo
  23 echo "Output of \"df\" command"
  24 echo
  25 df
  26
  27 # ----------------------------------------------------------- #
  28
  29 exec 1>&6 6>&-      # Restore stdout and close file descriptor #6.
  30
  31 echo
  32 echo "== stdout now restored to default == "
  33 echo
  34 ls -al
  35 echo
  36
  37 exit 0

Example 16-3. Redirecting both stdin  and stdout  in the same script with exec

   1 #!/bin/bash
   2 # upperconv.sh
   3 # Converts a specified input file to uppercase.
   4
   5 E_FILE_ACCESS=70
   6 E_WRONG_ARGS=71
   7
   8 if [ ! -r "$1" ]     # Is specified input file readable?
   9 then
  10   echo "Can't read from input file!"
  11   echo "Usage: $0 input-file output-file"
  12   exit $E_FILE_ACCESS
  13 fi                   #  Will exit with same error
  14                      #+ even if input file ($1) not specified.
  15
  16 if [ -z "$2" ]
  17 then
  18   echo "Need to specify output file."
  19   echo "Usage: $0 input-file output-file"
  20   exit $E_WRONG_ARGS
  21 fi
  22
  23
  24 exec 4<&0
  25 exec < $1            # Will read from input file.
  26
  27 exec 7>&1
  28 exec > $2            # Will write to output file.
  29                      # Assumes output file writable (add check?).
  30
  31 # -----------------------------------------------
  32     cat - | tr a-z A-Z   # Uppercase conversion.
  33 #   ^^^^^                # Reads from stdin.
  34 #           ^^^^^^^^^^   # Writes to stdout.
  35 # However, both stdin and stdout were redirected.
  36 # -----------------------------------------------
  37
  38 exec 1>&7 7>&-       # Restore stout.
  39 exec 0<&4 4<&-       # Restore stdin.
  40
  41 # After restoration, the following line prints to stdout as expected.
  42 echo "File \"$1\" written to \"$2\" as uppercase conversion."
  43
  44 exit 0

Notes

[1] A file descriptor is simply a number that the operating system assigns to an open file to keep track of it. Consider it a simplified version of a file pointer. It is analogous to a file handle in C.
[2] Using file descriptor 5  might cause problems. When Bash creates a child process, as with exec, the child inherits fd 5 (see Chet Ramey's archived e-mail, SUBJECT: RE: File descriptor 5 is held open). Best leave this particular fd alone.

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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多