简体   繁体   English

如何将非常长的参数列表传递给程序?

[英]How to pass a very very long list of arguments to a program?

I'm coding and running programs to which I need to pass a long list of data files for analysis, sometimes a few hundred thousands. 我正在编码和运行程序,我需要将一长串的数据文件传递给这些程序进行分析,有时需要几十万个。 The problem is that the argument list can be so long that the system (Unix) refuses to run it, outputting: 问题在于参数列表可能很长,以致系统(Unix)拒绝运行它,输出:

bash: ./yourProgram: Argument list too long

Is there a environment variable I can change to bypass this obstacle? 是否可以更改环境变量以绕过此障碍?

The only solution I can think of is writing my program list in a separate file (using ls ... > ) and then reading each file line by line. 我能想到的唯一解决方案是将程序列表写入一个单独的文件中(使用ls ... > ),然后逐行读取每个文件。 Would you know of anything simpler? 您知道更简单的方法吗?

ps: my programs are written in C++ if it matters ps:如果重要的话,我的程序是用C ++编写的

Better to have an environment variable defined with values as space delimited list of items, for example, define as 最好将环境变量定义为以空格分隔的项目列表的值,例如,定义为

export MYLIST=a b ab cd ef

Within your program, use getenv("MYLIST") to get the value as char *, and tokenize to get individual values 在您的程序中,使用getenv(“ MYLIST”)将值获取为char *,并标记化以获取单个值

我只是将其作为标准输入传递。

echo "file1 file2 file3" | ./program

How to pass a very very long list of arguments to a program? 如何将非常长的参数列表传递给程序?

  1. place the arguments in a file 将参数放在文件中
  2. redirect the file to the standard input of the application, on startup 启动时将文件重定向到应用程序的标准输入

     bash$ echo "arg1 arg2 arg3 ... argn" >> inputs.txt bash$ ./yourProgram < inputs.txt 

This has the advantage of storing your arguments (so that for a subsequent execution, you only need to run the second line). 这具有存储您的参数的优点(因此,对于后续执行,您只需要运行第二行)。

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

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