在“for”循环中使用“tee”进行管道传输

发布于 2024-11-26 16:17:12 字数 559 浏览 1 评论 0原文

这可能是新手逃避问题。我正在尝试在这样的 for 循环中运行命令

$ for SET in `ls ../../mybook/WS/wsc_production/`; do ~/sandbox/scripts/ftype-switch/typesort.pl /media/mybook/WS/wsc_production/$SET ./wsc_sorter/$SET | tee -a sorter.log; done;

,但最终 sorter.log 为空。 (我确信有一些输出。)如果我转义管道符号 (\|),我最终不会得到 sorter.log 完全没有。

我做错了什么?

$ bash --version
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)

编辑:糟糕,/media/mybook/睡着了,所以实际上没有输出。该代码首先是正确的。不过还是谢谢大家的评论。

This is probably a newbie's escaping problem. I'm trying run command in a for loop like this

$ for SET in `ls ../../mybook/WS/wsc_production/`; do ~/sandbox/scripts/ftype-switch/typesort.pl /media/mybook/WS/wsc_production/$SET ./wsc_sorter/$SET | tee -a sorter.log; done;

but I end up with sorter.log being empty. (I'm sure there is some output.) If I escape the pipe symbol (\|), I end up with no sorter.log at all.

What am I doing wrong?

$ bash --version
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)

Edit: Oops, /media/mybook/ fell asleep, so there actually was no output. The code was correct in the first place. Thanks to all for comments, though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯然#的苍凉 2024-12-03 16:17:12

格伦说得好。我想提供一个不同的角度:您可以将“tee”命令移到 for 循环之外。这种方法的优点是 tee 仅被调用一次:

dir1=$HOME/sandbox/scripts/ftype-switch
dir2=/media/mybook/WS/wsc_production
for SET in ../../mybook/WS/wsc_production/*; do 
  $dir1/typesort.pl $dir2/$SET ./wsc_sorter/$SET 2>&1 
done | tee -a sorter.log

Glenn said it well. I would like to offer a different angle: you can move the 'tee' command outside of the for loop. The advantage to this approach is tee is invoked only once:

dir1=$HOME/sandbox/scripts/ftype-switch
dir2=/media/mybook/WS/wsc_production
for SET in ../../mybook/WS/wsc_production/*; do 
  $dir1/typesort.pl $dir2/$SET ./wsc_sorter/$SET 2>&1 
done | tee -a sorter.log
盗心人 2024-12-03 16:17:12

您正在使用 tee,因此如果有输出,您会在终端上看到它。你看到了什么?

如果您看到输出,则您看到的可能是 stderr,因此您可能需要重定向它:

dir1=$HOME/sandbox/scripts/ftype-switch
dir2=/media/mybook/WS/wsc_production
for SET in ../../mybook/WS/wsc_production/*; do 
  $dir1/typesort.pl $dir2/$SET ./wsc_sorter/$SET 2>&1 | tee -a sorter.log
done

You're using tee, so if there is output, you'd see it on your terminal. What do you see?

If you see output, it's probably stderr you're seeing, so you might want to redirect it:

dir1=$HOME/sandbox/scripts/ftype-switch
dir2=/media/mybook/WS/wsc_production
for SET in ../../mybook/WS/wsc_production/*; do 
  $dir1/typesort.pl $dir2/$SET ./wsc_sorter/$SET 2>&1 | tee -a sorter.log
done
几度春秋 2024-12-03 16:17:12

我最深切的歉意,问题出在其他地方,我的脚本实际上根本没有输出任何内容。现在可以了。

我产生了问题在于转义的错觉的两个原因:

  • 当然,对 bash 脚本缺乏信心,这是缺乏知识和经验的结果
  • ,也是缺乏关注的结果 - 我没有想到USB 上的磁盘睡着了,所以当我尝试循环时,实际上没有输出。

好吧,这是我在知识之路上遇到的一些绊脚石......:)

My deepest apologies, the problem was somewhere else and my script actually did not output anything at all. Now it works.

Two reasons why I got the illusion that the problem is in escaping:

  • of course, lack of confidence in bash scripting, which is effect of lack of knowledge and experience
  • and also, lack of attention--it did not come into my mind that the disk on USB fell asleep, so when I tried the loop there actually was no output

Well, that's for some stumbling on my way to knowledge... :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文