亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 7198 | 回復(fù): 5
打印 上一主題 下一主題

請教 命令 exec 7<>$outputfile 是什么意思呢 ? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2008-05-06 17:34 |只看該作者 |倒序瀏覽
請教 命令 exec 7<>$outputfile  是什么意思呢 ?
今天在網(wǎng)上看到一個自動telnet登錄的shell腳本,其中就有一個命令是:  exec 7<>$outputfile

整個的腳本內(nèi)容如下:
#!/bin/sh

if (( $# != 1 ))
then
        echo "usage0 address "
        exit 1
fi


ip=$1
inp1=`cat param|grep "$ip" |awk '{ print $2 }'`
inp2=`cat param|grep "$ip" |awk '{ print $3 }'`
inp3=`cat param|grep "$ip" |awk '{ print $4 }'`

inputfile=in
outputfile=out

rm -rf $inputfile
rm -rf $outputfile

mknod $inputfile p
touch $outputfile

exec 7<>$outputfile
exec 8<>$inputfile

telnet $ip <&8 >&7 &

sleep 1; echo $inp1 >>$inputfile
sleep 1; echo $inp2 >>$inputfile
sleep 1; echo $inp3 >>$inputfile

tail -f $outputfile &

while true
do
        read str
        if [[ $str = "quit" || $str = "exit" ]]
        then echo $str >>$inputfile; exit
        else echo $str >>$inputfile
        fi
done

其中有個參數(shù)文件param,內(nèi)容如下:
#=====param============
192.168.226.11 root 1q2w3e4r

#=====================

論壇徽章:
0
2 [報告]
發(fā)表于 2008-05-07 15:41 |只看該作者
打開該文件,并賦予文件句柄 7.

論壇徽章:
23
15-16賽季CBA聯(lián)賽之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午馬
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16賽季CBA聯(lián)賽之山東
日期:2017-12-21 16:39:1915-16賽季CBA聯(lián)賽之廣東
日期:2016-01-19 13:33:372015亞冠之山東魯能
日期:2015-10-13 09:39:062015亞冠之西悉尼流浪者
日期:2015-09-21 08:27:57
3 [報告]
發(fā)表于 2008-05-07 15:45 |只看該作者
[j]<>filename
   # 為了讀寫"filename", 把文件"filename"打開, 并且分配文件描述符"j"給它.
   # 如果文件"filename"不存在, 那么就創(chuàng)建它.
   # 如果文件描述符"j"沒指定, 那默認(rèn)是fd 0, stdin.
   #
   # 這種應(yīng)用通常是為了寫到一個文件中指定的地方.
   echo 1234567890 > File    # 寫字符串到"File".
   exec 3<> File             # 打開"File"并且給它分配fd 3.
   read -n 4 <&3             # 只讀4個字符.
   echo -n . >&3             # 寫一個小數(shù)點(diǎn).
   exec 3>&-                 # 關(guān)閉fd 3.
   cat File                  # ==> 1234.67890

招聘 : Linux運(yùn)維
論壇徽章:
0
4 [報告]
發(fā)表于 2008-05-07 16:04 |只看該作者
原帖由 ly5066113 于 2008-5-7 15:45 發(fā)表
[j]filename
   # 為了讀寫"filename", 把文件"filename"打開, 并且分配文件描述符"j"給它.
   # 如果文件"filename"不存在, 那么就創(chuàng)建它.
   # 如果文件描述符"j"沒指定, 那默認(rèn)是fd 0, stdin.
   #
    ...

fd 的意思是啥啊
還有>&3

論壇徽章:
23
15-16賽季CBA聯(lián)賽之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午馬
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16賽季CBA聯(lián)賽之山東
日期:2017-12-21 16:39:1915-16賽季CBA聯(lián)賽之廣東
日期:2016-01-19 13:33:372015亞冠之山東魯能
日期:2015-10-13 09:39:062015亞冠之西悉尼流浪者
日期:2015-09-21 08:27:57
5 [報告]
發(fā)表于 2008-05-07 16:11 |只看該作者
原帖由 lil33 于 2008-5-7 16:04 發(fā)表

fd 的意思是啥啊
還有>&3


十三問:
http://www.72891.cn/viewthr ... p;page=7#pid1636825

論壇徽章:
0
6 [報告]
發(fā)表于 2008-05-07 17:15 |只看該作者
I meet the  question before
And I believe after read this you will understand

  1. Chapter 16. I/O Redirection

  2. There are always three default "files" open, stdin (the keyboard), stdout (the screen), and stderr (error messages output to the screen). These, and any other open files, can be redirected. Redirection simply means capturing output from a file, command, program, script, or even code block within a script (see Example 3-1 and Example 3-2) and sending it as input to another file, command, program, or script.

  3. Each open file gets assigned a file descriptor. [1] The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively. For opening additional files, there remain descriptors 3 to 9. It is sometimes useful to assign one of these additional file descriptors to stdin, stdout, or stderr as a temporary duplicate link. [2] This simplifies restoration to normal after complex redirection and reshuffling (see Example 16-1).

  4.    1    COMMAND_OUTPUT >
  5.    2       # Redirect stdout to a file.
  6.    3       # Creates the file if not present, otherwise overwrites it.
  7.    4
  8.    5       ls -lR > dir-tree.list
  9.    6       # Creates a file containing a listing of the directory tree.
  10.    7
  11.    8    : > filename
  12.    9       # The > truncates file "filename" to zero length.
  13.   10       # If file not present, creates zero-length file (same effect as 'touch').
  14.   11       # The : serves as a dummy placeholder, producing no output.
  15.   12
  16.   13    > filename   
  17.   14       # The > truncates file "filename" to zero length.
  18.   15       # If file not present, creates zero-length file (same effect as 'touch').
  19.   16       # (Same result as ": >", above, but this does not work with some shells.)
  20.   17
  21.   18    COMMAND_OUTPUT >>
  22.   19       # Redirect stdout to a file.
  23.   20       # Creates the file if not present, otherwise appends to it.
  24.   21
  25.   22
  26.   23       # Single-line redirection commands (affect only the line they are on):
  27.   24       # --------------------------------------------------------------------
  28.   25
  29.   26    1>filename
  30.   27       # Redirect stdout to file "filename".
  31.   28    1>>filename
  32.   29       # Redirect and append stdout to file "filename".
  33.   30    2>filename
  34.   31       # Redirect stderr to file "filename".
  35.   32    2>>filename
  36.   33       # Redirect and append stderr to file "filename".
  37.   34    &>filename
  38.   35       # Redirect both stdout and stderr to file "filename".
  39.   36
  40.   37       #==============================================================================
  41.   38       # Redirecting stdout, one line at a time.
  42.   39       LOGFILE=script.log
  43.   40
  44.   41       echo "This statement is sent to the log file, \"$LOGFILE\"." 1>$LOGFILE
  45.   42       echo "This statement is appended to \"$LOGFILE\"." 1>>$LOGFILE
  46.   43       echo "This statement is also appended to \"$LOGFILE\"." 1>>$LOGFILE
  47.   44       echo "This statement is echoed to stdout, and will not appear in \"$LOGFILE\"."
  48.   45       # These redirection commands automatically "reset" after each line.
  49.   46
  50.   47
  51.   48
  52.   49       # Redirecting stderr, one line at a time.
  53.   50       ERRORFILE=script.errors
  54.   51
  55.   52       bad_command1 2>$ERRORFILE       #  Error message sent to $ERRORFILE.
  56.   53       bad_command2 2>>$ERRORFILE      #  Error message appended to $ERRORFILE.
  57.   54       bad_command3                    #  Error message echoed to stderr,
  58.   55                                       #+ and does not appear in $ERRORFILE.
  59.   56       # These redirection commands also automatically "reset" after each line.
  60.   57       #==============================================================================
  61.   58
  62.   59
  63.   60
  64.   61    2>&1
  65.   62       # Redirects stderr to stdout.
  66.   63       # Error messages get sent to same place as standard output.
  67.   64
  68.   65    i>&j
  69.   66       # Redirects file descriptor i to j.
  70.   67       # All output of file pointed to by i gets sent to file pointed to by j.
  71.   68
  72.   69    >&j
  73.   70       # Redirects, by default, file descriptor 1 (stdout) to j.
  74.   71       # All stdout gets sent to file pointed to by j.
  75.   72
  76.   73    0< FILENAME
  77.   74     < FILENAME
  78.   75       # Accept input from a file.
  79.   76       # Companion command to ">", and often used in combination with it.
  80.   77       #
  81.   78       # grep search-word <filename
  82.   79
  83.   80
  84.   81    [j]<>filename
  85.   82       # Open file "filename" for reading and writing, and assign file descriptor "j" to it.
  86.   83       # If "filename" does not exist, create it.
  87.   84       # If file descriptor "j" is not specified, default to fd 0, stdin.
  88.   85       #
  89.   86       # An application of this is writing at a specified place in a file.
  90.   87       echo 1234567890 > File    # Write string to "File".
  91.   88       exec 3<> File             # Open "File" and assign fd 3 to it.
  92.   89       read -n 4 <&3             # Read only 4 characters.
  93.   90       echo -n . >&3             # Write a decimal point there.
  94.   91       exec 3>&-                 # Close fd 3.
  95.   92       cat File                  # ==> 1234.67890
  96.   93       # Random access, by golly.
  97.   94
  98.   95
  99.   96
  100.   97    |
  101.   98       # Pipe.
  102.   99       # General purpose process and command chaining tool.
  103. 100       # Similar to ">", but more general in effect.
  104. 101       # Useful for chaining commands, scripts, files, and programs together.
  105. 102       cat *.txt | sort | uniq > result-file
  106. 103       # Sorts the output of all the .txt files and deletes duplicate lines,
  107. 104       # finally saves results to "result-file".

  108. Multiple instances of input and output redirection and/or pipes can be combined in a single command line.

  109.    1 command < input-file > output-file
  110.    2
  111.    3 command1 | command2 | command3 > output-file

  112. See Example 12-28 and Example A-15.

  113. Multiple output streams may be redirected to one file.

  114.    1 ls -yz >> command.log 2>&1
  115.    2 #  Capture result of illegal options "yz" in file "command.log."
  116.    3 #  Because stderr is redirected to the file,
  117.    4 #+ any error messages will also be there.
  118.    5
  119.    6 #  Note, however, that the following does *not* give the same result.
  120.    7 ls -yz 2>&1 >> command.log
  121.    8 #  Outputs an error message and does not write to file.
  122.    9
  123.   10 #  If redirecting both stdout and stderr,
  124.   11 #+ the order of the commands makes a difference.

  125. Closing File Descriptors

  126. n<&-

  127.     Close input file descriptor n.
  128. 0<&-, <&-

  129.     Close stdin.
  130. n>&-

  131.     Close output file descriptor n.
  132. 1>&-, >&-

  133.     Close stdout.

  134. Child processes inherit open file descriptors. This is why pipes work. To prevent an fd from being inherited, close it.

  135.    1 # Redirecting only stderr to a pipe.
  136.    2
  137.    3 exec 3>&1                              # Save current "value" of stdout.
  138.    4 ls -l 2>&1 >&3 3>&- | grep bad 3>&-    # Close fd 3 for 'grep' (but not 'ls').
  139.    5 #              ^^^^   ^^^^
  140.    6 exec 3>&-                              # Now close it for the remainder of the script.
  141.    7
  142.    8 # Thanks, S.C.

  143. For a more detailed introduction to I/O redirection see Appendix E.
  144. 16.1. Using exec

  145. 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.

  146. Example 16-1. Redirecting stdin using exec

  147.    1 #!/bin/bash
  148.    2 # Redirecting stdin using 'exec'.
  149.    3
  150.    4
  151.    5 exec 6<&0          # Link file descriptor #6 with stdin.
  152.    6                    # Saves stdin.
  153.    7
  154.    8 exec < data-file   # stdin replaced by file "data-file"
  155.    9
  156.   10 read a1            # Reads first line of file "data-file".
  157.   11 read a2            # Reads second line of file "data-file."
  158.   12
  159.   13 echo
  160.   14 echo "Following lines read from file."
  161.   15 echo "-------------------------------"
  162.   16 echo $a1
  163.   17 echo $a2
  164.   18
  165.   19 echo; echo; echo
  166.   20
  167.   21 exec 0<&6 6<&-
  168.   22 #  Now restore stdin from fd #6, where it had been saved,
  169.   23 #+ and close fd #6 ( 6<&- ) to free it for other processes to use.
  170.   24 #
  171.   25 # <&6 6<&-    also works.
  172.   26
  173.   27 echo -n "Enter data  "
  174.   28 read b1  # Now "read" functions as expected, reading from normal stdin.
  175.   29 echo "Input read from stdin."
  176.   30 echo "----------------------"
  177.   31 echo "b1 = $b1"
  178.   32
  179.   33 echo
  180.   34
  181.   35 exit 0

  182. 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.

  183. Example 16-2. Redirecting stdout using exec

  184.    1 #!/bin/bash
  185.    2 # reassign-stdout.sh
  186.    3
  187.    4 LOGFILE=logfile.txt
  188.    5
  189.    6 exec 6>&1           # Link file descriptor #6 with stdout.
  190.    7                     # Saves stdout.
  191.    8
  192.    9 exec > $LOGFILE     # stdout replaced with file "logfile.txt".
  193.   10
  194.   11 # ----------------------------------------------------------- #
  195.   12 # All output from commands in this block sent to file $LOGFILE.
  196.   13
  197.   14 echo -n "Logfile: "
  198.   15 date
  199.   16 echo "-------------------------------------"
  200.   17 echo
  201.   18
  202.   19 echo "Output of \"ls -al\" command"
  203.   20 echo
  204.   21 ls -al
  205.   22 echo; echo
  206.   23 echo "Output of \"df\" command"
  207.   24 echo
  208.   25 df
  209.   26
  210.   27 # ----------------------------------------------------------- #
  211.   28
  212.   29 exec 1>&6 6>&-      # Restore stdout and close file descriptor #6.
  213.   30
  214.   31 echo
  215.   32 echo "== stdout now restored to default == "
  216.   33 echo
  217.   34 ls -al
  218.   35 echo
  219.   36
  220.   37 exit 0

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

  222.    1 #!/bin/bash
  223.    2 # upperconv.sh
  224.    3 # Converts a specified input file to uppercase.
  225.    4
  226.    5 E_FILE_ACCESS=70
  227.    6 E_WRONG_ARGS=71
  228.    7
  229.    8 if [ ! -r "$1" ]     # Is specified input file readable?
  230.    9 then
  231.   10   echo "Can't read from input file!"
  232.   11   echo "Usage: $0 input-file output-file"
  233.   12   exit $E_FILE_ACCESS
  234.   13 fi                   #  Will exit with same error
  235.   14                      #+ even if input file ($1) not specified (why?).
  236.   15
  237.   16 if [ -z "$2" ]
  238.   17 then
  239.   18   echo "Need to specify output file."
  240.   19   echo "Usage: $0 input-file output-file"
  241.   20   exit $E_WRONG_ARGS
  242.   21 fi
  243.   22
  244.   23
  245.   24 exec 4<&0
  246.   25 exec < $1            # Will read from input file.
  247.   26
  248.   27 exec 7>&1
  249.   28 exec > $2            # Will write to output file.
  250.   29                      # Assumes output file writable (add check?).
  251.   30
  252.   31 # -----------------------------------------------
  253.   32     cat - | tr a-z A-Z   # Uppercase conversion.
  254.   33 #   ^^^^^                # Reads from stdin.
  255.   34 #           ^^^^^^^^^^   # Writes to stdout.
  256.   35 # However, both stdin and stdout were redirected.
  257.   36 # -----------------------------------------------
  258.   37
  259.   38 exec 1>&7 7>&-       # Restore stout.
  260.   39 exec 0<&4 4<&-       # Restore stdin.
  261.   40
  262.   41 # After restoration, the following line prints to stdout as expected.
  263.   42 echo "File \"$1\" written to \"$2\" as uppercase conversion."
  264.   43
  265.   44 exit 0

  266. I/O redirection is a clever way of avoiding the dreaded inaccessible variables within a subshell problem.

  267. Example 16-4. Avoiding a subshell

  268.    1 #!/bin/bash
  269.    2 # avoid-subshell.sh
  270.    3 # Suggested by Matthew Walker.
  271.    4
  272.    5 Lines=0
  273.    6
  274.    7 echo
  275.    8
  276.    9 cat myfile.txt | while read line;
  277.   10                  do {
  278.   11                    echo $line
  279.   12                    (( Lines++ ));  #  Incremented values of this variable
  280.   13                                    #+ inaccessible outside loop.
  281.   14                                    #  Subshell problem.
  282.   15                  }
  283.   16                  done
  284.   17
  285.   18 echo "Number of lines read = $Lines"     # 0
  286.   19                                          # Wrong!
  287.   20
  288.   21 echo "------------------------"
  289.   22
  290.   23
  291.   24 exec 3<> myfile.txt
  292.   25 while read line <&3
  293.   26 do {
  294.   27   echo "$line"
  295.   28   (( Lines++ ));                   #  Incremented values of this variable
  296.   29                                    #+ accessible outside loop.
  297.   30                                    #  No subshell, no problem.
  298.   31 }
  299.   32 done
  300.   33 exec 3>&-
  301.   34
  302.   35 echo "Number of lines read = $Lines"     # 8
  303.   36
  304.   37 echo
  305.   38
  306.   39 exit 0
  307.   40
  308.   41 # Lines below not seen by script.
  309.   42
  310.   43 $ cat myfile.txt
  311.   44
  312.   45 Line 1.
  313.   46 Line 2.
  314.   47 Line 3.
  315.   48 Line 4.
  316.   49 Line 5.
  317.   50 Line 6.
  318.   51 Line 7.
  319.   52 Line 8.

  320. Notes
  321. [1]       

  322. 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.
  323. [2]       

  324. 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.
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP