bash 重定向到/dev/stdout:不是目录 [英] bash redirect to /dev/stdout: Not a directory

查看:26
本文介绍了bash 重定向到/dev/stdout:不是目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从 CentOS 5.8(带有 GNU bash 3.2.25)升级到 CentOS 6.5(带有 GNU bash 4.1.2).一个曾经适用于 CentOS 5.8 的命令不再适用于 CentOS 6.5.这是一个简单的解决方法的愚蠢示例,但我试图了解导致不同行为的 bash 引擎盖下发生的事情.可能是 bash 4.1.2 中的一个新 bug,或者是一个已修复的旧 bug,新的行为是预期的?

I recently upgraded from CentOS 5.8 (with GNU bash 3.2.25) to CentOS 6.5 (with GNU bash 4.1.2). A command that used to work with CentOS 5.8 no longer works with CentOS 6.5. It is a silly example with an easy workaround, but I am trying to understand what is going on underneath the bash hood that is causing the different behavior. Maybe it is a new bug in bash 4.1.2 or an old bug that was fixed and the new behavior is expected?

CentOS 5.8:

CentOS 5.8:

    (echo "hi" > /dev/stdout) > test.txt
    echo $?
0
    cat test.txt
hi

CentOS 6.5:

CentOS 6.5:

    (echo "hi" > /dev/stdout) > test.txt
-bash: /dev/stdout: Not a directory
    echo $?
1

更新:看起来这不是与 CentOS 版本有关的问题.我有另一台 CentOS 6.5 机器,该命令可以运行.我已经消除了任何环境变量作为罪魁祸首.有任何想法吗?在所有机器上,这些命令给出相同的输出:

Update: It doesn't look like this is problem related to CentOS version. I have another CentOS 6.5 machine where the command works. I have eliminated any environment variables as the culprit. Any ideas? On all the machines these commands gives the same output:

    ls -ld /dev/stdout
lrwxrwxrwx 1 root root 15 Apr 30 13:30 /dev/stdout -> /proc/self/fd/1

    ls -lL /dev/stdout
crw--w---- 1 user1 tty 136, 0 Oct 28 23:21 /dev/stdout

另一个更新:似乎子 shell 正在继承父 shell 的重定向标准输出.我猜这并不太令人惊讶,但为什么它仍然可以在一台机器上运行,但是当它们运行相同的 bash 版本时在另一台机器上失败?

Another Update: It seems the sub-shell is inheriting the redirected stdout of the parent shell. The is not too surprising I guess, but still why does it work on one machine, but fail on the other machine when they are running the same bash version?

在工作机器上:

    ((ls -la /dev/stdout; ls -la /proc/self/fd/1) >/dev/stdout) > test.txt
    cat test.txt
lrwxrwxrwx 1 root root 15 Aug 13 08:14 /dev/stdout -> /proc/self/fd/1
l-wx------ 1 user1 aladdin 64 Oct 29 06:54 /proc/self/fd/1 -> /home/user1/test.txt

我认为 Yu Huang 是对的,重定向到/tmp 在两台机器上都有效.两台机器都使用 isilon NAS 进行/home 挂载,但可能其中一台的文件系统版本或配置略有不同,导致错误.总之,应该避免重定向到/dev/stdout,除非你知道父进程不会重定向它.

I think Yu Huang is right, redirecting to /tmp works on both machines. Both machines are using isilon NAS for the /home mount, but probably one has slightly different filesystem version or configuration that caused the error. In conclusion, redirecting to /dev/stdout should be avoided unless you know the parent process will not redirecting it.

更新:从 v3 升级到 NFS v4 后出现此问题.降级回 v3 后,此行为消失了.

UPDATE: This problem arose after upgrade to NFS v4 from v3. After downgrading back to v3 this behavior went away.

推荐答案

早安,user1999165,:)

Good morning, user1999165, :)

我怀疑它与底层文件系统有关.在同一台机器上,尝试:

I suspect it's related to the underlying filesystem. On the same machine, try:

(echo "hi" >/dev/stdout) > /tmp/test.txt

(echo "hi" > /dev/stdout) > /tmp/test.txt

/tmp/ 应该是 linux 原生(ext3 之类的)文件系统

/tmp/ should be linux native (ext3 or something) filesystem

这篇关于bash 重定向到/dev/stdout:不是目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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