从 C++ 设置系统变量
这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
查看
中的setenv:Look at setenv in
<cstdlib>
:你基本上不能这样做。
您可以在 中调用 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 isssh-agent -s
.您可以使用
putenv()
。这很方便,但是
putenv
不会复制字符串。这意味着如果您稍后修改它,那么您就修改了环境。对于文字来说这不是问题,但使用 std::string 形成字符串的方式与 putenv() 并不容易兼容。另一种选择是使用
setenv()
通过
setenv()
,将创建输入的副本,并且您可以在调用setenv( )
。You can use
putenv()
.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, usingstd::string
is not readily compatible withputenv()
.The alternative then is to use
setenv()
With
setenv()
, copies of the input are made and you are safe to dispose of the strings after callingsetenv()
.您想要的函数可能是
putenv()
。您没有指定您所在的操作系统,因此我假设使用 Linux,因为这是我方便使用的手册页: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:IIRC, there is a putenv on win32 as well. Finally you might try looking at this question, possibly a dup
上面的答案正确地解释了如何从 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.请注意,否则优秀的 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, onlygetenv
. This may not be a problem though as I could usesetenv
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.