bash echo (shell) 和 C 语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12453266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:17:16  来源:igfitidea点击:

echo (shell) and C language

c++cbashshell

提问by Toufik Moad

I have a program written in C language. In this program, I have an integer variable QS. The value of this variable changes during execution of the program In this program written in C, I want to display this variable QS using the echo command shell

我有一个用 C 语言编写的程序。在这个程序中,我有一个整数变量 QS。这个变量的值在程序执行过程中发生变化在这个用C编写的程序中,我想使用echo命令shell显示这个变量QS

I heard about the system () function of the diaper C gives hand to use the terminal or console

听说有尿布C的system()函数 手把手地用终端或控制台

system ("echo $ QS") when I do that, the variable QS is not recognized and it displays nothing.

system ("echo $ QS") 当我这样做时,变量 QS 不被识别,它什么也不显示。

Do you have any specific answers on how I could do this? and how to recognize the variable in C language?

你对我如何做到这一点有任何具体的答案吗?以及如何识别C语言中的变量?

I know that with a simple printf, I can display this variable, but what I want is to use echo.

我知道用一个简单的 printf,我可以显示这个变量,但我想要的是使用 echo。

Thank you in advance for your answers.

预先感谢您的回答。

回答by programmist

Shell can not access your program variable values. You have to set the value into the environment first. There are different ways to do this. One of them is (works in linux/unix)

Shell 无法访问您的程序变量值。您必须首先将值设置到环境中。有不同的方法可以做到这一点。其中之一是(适用于 linux/unix)

int main()  
{  
    char chProgramVar[] = "hello world" ;  
    setenv("ShellVar", chProgramVar, 1) ;  
    system("echo $ShellVar") ;  
}  

回答by Barmar

As others have said, the shell can't access your program variables. You can use sprintfto insert the value into the command that you will execute using system:

正如其他人所说,shell 无法访问您的程序变量。您可以使用sprintf将值插入到您将使用system以下命令执行的命令中:

char command[BUFSIZ];

sprintf(command, "echo %d", QS);
system(command);

回答by Adam Rosenfield

The shell you're spawning with the system(3)call has no idea about any of the variables in your C program. The easiest way to do what you want is to export your data as an environment variable. Since the environment is inherited by the call to system(3), you can just set the variable with setenv(3). However, you must first format it as a string:

您通过system(3)调用生成的外壳不知道您的 C 程序中的任何变量。做你想做的最简单的方法是将数据导出为环境变量。由于环境是由对 的调用继承的system(3),因此您只需使用 设置变量即可setenv(3)。但是,您必须首先将其格式化为字符串:

int QS = ...;
char QSStr[32];  // Longest integer string is INT_MIN = -2147483648, assuming
                 // that sizeof(int) == 4
snprintf(QSStr, sizeof(QSStr), "%d", QS);  // Convert to string
setenv("QS", QSStr);  // Set the environment variable)
...
system("echo $QS");  // Will print the value of QS

回答by sapht

You have to format the number as a character byte. The number "4" has a different integer representation as a printed character.

您必须将数字格式化为字符字节。数字“4”具有与打印字符不同的整数表示形式。

Use printf to print the number, or sprintf to reformat it as a character into a new buffer. Then you can print the new buffer using system echo (for whatever reason).

使用 printf 打印数字,或使用 sprintf 将其重新格式化为新缓冲区的字符。然后您可以使用系统回显(无论出于何种原因)打印新缓冲区。

char s[16]; // might be too small
sprintf(s, "%d", integer_var); 
// s is the string