简体   繁体   English

为什么“gnome-screensaver-command -q > test.txt”不输出到文件?

[英]Why “gnome-screensaver-command -q > test.txt” do not output to file?

In short, I am looking for the explanation why the output of the command does not reach the file.简而言之,我正在寻找为什么命令的输出没有到达文件的解释。 And if I do something wrong here, how do I do it right so the output would reach the file?如果我在这里做错了什么,我该怎么做才能让输出到达文件?

The command:命令:

gnome-screensaver-command -q > test.txt

TL;TR TL;TR

At first, the short story, the point of mine was to find a way for the bash script to tell if someone is working on the computer (the screen is unlocked) or not (the screen is locked).一开始是短篇小说,我的目的是为 bash 脚本找到一种方法来判断某人是否正在计算机上工作(屏幕未锁定)或没有(屏幕已锁定)。 And it seems I have found the command ( gnome-screensaver-command -q ) for that, maybe, not sure yet as I cannot really check the locked/unlocked stages with the script..似乎我已经找到了命令( gnome-screensaver-command -q ),也许还不确定,因为我无法真正检查脚本的锁定/解锁阶段..

If I run the command gnome-screensaver-command -q in the terminal screen itself then I get the output back as expected.如果我在终端屏幕本身中运行命令gnome-screensaver-command -q ,那么我会按预期返回输出。 The output suggests me I am on the right path with the command:输出表明我使用以下命令走在正确的道路上:

** Message: 00:06:00.049: Screensaver is not running!

If I run the same code in the script and output the result to the variable IS_SCREEN_LOCKED=$(gnome-screensaver-command -q) then I get nothing back.. I mean, the value of $IS_SCREEN_LOCKED variable is empty.如果我在脚本中运行相同的代码并将结果输出到变量IS_SCREEN_LOCKED=$(gnome-screensaver-command -q)那么我什么也得不到......我的意思是, $IS_SCREEN_LOCKED变量的值为空。

If I run gnome-screensaver-command -q > test.txt in the terminal then ** Message: 00:06:00.049: Screensaver is not running!如果我在终端中运行gnome-screensaver-command -q > test.txt然后** Message: 00:06:00.049: Screensaver is not running! reaches the screen and nothing reaches the file... Which tells me the redirect itself works quite ok (because the file is created), just the command is somehow different... May someone explain what's the trick with the output here?到达屏幕而没有任何内容到达文件...这告诉我重定向本身工作得很好(因为文件已创建),只是命令在某种程度上有所不同...有人可以解释这里的输出有什么技巧吗?

I suspect it is some kind of "different type of stream".. Is there a stream, not sure if that is the good word for it, called "a query" as man states -q - Query the state of the screensaver ?... Not sure what's the matter with the command directing the output to the file here.我怀疑它是某种“不同类型的流”......是否有一个流,不确定这是否是它的好词,被称为“查询”,因为man状态-q - Query the state of the screensaver ?。 .. 不确定将输出定向到此处的文件的命令有什么问题。

System details:系统详情:

  • Ubuntu 20.04.1 LTS (Focal Fossa) Ubuntu 20.04.1 LTS(焦点窝)
  • GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu) GNU bash,版本 5.0.17(1)-release (x86_64-pc-linux-gnu)
  • gnome-screensaver-command 3.6.1 gnome-屏幕保护程序命令 3.6.1

It may be writing to stderr instead of stdout.它可能正在写入 stderr 而不是 stdout。 So redirect fd 2 rather than fd 1.所以重定向 fd 2 而不是 fd 1。

gnome-screensaver-command -q 2> test.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM