TCP connection, bash only

后端 未结 2 504
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 02:00

I found this line in a script. While I globally understand what it does--opening a bidirectional TCP connection--, I need some explanations on the syntax. Here\'s the line:<

2条回答
  •  孤独总比滥情好
    2020-12-24 02:22

    I can only answer for the exec part:

    exec without a command given may be used to change I/O redirections. <> in this case means open for read+write. 5 is the channel number (or file descriptor). This makes sense if other commands send their output / read their input from channel 5.

    For "/dev/tcp/${SERVER}/${PORT}" I don't know if it's a feature of a specific Linux version or if it's a bash feature (I assume the latter).

    -- EDIT: from the bash manual page: --

     Bash handles several filenames specially when they are  used
     in redirections, as described in the following table:
    
          /dev/fd/fd
               If fd is a valid integer, file  descriptor  fd  is
               duplicated.
          /dev/stdin
               File descriptor 0 is duplicated.
          /dev/stdout
               File descriptor 1 is duplicated.
          /dev/stderr
               File descriptor 2 is duplicated.
          /dev/tcp/host/port
               If host is a valid hostname or  Internet  address,
               and  port  is  an  integer  port number or service
               name, bash attempts to open a  TCP  connection  to
               the corresponding socket.
          /dev/udp/host/port
               If host is a valid hostname or  Internet  address,
               and  port  is  an  integer  port number or service
               name, bash attempts to open a  UDP  connection  to
               the corresponding socket.
    

提交回复
热议问题