【Linux系统编程】环境变量--1

简介: 【Linux系统编程】环境变量--1

介绍


       环境变量是一个系统预定义的全局参数,系统自己开辟空间,将它存在于所有的Shell中,具有继承性,可以存储有关shell会话和工作环境的信息。


       环境变量不是只有一个,而是一堆,是系统内具有特殊用途的变量。如:PATH、PWD、USER、HOME等。


环境变量的认识


       首先,要说明,每当启动系统时,环境变量就已被系统开辟完毕,我们可使用env指令进行查看系统下的所有环境变量以及环境变量对应的数据。


[zhujunhao@bogon code]$ env
........
USER=zhujunhao
PATH=.........
PWD=/home/zhujunhao/code
HOME=/home/zhujunhao
........


       查看单个环境变量的数据时,只需输入:echo 环境变量。如echo环境变量。如echo PATH、echo $PWD等。


[zhujunhao@bogon code]$ echo $USER
zhujunhao   
[zhujunhao@bogon code]$ echo $PWD
/home/zhujunhao/code

       我们也可以创建环境变量。在Linux中,export命令用于创建环境变量,unset删除环境变量。如export ENV=12,创建存放12的环境变量,unset ENV删除ENV环境变量。


[zhujunhao@bogon code]$ export ENV=12
[zhujunhao@bogon code]$ echo $ENV
12
[zhujunhao@bogon ~]$ unset HHH   //删除HHH环境变量


       每个环境变量对应的数据就是其功能的代表,它具有特殊的意义,下面会详细介绍其功能。


命令行参数


      在认识环境变量的功能前,我们来学习一下命令行参数。在Linux中,在我们启动的非图形化界面时的命令行时,实际上就是bash(或shell)给我们输出的命令行字符串,对应的小部分代码样例如下:


printf("[zhujunhao@bogon code]$ ");//输出指令行,不同机器的数据可能有些不同
char buffer[1024];//指令数组,存入相关指令的字符串
scanf("%s, buffer");//从输入流中输入指令


       该命令行还可以与我们写入的代码程序联系起来。在平常我们写入C/C++程序时,主函数中其实也可以有参数。main主函数中的参数是从bash指令行中维护、创建、传参,即这些参数通常在命令行中传递给程序,并可以通过main函数的参数进行访问。样例如下:


int main(int n, char* news[],char* S[]) //其中,第一个整型参数n表示命令行参数的数量。第二个参数news是一个指向命令行参数字符串数组的指针,命令行中输入的字符串都将放入里面。第三个参数S存放的就是系统下的环境变量

       我们先来观察前两个参数的使用。当我们在命令行中运行程序时,输入的参数将会传递给程序,下面的代码是依次访问输入数据的代码。如下:


[zhujunhao@bogon code]$ cat test.cpp
#include <iostream>
#include <cstdio>
using namespace std;
int main(int n, char* news[])
{
    /*int i = 0;
    for (i = 0; i < n; i++)
    {
        cout << "news[" << i << "]: " << news[i] << endl;
    }*/
    //以下效果一样
    for (int i = 0; news[i]; i++)
    {
        cout << "news[" << i << "]: " << news[i] << endl;
    }
    return 0;
}
[zhujunhao@bogon code]$ g++ -o test.exe test.cpp
//以下是输出指令行中的内容
[zhujunhao@bogon code]$ ./test.exe
news[0]: ./test.exe  
[zhujunhao@bogon code]$ ./test.exe -a -b -c -d -e
news[0]: ./test.exe
news[1]: -a
news[2]: -b
news[3]: -c
news[4]: -d
news[5]: -e


       这种使用通常在命令代码中最常见,通过不同选项,让相同的程序执行内部不同的功能。在bash的指令中,运用的其实就是上面的道理来实现命令逻辑,即命令行参数是指令选项的基础。


[zhujunhao@bogon code]$ cat code.cpp   //系统的源代码
#include <iostream>
#include <cstring>
using namespace std;
int main(int n, char *s[])
{
    if (n != 2)
    {
        cout << "Error:\n\tPlease Inoput" << endl;
    }
    else if (strcmp("-1", s[1]) == 0)
    {
        cout << "test.exe: -1" << endl;
    }
    else if (strcmp("-2", s[1]) == 0)
    {
        cout << "test.exe: -2" << endl;
    }
    else if (strcmp("-3", s[1]) == 0)
    {
        cout << "test.exe: -3" << endl;
    }
    else
    {
        cout << "unknow" << endl;
    }
    return 0;   
}
[zhujunhao@bogon code]$ g++ -o code.exe code.cpp
[zhujunhao@bogon code]$ ./code.exe
Error:
    Please Inoput
//下面的-1、-2、-3、-4可理解为对应设计的选项功能,而./code.exe为设计对应的命令
[zhujunhao@bogon code]$ ./code.exe -1 
test.exe: -1
[zhujunhao@bogon code]$ ./code.exe -2
test.exe: -2
[zhujunhao@bogon code]$ ./code.exe -3
test.exe: -3
[zhujunhao@bogon code]$ ./code.exe -4
unknow

       main函数中存放环境变量的第三个参数使用样例如下,该代码的功能是输出系统下的所有环境变量,也就是说,环境变量可以被子进程进行下去。


[zhujunhao@bogon code]$ cat test.cpp
//以下代码的S2[i]将会输出所有的环境变量
#include <iostream>
using namespace std;
int main(int n, char *S1[], char *S2[])
{
    for (int i = 0; S2[i]; i++)
    {
        cout << "S2[" << i << "]:  " << S2[i] << endl;
    }
    return 0;
}

【Linux系统编程】环境变量--2 https://developer.aliyun.com/article/1424730?spm=a2c6h.13148508.setting.17.214f4f0eXZtIwY

相关文章
|
3天前
|
IDE Linux 开发工具
Linux 系统上安装
在Linux和Mac上安装Lua 5.3.0只需下载源码,解压,编译和安装。Windows用户可选择SciTE IDE或通过LuaForWindows在Github或Google Code下载安装。创建 HelloWorld.lua,使用`lua HelloWorld.lua`运行显示&quot;Hello World!&quot;。另可参考LuaDist官方推荐方式安装。
|
1天前
|
运维 Linux
CentOS系统openssh-9,你会的还只有初级Linux运维工程师的技术吗
CentOS系统openssh-9,你会的还只有初级Linux运维工程师的技术吗
|
2天前
|
监控 JavaScript Linux
Linux系统之部署Homepage个人导航页
【5月更文挑战第13天】Linux系统之部署Homepage个人导航页
21 1
|
3天前
|
监控 JavaScript 网络协议
Linux系统之安装uptime-kuma服务器监控面板
【5月更文挑战第12天】Linux系统之安装uptime-kuma服务器监控面板
14 0
|
3天前
|
Linux C语言 调度
|
3天前
|
Linux Perl
Linux系统的文本处理
Linux系统的文本处理
|
3天前
|
Linux API
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
|
3天前
|
编解码 Ubuntu Linux
|
3天前
|
Linux
|
3天前
|
Linux
请问如何在Linux系统中删除一个磁盘分区
请问如何在Linux系统中删除一个磁盘分区