在通过管道传输到另一个程序之前,如何将程序输出流延迟或缓冲几秒钟?
How do i delay or buffer the stream of a programs output for a few seconds before it pipes to another program?
我想要 运行 一个程序,它会在几秒钟后在 stderr 中显示生成的 url。然后我想把这个 url 传递给我的浏览器。我也想保持终端的输出不变,所以我使用了tee命令。
我已经通过将输出放入文件中解决了所有的解析和管道路径。但仍然需要弄清楚如何 link 它到程序本身。
michael@DESKTOP-OI3AOU6:~$ ./anaconda3/bin/jupyter lab ~ 2> 1.txt
michael@DESKTOP-OI3AOU6:~$ cat 1.txt
[I 12:02:11.619 NotebookApp] JupyterLab extension loaded from /home/michael/anaconda3/lib/python3.7/site-packages/jupyterlab
[I 12:02:11.620 NotebookApp] JupyterLab application directory is /home/michael/anaconda3/share/jupyter/lab
[I 12:02:11.622 NotebookApp] Serving notebooks from local directory: /home/michael/anaconda3/bin
[I 12:02:11.622 NotebookApp] The Jupyter Notebook is running at:
[I 12:02:11.622 NotebookApp] http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34
[I 12:02:11.622 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 12:02:11.631 NotebookApp]
To access the notebook, open this file in a browser:
file:///home/michael/.local/share/jupyter/runtime/nbserver-2069-open.html
Or copy and paste one of these URLs:
http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34
然后我可以像这样将它通过我的链:
michael@DESKTOP-OI3AOU6:~$ cat 1.txt > >(grep ^[[:blank:]].*http.* | tr -d " \t\n\r")
michael@DESKTOP-OI3AOU6:~$ http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34
通过设置自定义配置文件将 url 通过管道传输到我的浏览器中效果非常好:
cat 1.txt > >(grep ^[[:blank:]].*http.* | tr -d " \t\n\r" | xargs firefox.exe -P jupyterlab 2> /dev/null)
将它们放在一起,我得到了我想要的浏览器启动行为和错误日志显示:
michael@DESKTOP-OI3AOU6:~$ cat 1.txt > >(tee >(grep ^[[:blank:]].*http.* | tr -d " \t\n\r"| xargs firefox.exe -P jupyterlab 2>/dev/null))
michael@DESKTOP-OI3AOU6:~$ [I 12:02:11.619 NotebookApp] JupyterLab extension loaded from /home/michael/anaconda3/lib/python3.7/site-packages/jupyterlab
[I 12:02:11.620 NotebookApp] JupyterLab application directory is /home/michael/anaconda3/share/jupyter/lab
[I 12:02:11.622 NotebookApp] Serving notebooks from local directory: /home/michael/anaconda3/bin
[I 12:02:11.622 NotebookApp] The Jupyter Notebook is running at:
[I 12:02:11.622 NotebookApp] http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34
[I 12:02:11.622 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 12:02:11.631 NotebookApp]
To access the notebook, open this file in a browser:
file:///home/michael/.local/share/jupyter/runtime/nbserver-2069-open.html
Or copy and paste one of these URLs:
http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34
当我将我的程序附加到管道时出现问题。初始化服务器和提供输出需要几秒钟。在 grep 收到正确的行之前,tr 命令最终只是向浏览器推送一个空字符串。
michael@DESKTOP-OI3AOU6:~$ ./anaconda3/bin/jupyter lab ~ 2> >(tee >(gre
p ^[[:blank:]].*http.* | tr -d " \t\n\r"| xargs firefox.exe -P jupyterlab 2>/dev/null))
它可以用 grep,但是 url 只会在加载后几秒钟后显示:
michael@DESKTOP-OI3AOU6:~$ ./anaconda3/bin/jupyter lab ~ 2> >(grep ^[[:blank:]].*http.*)
http://localhost:8888/?token=6988d45b9baa2e9f07c5a91a9a91457d6119e9884bdbcb10
我试了之后没有任何显示。
michael@DESKTOP-OI3AOU6:~$ ./anaconda3/bin/jupyter lab ~ 2> >(grep ^[[:blank:]].*http.* | tr -d " \t\n\r")
如何让命令 (grep) 在发送到流链 (tr) 中的下一个命令之前等待几秒钟?
我能够通过简化问题并将输出保存到临时文件而不是尝试一次通过管道传输来解决问题。
michael@DESKTOP-OI3AOU6:~$ ~/anaconda3/bin/jupyter lab ~ 2> >(tee /tmp/jlab ) & sleep 4 ; cat /tmp/jlab | grep ^[[:blank:]].*http.* | tr -d " \t\n\r" | xargs firefox.exe -P jupyterlab ; rm /tmp/jlab; %
逐行浏览每个部分以供其他人参考:
~/anaconda3/bin/jupyter lab ~
→ 运行 主目录 (~) 中的 jupyter 实验室 session。
2>
→ 将标准错误输入文件中。
>()
→ 允许进入文件的管道被定向到所附命令的标准输入。
tee /tmp/jlab
→ 将输入重定向到临时文件 jlab 并将其复制到标准输出。这就是我如何保留原始程序在终端中显示信息的行为。更多信息 https://en.wikipedia.org/wiki/Tee_(command)
>(tee /tmp/jlab )
→ 输出通过管道传输到 tee 命令
&
→ 允许进程在后台继续 运行。
sleep 4
→ 等待 4 秒让服务器启动。
;
→ 命令执行后执行下一条命令
cat /tmp/jlab
→ 将临时文件 /tmp/jlab 的内容连接到标准输出中。
|
→ 将左侧程序的标准输出通过管道连接到右侧程序的标准输入。在这种情况下 cat /tmp/jlab
变成 grep ^[[:blank:]].*http.*
.
grep ^[[:blank:]].*http.*
→ 提取开头有 space 和 space 且包含 http 的行。它将允许介于两者之间和之后的任意数量的字符。在这种情况下,它的效果非常好,但如果 jupyter 的更新偶然改变了输出,这就是它会中断的地方,并且将选择更合适的正则表达式。
|
grep 的输出通过管道传输到 tr
tr -d " \t\n\r"
→ 从行中删除所有制表符 space 和换行符。
|
将 tr 的输出通过管道传输到 xargs。这是 jupyter session.
独有的完整 url
xargs firefox.exe -P jupyterlab
→ Xargs 获取其标准输入并将其作为参数提供给以下命令。
在这种情况下,firefox.exe
是我存储在 /usr/local/bin/firefox.exe
中的软 link,软 link 指向安装的 windows 位置 /mnt/c/
位于 /mnt/c/Program Files/Mozilla Firefox/Firefox.exe
。我以这种方式安装它的原因只是我的惯例,因为 windows 可执行文件比在 WSL 中执行的程序和通过 xming 运行 执行的程序具有更好的渲染效果。
-P jupyterlab → 启动我创建的配置文件,它从 firefox 中删除了选项卡和导航栏。我还访问了 firefox 中的自定义选项,因此显示了标题栏。
配置文件是通过在特定配置文件目录%APPDATA%\Mozilla\Firefox\Profiles\
中设置自定义css来设置的
文件的完整路径是%APPDATA%\Mozilla\Firefox\Profilesvv7gs2r.jupyterlab\chrome\userChrome.css
此文件将设置 firefox,因此它不会有使 window.
混乱的选项卡或导航
文件内容如下:
/*
* Do not remove the @namespace line -- it's required for correct functioning
*/
@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"); /* set default namespace to XUL */
/*
* Hide tab bar, navigation bar and scrollbars
* !important may be added to force override, but not necessary
* #content is not necessary to hide scroll bars
*/
#TabsToolbar {visibility: collapse;}
#navigator-toolbox {visibility: collapse;}
;
→ 命令执行后执行下一条命令
rm /tmp/jlab
→删除临时文件的样板行。它通常应该在 linux 系统重置时删除,但它并不适用于所有实现。我还没有检查wsl是否这样做。
;
→ 命令执行后执行下一条命令
%
→ 将后台移动的最后一个作业 &
移到前台。现在执行的程序现在将像移到后台之前一样接受中断。
你可以很容易地做到这一点:
$ echo "hello world" | { read test && sleep 2 && echo $test; } | xargs echo
我想要 运行 一个程序,它会在几秒钟后在 stderr 中显示生成的 url。然后我想把这个 url 传递给我的浏览器。我也想保持终端的输出不变,所以我使用了tee命令。
我已经通过将输出放入文件中解决了所有的解析和管道路径。但仍然需要弄清楚如何 link 它到程序本身。
michael@DESKTOP-OI3AOU6:~$ ./anaconda3/bin/jupyter lab ~ 2> 1.txt
michael@DESKTOP-OI3AOU6:~$ cat 1.txt
[I 12:02:11.619 NotebookApp] JupyterLab extension loaded from /home/michael/anaconda3/lib/python3.7/site-packages/jupyterlab
[I 12:02:11.620 NotebookApp] JupyterLab application directory is /home/michael/anaconda3/share/jupyter/lab
[I 12:02:11.622 NotebookApp] Serving notebooks from local directory: /home/michael/anaconda3/bin
[I 12:02:11.622 NotebookApp] The Jupyter Notebook is running at:
[I 12:02:11.622 NotebookApp] http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34
[I 12:02:11.622 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 12:02:11.631 NotebookApp]
To access the notebook, open this file in a browser:
file:///home/michael/.local/share/jupyter/runtime/nbserver-2069-open.html
Or copy and paste one of these URLs:
http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34
然后我可以像这样将它通过我的链:
michael@DESKTOP-OI3AOU6:~$ cat 1.txt > >(grep ^[[:blank:]].*http.* | tr -d " \t\n\r")
michael@DESKTOP-OI3AOU6:~$ http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34
通过设置自定义配置文件将 url 通过管道传输到我的浏览器中效果非常好:
cat 1.txt > >(grep ^[[:blank:]].*http.* | tr -d " \t\n\r" | xargs firefox.exe -P jupyterlab 2> /dev/null)
将它们放在一起,我得到了我想要的浏览器启动行为和错误日志显示:
michael@DESKTOP-OI3AOU6:~$ cat 1.txt > >(tee >(grep ^[[:blank:]].*http.* | tr -d " \t\n\r"| xargs firefox.exe -P jupyterlab 2>/dev/null))
michael@DESKTOP-OI3AOU6:~$ [I 12:02:11.619 NotebookApp] JupyterLab extension loaded from /home/michael/anaconda3/lib/python3.7/site-packages/jupyterlab
[I 12:02:11.620 NotebookApp] JupyterLab application directory is /home/michael/anaconda3/share/jupyter/lab
[I 12:02:11.622 NotebookApp] Serving notebooks from local directory: /home/michael/anaconda3/bin
[I 12:02:11.622 NotebookApp] The Jupyter Notebook is running at:
[I 12:02:11.622 NotebookApp] http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34
[I 12:02:11.622 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 12:02:11.631 NotebookApp]
To access the notebook, open this file in a browser:
file:///home/michael/.local/share/jupyter/runtime/nbserver-2069-open.html
Or copy and paste one of these URLs:
http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34
当我将我的程序附加到管道时出现问题。初始化服务器和提供输出需要几秒钟。在 grep 收到正确的行之前,tr 命令最终只是向浏览器推送一个空字符串。
michael@DESKTOP-OI3AOU6:~$ ./anaconda3/bin/jupyter lab ~ 2> >(tee >(gre
p ^[[:blank:]].*http.* | tr -d " \t\n\r"| xargs firefox.exe -P jupyterlab 2>/dev/null))
它可以用 grep,但是 url 只会在加载后几秒钟后显示:
michael@DESKTOP-OI3AOU6:~$ ./anaconda3/bin/jupyter lab ~ 2> >(grep ^[[:blank:]].*http.*)
http://localhost:8888/?token=6988d45b9baa2e9f07c5a91a9a91457d6119e9884bdbcb10
我试了之后没有任何显示。
michael@DESKTOP-OI3AOU6:~$ ./anaconda3/bin/jupyter lab ~ 2> >(grep ^[[:blank:]].*http.* | tr -d " \t\n\r")
如何让命令 (grep) 在发送到流链 (tr) 中的下一个命令之前等待几秒钟?
我能够通过简化问题并将输出保存到临时文件而不是尝试一次通过管道传输来解决问题。
michael@DESKTOP-OI3AOU6:~$ ~/anaconda3/bin/jupyter lab ~ 2> >(tee /tmp/jlab ) & sleep 4 ; cat /tmp/jlab | grep ^[[:blank:]].*http.* | tr -d " \t\n\r" | xargs firefox.exe -P jupyterlab ; rm /tmp/jlab; %
逐行浏览每个部分以供其他人参考:
~/anaconda3/bin/jupyter lab ~
→ 运行 主目录 (~) 中的 jupyter 实验室 session。
2>
→ 将标准错误输入文件中。
>()
→ 允许进入文件的管道被定向到所附命令的标准输入。
tee /tmp/jlab
→ 将输入重定向到临时文件 jlab 并将其复制到标准输出。这就是我如何保留原始程序在终端中显示信息的行为。更多信息 https://en.wikipedia.org/wiki/Tee_(command)
>(tee /tmp/jlab )
→ 输出通过管道传输到 tee 命令
&
→ 允许进程在后台继续 运行。
sleep 4
→ 等待 4 秒让服务器启动。
;
→ 命令执行后执行下一条命令
cat /tmp/jlab
→ 将临时文件 /tmp/jlab 的内容连接到标准输出中。
|
→ 将左侧程序的标准输出通过管道连接到右侧程序的标准输入。在这种情况下 cat /tmp/jlab
变成 grep ^[[:blank:]].*http.*
.
grep ^[[:blank:]].*http.*
→ 提取开头有 space 和 space 且包含 http 的行。它将允许介于两者之间和之后的任意数量的字符。在这种情况下,它的效果非常好,但如果 jupyter 的更新偶然改变了输出,这就是它会中断的地方,并且将选择更合适的正则表达式。
|
grep 的输出通过管道传输到 tr
tr -d " \t\n\r"
→ 从行中删除所有制表符 space 和换行符。
|
将 tr 的输出通过管道传输到 xargs。这是 jupyter session.
xargs firefox.exe -P jupyterlab
→ Xargs 获取其标准输入并将其作为参数提供给以下命令。
在这种情况下,firefox.exe
是我存储在 /usr/local/bin/firefox.exe
中的软 link,软 link 指向安装的 windows 位置 /mnt/c/
位于 /mnt/c/Program Files/Mozilla Firefox/Firefox.exe
。我以这种方式安装它的原因只是我的惯例,因为 windows 可执行文件比在 WSL 中执行的程序和通过 xming 运行 执行的程序具有更好的渲染效果。
-P jupyterlab → 启动我创建的配置文件,它从 firefox 中删除了选项卡和导航栏。我还访问了 firefox 中的自定义选项,因此显示了标题栏。
配置文件是通过在特定配置文件目录%APPDATA%\Mozilla\Firefox\Profiles\
文件的完整路径是%APPDATA%\Mozilla\Firefox\Profilesvv7gs2r.jupyterlab\chrome\userChrome.css
此文件将设置 firefox,因此它不会有使 window.
混乱的选项卡或导航文件内容如下:
/*
* Do not remove the @namespace line -- it's required for correct functioning
*/
@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"); /* set default namespace to XUL */
/*
* Hide tab bar, navigation bar and scrollbars
* !important may be added to force override, but not necessary
* #content is not necessary to hide scroll bars
*/
#TabsToolbar {visibility: collapse;}
#navigator-toolbox {visibility: collapse;}
;
→ 命令执行后执行下一条命令
rm /tmp/jlab
→删除临时文件的样板行。它通常应该在 linux 系统重置时删除,但它并不适用于所有实现。我还没有检查wsl是否这样做。
;
→ 命令执行后执行下一条命令
%
→ 将后台移动的最后一个作业 &
移到前台。现在执行的程序现在将像移到后台之前一样接受中断。
你可以很容易地做到这一点:
$ echo "hello world" | { read test && sleep 2 && echo $test; } | xargs echo