命令行参数
我们知道中Linux命令中许多命令都是有对应的选项的,不同的选项对应不同的功能。那这个操作是如何实现的呢?
main函数参数
首先,让我们先来讲讲main函数。mian函数可以说是我们接触编程的第一步,但许多人可以对main函数并不了解。 main函数其实是可以有参数的。
代码语言:javascript
AI代码解释
int main(int argc,char* argv[])第一个参数,argc:表示命名行参数的数量(包括程序名本身)
第二个参数,argv:是一个字符指针数组,储存了命令行中的每个参数
验证:
代码语言:javascript
AI代码解释
#include<stdio.h> int main(int argc,char* argv[]) { for(int i=0;i<argc;i++) { printf("%s\n",argv[i]); } }实现指令选项
代码语言:javascript
AI代码解释
#include<stdio.h> #include<string.h> int main(int argc,char* argv[]) { if(argc==2) { char* x=argv[1]; if(strcmp(x,"-a")==0) printf("执行-a功能\n"); if(strcmp(x,"-b")==0) printf("执行-b功能\n"); if(strcmp(x,"-c")==0) printf("执行-c功能\n"); } else printf("请使用:-a/-b/-c\n"); }进程中有一张表:argv表(命令行参数表),利用此表则可以实现命令的选项的功能。
认识环境变量
首先我们要执行一个程序,必须先找到它 而环境变量可以帮助我们找到其位置
使用env指令我们可以查看系统中所有的环境变量,也可以使用echo $名称,打印对应的环境变量
环境变量的格式为:名称=内容
PATH
我们先来认识一下PATH环境变量。
PATH环境变量表示查找指令位置的默认路径,比如当我们执行指令:ls、cd等等指令时,bash就会使用PATH环境变量去找到指令对应的程序并执行。
我们知道系统指令的实现也是C语言,但为什么系统自己的指令可以直接执行,而我们自己写的代码却必须要 “./” 作为前缀才可以执行,PATH环境变量就是关键。
理解两个问题:
1.环境变量是如何存储的呢?
bash下会形成一个环境变量表,存放各种各样的环境变量。(上面我们也讲到了bash中有一个命令行参数表,也是就是bash下有两张表)
2.环境变量一开始是从哪里来的?
bash下的环境变量表是重系统配置文件中来的(以下便是配置文件)。
认识更多环境变量
HOME
HOME环境变量,记录的是当前用户的家目录路径
代码语言:javascript
AI代码解释
root@hcss-ecs-4ce7:~# echo $HOME /rootSHELL
表示当前使用的shell版本
代码语言:javascript
AI代码解释
root@hcss-ecs-4ce7:~# echo $SHELL /bin/bashUSER
表示当前用户是谁
代码语言:javascript
AI代码解释
root@hcss-ecs-4ce7:~# echo $USER rootHISTSIZE
表示Linux中记录历史命令的最大数量(既Linux的历史命令最多记录HISTSIZE条)
代码语言:javascript
AI代码解释
root@hcss-ecs-4ce7:~# echo $HISTSIZE 1000HOSTNAME
表示当前主机的主机名
代码语言:javascript
AI代码解释
root@hcss-ecs-4ce7:~# echo $HOSTNAME hcss-ecs-4ce7PWD
记录当前所在的工作路径
代码语言:javascript
AI代码解释
hyc@hcss-ecs-4ce7:~/linux/progress$ echo $PWD /home/hyc/linux/progressOLDPWD
记录上一次的工作路径
代码语言:javascript
AI代码解释
hyc@hcss-ecs-4ce7:~/linux/progress$ echo $OLDPWD /home/hyc/linux环境变量的获取方式
指令
env:查看全部的环境变量
echo $xxx:查看某个环境变量
expot:添加环境变量
unset:删除环境变量
代码
调用系统调用:getenv获取环境变量
头文件为:#include<stdlib.h>
返回值:获取成功返回指针,失败返回NULL
代码语言:javascript
AI代码解释
#include<stdio.h> #include <stdlib.h> int main() { char* str=getenv("PATH"); if(!str) printf("获取失败"); else printf("%s\n",str); }环境变量特性
1.环境变量具有全局性,可以被子进程继承,同一个bash下的进程都可以访问。
2.存在本地变量,只能在bash内部使用。如下:i 就是本地变量。
3.export添加环境变量,是添加在bash中的,再子进程继承达到全局可用。
export命令与其他命令不同:export命令是内键命令:既不需要创建子进程,bash自己调用函数执行(或系统调用完成)
https://www.dongchedi.com/article/7600082197533737496
https://www.dongchedi.com/article/7600081551585788478
https://www.dongchedi.com/article/7600080627626836505
https://www.dongchedi.com/article/7600076432072786494
https://www.dongchedi.com/article/7600077785914540569
https://www.dongchedi.com/article/7600076654119207449
https://www.dongchedi.com/article/7600076084386021950
https://www.dongchedi.com/article/7600077060547084862
https://www.dongchedi.com/article/7600075783578763800
https://www.dongchedi.com/article/7600075136489783833
https://www.dongchedi.com/article/7600073513009529369
https://www.dongchedi.com/article/7600074164837761598
https://www.dongchedi.com/article/7600073368318542361
https://www.dongchedi.com/article/7600073001341993534
https://www.dongchedi.com/article/7600073974269329982
https://www.dongchedi.com/article/7600074487064805912
https://www.dongchedi.com/article/7600073227586683454
https://www.dongchedi.com/article/7600069482082386457
https://www.dongchedi.com/article/7600071292192260670
https://www.dongchedi.com/article/7600069215726977561
https://www.dongchedi.com/article/7600071094875537982
https://www.dongchedi.com/article/7600070748635939353
https://www.dongchedi.com/article/7600069587262308926
https://www.dongchedi.com/article/7600069571290087961
https://www.dongchedi.com/article/7600069625426395673
https://www.dongchedi.com/article/7600069065444704793
https://www.dongchedi.com/article/7600070228240138814
https://www.dongchedi.com/article/7600069571290645017
https://www.dongchedi.com/article/7600068352702448153
https://www.dongchedi.com/article/7600069299780813374
https://www.dongchedi.com/article/7600070076465070654
https://www.dongchedi.com/article/7600070076464808510
https://www.dongchedi.com/article/7600069722168148504
https://www.dongchedi.com/article/7600068030437032510
https://www.dongchedi.com/article/7600068903506723352
https://www.dongchedi.com/article/7600069051742044697
https://www.dongchedi.com/article/7600067161435980313
https://www.dongchedi.com/article/7600067215119450648
https://www.dongchedi.com/article/7600066283626922521
https://www.dongchedi.com/article/7600067654162367000
https://www.dongchedi.com/article/7600067338523640382
https://www.dongchedi.com/article/7600061876546290238
https://www.dongchedi.com/article/7600062394333037081
https://www.dongchedi.com/article/7600064751125168702
https://www.dongchedi.com/article/7600062857611199000
https://www.dongchedi.com/article/7600064428301877822
https://www.dongchedi.com/article/7600065673062187544
https://www.dongchedi.com/article/7600061447326286360
https://www.dongchedi.com/article/7600064204875727422
https://www.dongchedi.com/article/7600063689303409214
https://www.dongchedi.com/article/7600063881645883928
https://www.dongchedi.com/article/7600061290274980376
https://www.dongchedi.com/article/7600063037474652696
https://www.dongchedi.com/article/7600058391884956222
https://www.dongchedi.com/article/7600058999849091609
https://www.dongchedi.com/article/7600054609557619224
https://www.dongchedi.com/article/7600058346296918590
https://www.dongchedi.com/article/7600062930226741822