环境变量相关函数:

getenv()和putenv()

代码示例【Linux程序设计(4th)_4.2小节配套代码】:

程序功能:编写一个程序来打印所选的任意环境变量的值;如果给程序传递第二个参数,还设置环境变量的值
// 1 The first few lines after the declaration of main ensure that the program, environ.c, has been called correctly. #include <stdlib.h>
#include <stdio.h>
#include <string.h> /************************
argc:参数个数(包含程序名)
argv:代表参数自身的字符串数组;
argv[0]为程序名,argv[1]为第1个实际参数 argv[2]为第2个实际参数
************************/ int main(int argc, char *argv[])
{
char *var, *value; if(argc == || argc > ) { //确保实际参数只有1个(argc=2)或2个(argc=3)
fprintf(stderr,"usage: environ var [value]\n");
exit();
} // 2 That done, we fetch the value of the variable from the environment, using getenv. var = argv[]; //第一个实际参数的参数名
value = getenv(var); //第一个实际参数的参数值
if(value) //判断参数值是否存在
printf("Variable %s has value %s\n", var, value);
else
printf("Variable %s has no value\n", var); // 3 Next, we check whether the program was called with a second argument. If it was, we set the variable to the value of that argument by constructing a string of the form name=value and then calling putenv. if(argc == ) { //如果第二个实际参数存在
char *string;
value = argv[]; //获取第2个参数的值
string = malloc(strlen(var)+strlen(value)+); //为第二个参数的“参数名=参数值”开辟空间(+2表示“=”和空格)
if(!string) { //如果开辟空间失败
fprintf(stderr,"out of memory\n");
exit();
}
strcpy(string,var);
strcat(string,"=");
strcat(string,value);
printf("Calling putenv with: %s\n",string);
if(putenv(string) != ) { //putenv()成功返回0.若环境变量设置失败
fprintf(stderr,"putenv failed\n");
free(string); //释放开辟的内存空间
exit();
} // 4 Finally, we discover the new value of the variable by calling getenv once again. value = getenv(var);
if(value)
printf("New value of %s is %s\n", var, value);
else
printf("New value of %s is null??\n", var);
}
exit();
}

注意:环境仅对程序本身有效。在程序里做的环境变量更改不会反映到外部环境,这是因为变量的值不会从子进程(你的程序)传播到父进程(shell)

linux 环境变量函数getenv()和putenv()的使用的更多相关文章

  1. Linux学习笔记之Linux环境变量总结

    0x00 概述 Linux是一个多用户多任务的操作系统,可以在Linux中为不同的用户设置不同的运行环境,具体做法是设置不同用户的环境变量. 0x01 Linux环境变量分类 按照生命周期来分,Lin ...

  2. Linux环境变量总结 转

    转自https://www.jianshu.com/p/ac2bc0ad3d74 Linux是一个多用户多任务的操作系统,可以在Linux中为不同的用户设置不同的运行环境,具体做法是设置不同用户的环境 ...

  3. 环境变量篇getenv putenv setenv

    getenv(取得环境变量内容) 相关函数 putenv,setenv,unsetenv 表头文件 #include<stdlib.h> 定义函数 char * getenv(const ...

  4. linux 环境变量【转】

    1.引言 在 linux系统 下,如果你下载并安装了应用程序,很有可能在键入它的名称时出现" command not found "的提示内容.如果每次都到安装目标文件夹内,找到可 ...

  5. 【转】linux环境变量设置

    1. 显示环境变量HOME $ echo $HOME /home/terry 2. 设置一个新的环境变量WELCOME $ export WELCOME="Hello!" $ ec ...

  6. Linux 环境变量和source命令 (转)

    可能是班门弄斧了,仅share给尚不知道的童鞋. 1.       问题的来源: 为什么我们编译Android代码时,需要输入:  source ./build/envsetup.sh  或者 . . ...

  7. Linux环境变量总结

    现在每天测试到时候会与Linux打交道,自然也会用到环境变量了.看了网上几篇文章,结合自己到实践和看法,总结以下Linux的环境变量吧.一.什么是环境变量?环境变量相当于给系统或用户应用程序设置的一些 ...

  8. LINUX 环境变量总结

    1.概述 Linux是一个多用户的操作系统.多用户意味着每个用户登录系统后,都有自己专用的运行环境.而这个环境是由一组变量所定义,这组变量被称为环境变量.用户可以对自己的环境变量进行修改以达到对环境的 ...

  9. linux环境变量 shell变量 command not found解决方法(转)

    在Ubuntu.centos中有如下几个文件可以设置环境变量1./etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文 ...

随机推荐

  1. 行为驱动:Cucumber + Selenium + Java(二) - extentreports 测试报告+jenkins持续集成

    1.extentreports 测试报告 pom文件 <dependency> <groupId>com.vimalselvam</groupId> <art ...

  2. mysql只删除表不删除库

    1.备份数据库mysqldump -uroot -P13306 -h10.1.50.14 -p 库名 > /opt/20190109.sql2.删除所有表mysql -uroot -P13306 ...

  3. css 初始化样式

    @charset "UTF-8"; /* reset */ html,body,div,h1,h2,h3,h4,h5,h6,p,dl,dt,dd,ol,ul,li,fieldset ...

  4. python计算文件的行数的方法

    1.简单方法把文件读入一个大的列表中,然后统计列表的长度.   count = len(open("文件名").readlines()) print count 2.读取文件某一行 ...

  5. 加载Assetbundle需要注意的地方

    WWW:异步实现,手机上不能用于同步代码,需要监测其完成状态.不用www.dispose. CreateFromFile:阻塞,但是移动平台上面的路径格式有点坑,没时间看,不用. 以下两个方式需要先使 ...

  6. 【Android】activity-alias的使用

    activity-alias是android里为了重复使用Activity而设计的. 当在Activity的onCreate()方法里,执行getIntent().getComponent().get ...

  7. C#批量裁剪图片

    有一批图片是全屏拷贝下来的,只需要保留指定区域的图片,用代码实现如下: Bitmap srcBmp = new Bitmap(fi.FullName); Bitmap dstBmp = srcBmp. ...

  8. git更新Activemq在远程github上指定版本的源码步骤

    第一步:根据地址克隆源码 (activemq-5.9) $  git  clone   https://github.com/apache/activemq.git 第二步:查看远程源码的版本清单 ( ...

  9. Promise源码深入理解

    ) ) }); ) }, ) }); ) ) }, ) }); ) }, ) }); p.then(function (x) { console.log(x) }) //输出 1 链式调用1 链式调用 ...

  10. 微信小程序--家庭记账本开发--06

    重要部分学习——记账簿 本次项目开发的目的主要是记账本的开发,最初自己想法简单,把家里的纸质记账簿变成手机上的记账簿.最终自己程序可以实现的功能可以记录每天的账目信息,并形成叠加效果,并按1.2.3… ...