簡體   English   中英

為什么將 stderr 重定向到文件有效,但將 stderr 重定向到 stdout 不起作用? (Linux外殼)

[英]Why does redirecting stderr to file work, but redirecting to stderr to stdout does not work? (linux shell)

我在使用 linux shell 將 stderr 重定向到 stdout 時遇到了一些問題 以下確實有效:

$ sh -c "./some-binary" < ./my_input 2>test.txt
$ cat test.txt
Error
$

(output 保存到文件中)

但這不適合我:

$ sh -c "./some-binary" < ./my_input 2>&1
$

(沒有輸出)

我不明白為什么會這樣。 有人可以澄清一下嗎?

注意:我已經嘗試過bashsh並且有相同的行為

對於上下文:我正在嘗試運行此二進制文件並從現有程序中捕獲 output

編輯:我認為它正在使用/dev/fd/2作為 stderr 以防萬一

編輯 2:我將問題 dockerized 以使其可重現:Dockerfile:

FROM ubuntu:focal-20220801
RUN apt update
RUN apt install wget freeglut3 libxft2 libxext6 libxinerama1 libxcursor-dev -y
RUN wget https://github.com/grapa-dev/grapa/raw/master/bin/grapa-ubuntu64.tar.gz
RUN tar -xvf grapa-ubuntu64.tar.gz -C /usr/local/bin
docker run -it $(docker build -q .) /bin/bash
root@db9abeca8b5f:/# grapa -ccmd '$file().set("/dev/fd/2","Error\n")' >/dev/null 2>test.txt
root@db9abeca8b5f:/# cat test.txt 
Error
root@db9abeca8b5f:/# grapa -ccmd '$file().set("/dev/fd/2","Error\n")' >/dev/null 2>&1      
root@db9abeca8b5f:/# 

重定向很好。 Grapa 只是不支持規范文件 IO。

這是 Grapa 的$file().set的文檔:

更新一行中的列。 默認情況下,更新 $VALUE 列。 但是可以指定一個備用列。

正如建議的那樣,它適用於結構化文件 IO。 這是源代碼

        u64 size = pValue.mLength;
        err = mFile.SetSize(size);
        err = mFile.Write(0, 0, 0, size, pValue.mBytes);
        mFile.Purge(size, 1);
        err = mFile.Close();

寫 function

    err = GetSize(fileSize);
    if (err)
        return((GrapaError)-1);
    [...]
    endPos = lseek(mFp, (blockPos*blockSize + offset), SEEK_SET);
    if (endPos != (blockPos*blockSize + offset))
        return((GrapaError)-1);

如您所見,它強烈假定該文件是可搜索的,如果不是,則放棄。 這意味着您不能使用此 function 寫入終端、管道或任何非基本文件。

class 說“隨着時間的推移,此類/庫將得到增強,以支持在文件系統和 grapa 數據庫之外導航數據類型”。 您可以考慮提交功能請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM