从 C++ 设置系统变量

发布于 2024-12-17 15:51:51 字数 529 浏览 0 评论 0原文

这个 shell 脚本

#!/bin/csh
set VAR=12345
echo $VAR

将在 shell 中平静地输出 12345。 我需要使用 C++ 在代码的某些部分执行相同的操作:

string str = "12345";
retValue="set var1= "+str;      
system(retValue1.c_str());
system("echo $var1");

这不会创建系统变量 var1 并回显 null,这是可以理解的,因为每个系统函数都会创建具有不同环境变量的子进程。因此,我仅使用一个系统函数将它们组合起来......但它再次回显 null。

retValue="set var1= "+str;
retValue1=retValue+";\n echo $var1";
system(retValue1.c_str());

有人可以指导我通过 C++ 设置系统变量吗? 预先非常感谢!

This shell script

#!/bin/csh
set VAR=12345
echo $VAR

will peacefully give the output 12345 at shell.
I need to use C++ to do the same in some part of the code:

string str = "12345";
retValue="set var1= "+str;      
system(retValue1.c_str());
system("echo $var1");

This doesn't create a system variable var1 and echos null which is understandable as each system function would create a child process with different environment variables. So I combine them as follows using just one system function...but it echos null again.

retValue="set var1= "+str;
retValue1=retValue+";\n echo $var1";
system(retValue1.c_str());

Can someone please guide me to set up the system variable thru C++.
Thanks a lot in advance!

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

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

发布评论

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

评论(6

妄司 2024-12-24 15:51:52

查看中的setenv

#include <cstdlib>

setenv("VAR", "12345", true);

Look at setenv in <cstdlib>:

#include <cstdlib>

setenv("VAR", "12345", true);
十级心震 2024-12-24 15:51:52

你基本上不能这样做。

您可以在 中调用 putenv更改环境变量您自己的进程以及所有未来的子进程,但是没有办法(这很好)更改 shell 的环境过程。

您可以为您的 C++ 程序制定一个使用约定,例如,它输出一些由用户获取(或eval-ed)的 shell 命令。一个例子是ssh-agent -s

You basically can't do that.

You could call putenv to change environment variables in your own process and in all future children processes, but there is no way (and this is good) to change the environment of the parent shell process.

You could have a use convention for your C++ program that e.g. it is outputting some shell commands to be sourced (or eval-ed) by the user. An example of this is ssh-agent -s.

嗳卜坏 2024-12-24 15:51:52

您可以使用putenv()

#include <cstdlib>
...
putenv("VAR=12345");

这很方便,但是 putenv 不会复制字符串。这意味着如果您稍后修改它,那么您就修改了环境。对于文字来说这不是问题,但使用 std::string 形成字符串的方式与 putenv() 并不容易兼容。

另一种选择是使用 setenv()

#include <cstdlib>
...
setenv("VAR", "12345", true);

通过 setenv(),将创建​​输入的副本,并且您可以在调用 setenv( )

You can use putenv().

#include <cstdlib>
...
putenv("VAR=12345");

This is very convenient, but the string is not copied by putenv. This means that if you modify it later, then you modify the environment. That's not an issue for a literal, but the way you are forming your strings, using std::string is not readily compatible with putenv().

The alternative then is to use setenv()

#include <cstdlib>
...
setenv("VAR", "12345", true);

With setenv(), copies of the input are made and you are safe to dispose of the strings after calling setenv().

愁杀 2024-12-24 15:51:52

您想要的函数可能是 putenv()。您没有指定您所在的操作系统,因此我假设使用 Linux,因为这是我方便使用的手册页:

int putenv(char *string);

putenv()函数添加或更改环境值
变量。参数字符串的形式为名称=值。如果名字
环境中尚不存在,则将字符串添加到
环境。如果 name 确实存在,则 name 的值
环境变为值。 string 所指向的字符串
成为环境的一部分,因此改变字符串会改变
环境。

IIRC,win32 上也有一个 putenv。最后,您可以尝试查看这个问题,可能是重复的

The function you want is probably putenv(). You didn't specify which OS you are on, so I'll assume Linux because that's the man page I have handy:

int putenv(char *string);

The putenv() function adds or changes the value of environment
variables. The argument string is of the form name=value. If name
does not already exist in the environment, then string is added to the
environment. If name does exist, then the value of name in the
environment is changed to value. The string pointed to by string
becomes part of the environment, so altering the string changes the
environment.

IIRC, there is a putenv on win32 as well. Finally you might try looking at this question, possibly a dup

盛夏已如深秋| 2024-12-24 15:51:52

上面的答案正确地解释了如何从 C++ 程序设置环境变量,这基本上是 setenv()

我想说的唯一一点是为什么你的方法不'不工作吗?原因是,当加载进程时,system 命令会加载新的上下文 - 相当于一个新的 shell。实际上,您的环境变量正在设置,但当您回来时它会丢失。

请参阅此 http://pubs.opengroup.org/onlinepubs/007904975/functions/ setenv.html

事实上,setenv() 设置进程的环境变量!这就是为什么它对你有用。

Above answers explains correctly as to how to set environment variable from a C++ program which is basically setenv()

The only other point i wanted to make is that why your approach doesn't work? The reason is, that when a process is loaded, the system command is loaded with new context - an equivalent of a new shell. Actually your environment variable is getting set but when you comeback it is lost.

Refer to this http://pubs.opengroup.org/onlinepubs/007904975/functions/setenv.html.

Infact, the setenv() sets the env variables of parent process! Which is why it works for you.

迟到的我 2024-12-24 15:51:52

请注意,否则优秀的 CPPreference site 似乎没有提及 setenv 标头中可用的函数中,只有 getenv。这可能不是问题,因为我可以在带有 GCC 9.1 的 Centos 7 系统上通过 #include-ing 使用 setenv 。我怀疑在大多数情况下 只是 的一个薄包装。

另一个需要注意的小事情是 setenv 采用 C 风格的字符参数(也用于要设置的环境变量的)。如果您使用 C++ 字符串(您应该这样做),请不要忘记使用其 .c_str() 方法来转换它们。

Note that the otherwise excellent CPPreference site does not seem to mention setenv among the functions available in the <cstdlib> header, only getenv. This may not be a problem though as I could use setenv by #include-ing <cstdlib> on a Centos 7 system with GCC 9.1. I suspect in most cases <cstdlib> is just a thin wrapper around <stdlib.h>.

Another small thing to note is that setenv takes C-style character arguments (also for the value of the environment variable to be set). If you use C++ strings (as you should), don't forget to convert them using their .c_str() method.

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