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

查看:626
本文介绍了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中的一个新错误,还是一个已修复的旧错误,并且预期会有新的行为?

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

另一个更新:看来子外壳继承了父外壳的重定向标准输出.我猜这并不奇怪,但是为什么它仍然可以在一台机器上运行,而在另一台机器上运行相同的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:

(回显"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天全站免登陆