环境变量相关函数:

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. remote connect to ubuntu unity

    https://community.nxp.com/thread/220596 putty secure copy protocol can be used to transfer file amon ...

  2. 【原创】大数据基础之Spark(4)RDD原理及代码解析

    一 简介 spark核心是RDD,官方文档地址:https://spark.apache.org/docs/latest/rdd-programming-guide.html#resilient-di ...

  3. js-分享功能插件

    soshm 分享功能插件 地市:https://github.com/calledT/soshm yarn 安装:yarn add soshm -s;  引入:import soshm from ‘s ...

  4. django中静态文件的配置路径

    一  先找到配置文件 二  将配置文件添加上(注意名字一定要大写)

  5. OrCAD Capture CIS 16.6 为原理图中的Off-Page Connector添加页面编号

    操作系统:Windows 10 x64 工具1:OrCAD Capture CIS 16.6-S062 (v16-6-112FF) 为原理图中的Off-Page Connector添加页面编号 一般来 ...

  6. python 的bif

    函数在类里面就叫做方法from sys import pi 导入sys模块里面的pi方法import sys as system 命名为新的名字dir(sys) 查看sys模块里面都提供了哪些东西he ...

  7. 2017-2018-2 20155309南皓芯 Exp4 恶意代码分析

    实验后回答问题 (1)如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控. 答:我会使用sysmon工具来进行监控 ...

  8. 视频转码成mp4格式,添加关键帧,添加元数据,把元数据放在第一帧,可拖动

    作者测试是在windows下使用,所以下载的页面地址是: http://ffmpeg.zeranoe.com/builds/点击页面上的Download FFmpeg git-738ebb4 64-b ...

  9. 作用域和闭包(二)this

    this 要在执行时才确认,定义时无法确认 1. 作为构造函数执行 2. 作为对象属性执行 3.作为普通函数执行 4. call,apply,bind 改变this

  10. SVN-您的主机中的软件中止了一个已建立的连接

    关于这个问题,网络上有各种解决的办法,关闭防火墙,HTTP/HTTPS切换,改端口... ...但我都试了没有用.本来一直用的好好的,突然就出现了这个问题,而且在几分钟前都是正常的.下面来说说我都干了 ...