bash stdout重定向在for循环中 [英] bash stdout redirection in a for loop

查看:53
本文介绍了bash stdout重定向在for循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复

你好

我在bash for循环中努力进行输出重定向.我有几个类似的bash脚本,每个脚本在不同的输入文件上启动.这些脚本称为带有一些参数的工具.

I'm struggling with output redirection within a bash for loop. I have several similar bash scripts, each scripts being launched on a different input file. The scripts called a tool with a few arguments.

bash 脚本是这样的:

The bash scripts are like this :

my_tool --input input_file --arg1 arg1_name --arg2 arg2_name > output_dir/result_X.dat

X 是每个脚本唯一的字符串.

X being a string which is unique for each script.

我都在for循环中运行它们:

I run all of them in a for loop :

for script in scripts_dir/*.sh
do
    bash $script
done

但是,每次运行的标准输出仍显示在终端上.指定的输出文件已创建,但为空.我该如何解决?我在stackoverflow上发现了其他问题,答案是在一个大输出文件中重定向完整循环,但我想避免这种情况.

However, the stdout of each run still display on the terminal. The specified output files are created, but empty. How I can solve that ? I found other questions on stackoverflow where the answer is a redirection of the full loop in one big output file, but I'd like to avoid that.

如果我替换>>的output_dir/result_X.dat /dev/null :标准输出显示在终端上

If I replace > output_dir/result_X.dat by > /dev/null : the standard outputs display on the terminal

如果我替换>通过`2>/dev/null来输出output_dir/result_X.dat :不显示任何内容.

If I replace > output_dir/result_X.dat by ` 2> /dev/null : nothing is displayed.

谢谢.

推荐答案

启动 my_tool 时,该工具中通常有3个文件描述符:

When you start my_tool, there are normally 3 file-descriptors available in the tool:

  • STDIN
  • STDOUT
  • STDERR

STDIN用于输入,因此与该问题无关.STDOUT用于标准输出.这是文件描述符1.如果您这样做

STDIN is used for input, and therefore irrelevant for this question. STDOUT is used for standard output. This is file-descriptor 1. If you do

ls 1> /dev/null

ls的STDOUT写入/dev/null .如果您未添加 1 ,如 ls>/dev/null ,假设您的意思是STDOUT.

the STDOUT of ls is written to /dev/null. If you do not add the 1, as in ls > /dev/null, it is assumed that you mean STDOUT.

STDERR用作错误消息的标准输出.STDERR的数量为2.

STDERR is used as standard output for error messages, in the broadest sense of the word. The number of STDERR is 2.

使用 ls 代替您的 my_command ls>文件会将列表放在文件中. ls/non_existing_dir_>文件会将 ls 的STDOUT放在 file 中.但是STDOUT上没有输出,并且由于STDERR没有重定向,它将被发送到终端.

Using ls instead of your my_command, ls > file will put the listing in the file. ls /non_existing_dir_ > file will put the STDOUT of ls in the file. But there is no output on STDOUT, and because STDERR is not redirected, it will be send to the terminal.

总而言之,

ls . /non_existing_dir 2>stderr >stdout

将把.的目录列表放在stdout文件中,stderr中不存在目录的错误.

will put the directory listing of . in the file stdout and the error for the non-existing directory in stderr.

使用 2>& 1 ,您可以将filedescriptor2(STDERR)的输出重定向到文件描述符1(SDTOUT).

With 2>&1 you redirect the output of filedescriptor2 (STDERR) to file descriptor 1 (SDTOUT).

要使事情复杂一点,可以添加其他文件描述符编号.例如:

To complicate things a bit, you can add other file descriptor numbers. For example:

exec 3>file

会将文件描述符3(新创建的)的输出放在 file 中.还有

will put the output of file descriptor 3 (which is newly created) in file. And

ls 1>&3

然后将文件描述符1的输出重定向到文件描述符3,将 ls 的输出有效地放置在 file 中.

will then redirect the output of file descriptor 1 to file descriptor 3, effectively putting the output of ls in file.

这篇关于bash stdout重定向在for循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆