Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 意外的重定向结果为2>&;1和命令提示符_Bash_Shell - Fatal编程技术网

Bash 意外的重定向结果为2>&;1和命令提示符

Bash 意外的重定向结果为2>&;1和命令提示符,bash,shell,Bash,Shell,我在玩nc,因为-e命令不见了,所以建议 cat /tmp/f | /bin/sh -i 2>&1 | nc -l 127.0.0.1 1234 > /tmp/f 我认为“2>&1”是不必要的,因为我不需要知道我将要运行的简单命令(如“whoami”、“ls”等)的错误,所以我的命令是 cat /tmp/f | /bin/sh -i | nc -l 127.0.0.1 1234 > /tmp/f 但是,我没有得到“干净”的结果。这就是我得到的 服务器: Listen

我在玩nc,因为-e命令不见了,所以建议

cat /tmp/f | /bin/sh -i 2>&1 | nc -l 127.0.0.1 1234 > /tmp/f
我认为“2>&1”是不必要的,因为我不需要知道我将要运行的简单命令(如“whoami”、“ls”等)的错误,所以我的命令是

cat /tmp/f | /bin/sh -i | nc -l 127.0.0.1 1234 > /tmp/f
但是,我没有得到“干净”的结果。这就是我得到的

服务器:

Listening on [0.0.0.0] (family 0, port 1234)
$  $
Listening on [0.0.0.0] (family 0, port 1234)
客户:

nc localhost 1234
ls 
testfile
whoami
user1
nc localhost 1234
$ ls 
testfile
$ whoami
user1
基本上,我在客户端没有看到美元。当我使用2>&1执行命令时,我得到了我所期望的结果

服务器:

Listening on [0.0.0.0] (family 0, port 1234)
$  $
Listening on [0.0.0.0] (family 0, port 1234)
客户:

nc localhost 1234
ls 
testfile
whoami
user1
nc localhost 1234
$ ls 
testfile
$ whoami
user1

我知道2>&1意味着将stderr重定向到stdout,所以在本例中,命令提示符“$”是stderr的一部分吗?它不应该是stdin的一部分吗

shell使用标准错误流进行提示,也用于由
select
创建的菜单,以及用于与用户交互的任何其他文本。

shell使用标准错误流进行提示,也用于由
select
创建的菜单,以及用于与用户交互的任何其他文本。

stdin
是您在shell中键入的内容,因此
$
不能成为其中的一部分。使用
/bin/sh-i>/dev/null
/bin/sh-i2>/dev/null
stdin
可以很容易地测试提示符的位置,因此
$
不能成为shell的一部分。可以使用
/bin/sh-i>/dev/null
/bin/sh-i2>/dev/null
轻松测试提示符的位置。这非常有趣!你有这方面的文件吗?我想读一下这方面的动机。我会假设提示和菜单会在stdout中。感谢您的评论。@Arrow POSIX标准对此非常明确:“每次交互式shell准备读取命令时,此变量(
$PS1
)的值都将进行参数扩展并写入标准错误。”谢谢。我真的不知道这一点,也没有预料到这一点。你已经回答了我的问题。这很有趣!你有这方面的文件吗?我想读一下这方面的动机。我会假设提示和菜单会在stdout中。感谢您的评论。@Arrow POSIX标准对此非常明确:“每次交互式shell准备读取命令时,此变量(
$PS1
)的值都将进行参数扩展并写入标准错误。”谢谢。我真的不知道这一点,也没有预料到这一点。你已经回答了我的问题。