TCP 连接,仅限 bash [英] TCP connection, bash only

查看:16
本文介绍了TCP 连接,仅限 bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在脚本中发现了这一行.虽然我全局理解它的作用——打开一个双向 TCP 连接——,但我需要一些关于语法的解释.这是行:

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:

exec 5<>"/dev/tcp/${SERVER}/${PORT}"

还有我的问题:

  1. <> 通常用于重定向 IO.在那里是什么意思?它可以在其他上下文中使用吗?怎么样?
  2. 为什么它有效,而 /dev/tcp 不存在?
  3. 为什么是 5?可以是另一个号码吗?允许的值是多少?
  4. 为什么需要 exec?(假设实际上没有执行)
  1. < and > are usually used to redirect IOs. What does it mean there? Is it usable in another context? How?
  2. Why does it work, while /dev/tcp doesn't exists?
  3. Why 5? Can it be another number? What are the values allowed?
  4. Why is exec necessary? (given nothing is actually executed)

谢谢.

推荐答案

<和 > 通常用于重定向 IO.在那里是什么意思?它可以在其他上下文中使用吗?怎么样?

< and > are usually used to redirect IOs. What does it mean there? Is it usable in another context? How?

这是一样的 - 输入和输出被重定向到 fd 5.

It's the same - input and output is redirected to fd 5.

为什么它有效,而/dev/tcp 不存在?

Why does it work, while /dev/tcp doesn't exists?

这是一个特殊文件:如果 host 是一个有效的主机名或 Internet 地址,并且 port 是一个整数端口号或服务名,bash 会尝试打开一个到相应套接字的 TCP 连接.

It's a special file: 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.

为什么是 5?可以是另一个号码吗?允许的值是多少?

Why 5? Can it be another number? What are the values allowed?

是的,它可以是任何值,但您需要确保不使用已经在使用的 fd.

Yes, it can be any value, but you need to ensure you don't use an fd already in use.

为什么需要 exec?(假设没有实际执行)

Why is exec necessary? (given nothing is actually executed)

exec 表示重定向发生在当前 shell 中,而不是在子 shell 中.

exec means the redirection happens in the current shell, not within a subshell.

这篇关于TCP 连接,仅限 bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆