简体   繁体   English

将多个值从Shell脚本返回到C程序

[英]Returning multiple values from shell script to c program

I am calling a shell script from ac program. 我正在从AC程序调用Shell脚本。 I want this script to return multiple values which I want to store in a structure of my c program. 我希望此脚本返回要存储在c程序结构中的多个值。 For instance say 'ac' calls 'a.sh', I want 'a.sh' to return the values to be stored in variables 'x' and 'y' which are a part of struct 's' in 'a.c'. 例如说“ ac”调用“ a.sh”,我希望“ a.sh”返回要存储在变量“ x”和“ y”中的值,这些变量是“ a.c”中结构“ s”的一部分'。

You cannot "return" values from a shell script, other than the single integer (often limited to 8 bits) of exit status that processes have (assuming a Unix-like system, now). 除了进程具有的退出状态的单个整数(通常限制为8位)之外,您不能从Shell脚本“返回”值(现在假定为类Unix系统)。

What you typically do is interpret the standard output of the external program, which you can easily read into your C program by using popen() to run the shell script. 通常,您要做的是解释外部程序的标准输出 ,您可以通过使用popen()运行shell脚本轻松地将其读入C程序。

You can transfer any amount of data using this approach. 您可以使用这种方法传输任何数量的数据。

Not the most elegant solution (but very quick and easy to implement) but you could always produce an output file from the shell script and then parse that file from the C program. 这不是最优雅的解决方案(但是非常容易实现),但是您总是可以从Shell脚本生成输出文件,然后从C程序解析该文件。 This has the benefit that you can store the output values of the shell script between executions of your C program, useful if those output values do not change every time. 这样的好处是,您可以在C程序的执行之间存储shell脚本的输出值,如果这些输出值不会每次都更改,则很有用。

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

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