linux 环境变量函数getenv()和putenv()的使用
环境变量相关函数:
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()的使用的更多相关文章
- Linux学习笔记之Linux环境变量总结
0x00 概述 Linux是一个多用户多任务的操作系统,可以在Linux中为不同的用户设置不同的运行环境,具体做法是设置不同用户的环境变量. 0x01 Linux环境变量分类 按照生命周期来分,Lin ...
- Linux环境变量总结 转
转自https://www.jianshu.com/p/ac2bc0ad3d74 Linux是一个多用户多任务的操作系统,可以在Linux中为不同的用户设置不同的运行环境,具体做法是设置不同用户的环境 ...
- 环境变量篇getenv putenv setenv
getenv(取得环境变量内容) 相关函数 putenv,setenv,unsetenv 表头文件 #include<stdlib.h> 定义函数 char * getenv(const ...
- linux 环境变量【转】
1.引言 在 linux系统 下,如果你下载并安装了应用程序,很有可能在键入它的名称时出现" command not found "的提示内容.如果每次都到安装目标文件夹内,找到可 ...
- 【转】linux环境变量设置
1. 显示环境变量HOME $ echo $HOME /home/terry 2. 设置一个新的环境变量WELCOME $ export WELCOME="Hello!" $ ec ...
- Linux 环境变量和source命令 (转)
可能是班门弄斧了,仅share给尚不知道的童鞋. 1. 问题的来源: 为什么我们编译Android代码时,需要输入: source ./build/envsetup.sh 或者 . . ...
- Linux环境变量总结
现在每天测试到时候会与Linux打交道,自然也会用到环境变量了.看了网上几篇文章,结合自己到实践和看法,总结以下Linux的环境变量吧.一.什么是环境变量?环境变量相当于给系统或用户应用程序设置的一些 ...
- LINUX 环境变量总结
1.概述 Linux是一个多用户的操作系统.多用户意味着每个用户登录系统后,都有自己专用的运行环境.而这个环境是由一组变量所定义,这组变量被称为环境变量.用户可以对自己的环境变量进行修改以达到对环境的 ...
- linux环境变量 shell变量 command not found解决方法(转)
在Ubuntu.centos中有如下几个文件可以设置环境变量1./etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文 ...
随机推荐
- 使用putty连接Ubuntu虚拟机,使用ssh方式访问
1 前言 Ubuntu14.04版本是可以直接连接的,没想到新装的Ubuntu18.04竟然没有默认安装ssh. 则安装一下open-ssh-server就可以的. 2 步骤 2.1 更新一下源 命令 ...
- Diango 框架起步
一.命令行搭建Django项目 安装django # 在指定解释器环境下安装django 1.11.9# 在真实python3环境下: pip3 install django==1.11.9# 在虚拟 ...
- (转)Vue种key的作用
https://blog.csdn.net/qq_41861679/article/details/80659278 https://cn.vuejs.org/v2/api/#key 其实不只是vue ...
- vue 3.0
参照网址: https://blog.csdn.net/qq_36407748/article/details/80739787
- Elasticsearch为记录添加时间戳timestamp
https://blog.csdn.net/peterwanghao/article/details/76577546
- Python与R的区别和联系
转自:http://bbs.pinggu.org/thread-3078817-1-1.html 有人说Python和R的区别是显而易见的,因为R是针对统计的,python是给程序员设计的,其实这话对 ...
- Python学习(三十八)—— Djago之Ajax
转载自:http://www.cnblogs.com/yuanchenqi/articles/7638956.html 一.Ajax准备知识:json 什么是json? 定义: JSON(JavaSc ...
- react-native 引入某些低三方库时出现的Command `run-android` unrecognized,命令不识别错误
在使用第三方库时react-native-swiper,执行 npm install react-native-swiper --save 再次运行react-native run-android时直 ...
- git使用习惯
1.每早一更新,提交前更新 git pull -u origin master: master(master为分支名称) 2.每晚一提交: git add . (注:别忘记后面的.此操作是把文件夹下面 ...
- Python开发实战PDF
Python开发实战(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1iP9VmwuzDMfdZTfpupR3CA 提取码:a523 复制这段内容后打开百度网盘手机A ...