Shell 如果脚本';s标准输出在执行过程中是否重定向到文件?

Shell 如果脚本';s标准输出在执行过程中是否重定向到文件?,shell,scripting,stdout,file-descriptor,io-redirection,Shell,Scripting,Stdout,File Descriptor,Io Redirection,考虑一下./my\u script>/var/log/my\u log 只有此脚本中的单个echo语句才能转到标准输出 这是如何实现的呢?所以我们有一些聪明的程序 cat print2stdout #!/bin/sh echo some words secret and sent to null echo some words to stdout > /dev/fd/3 最后一行放回打开的3个文件描述符 调用时,我们将3fd映射到标准输出,然后将标准输出重定向到文件 结果如下: .

考虑一下./my\u script>/var/log/my\u log

只有此脚本中的单个echo语句才能转到标准输出


这是如何实现的呢?

所以我们有一些聪明的程序

cat print2stdout 
#!/bin/sh

echo some words secret and sent to null

echo some words to stdout > /dev/fd/3
最后一行放回打开的3个文件描述符

调用时,我们将3fd映射到标准输出,然后将标准输出重定向到文件

结果如下:

./print2stdout 3>&1 >/dev/null  
some words to stdout

只需使用
/dev/tty
,无论重定向如何,它都指向您的终端仿真器

#!/bin/sh

echo this line go to the possibly redirected stdout 
echo this line shows up on the screen > /dev/tty

你为什么喜欢最复杂的答案?