C语言之简单的获取命令行参数和环境变量
C语言之简单的获取命令行参数和环境变量
本人的开发环境为WIN10操作系统用VMWARE虚拟的UBUNTU LINUX 18.04LTS!!!
所有代码的编辑、编译、运行都在虚拟机上操作,初学的朋友要注意这一点!!!
详细情况如下:
songvm@ubuntu:~/works/xdn/boo$ uname -a
Linux ubuntu 5.4.0-150-generic #167~18.04.1-Ubuntu SMP Wed May 24 00:51:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
命令行参数
- 常见C语言main函数带有两个参数:整型的argc和字符串指针型的argv。
- 这两个参数就是命令行参数相关的参数
- 整型 argc 保存命令行参数的数量
- 字符串指针 argv 保存命令行参数的值
- 命令行参数要用空格隔开!!!
- 测试代码如下:
/* filename: opt.c */
#include <stdio.h>
/**/
int
main (int argc, char *argv[])
{
printf ("Argument count : %d\n", argc);
printf ("Command is [%s]\n", argv[0]);
return 0;
}
编译运行,输出结果如下:
songvm@ubuntu:~/works/xdn/boo$ gcc opt.c -o opt
songvm@ubuntu:~/works/xdn/boo$ ./opt
Argument count : 1
Command is [./opt]
songvm@ubuntu:~/works/xdn/boo$
带参数测试
- 上面的结果可见参数总量为1,字符串数组引用索引为0,即argv[0],也就是命令本身!
- 加上两个参数测试,代码如下:
/* filename: opt.c */
#include <stdio.h>
/**/
int
main (int argc, char *argv[])
{
printf ("Argument count : %d\n", argc);
printf ("Command is [%s]\n", argv[0]);
for (int i = 0; i < argc; i++)
printf ("Argument %d is [%s]\n", i, argv[i]);
return 0;
}
编译运行,输出结果如下:
songvm@ubuntu:~/works/xdn/boo$ ./opt -s zoo.t
Argument count : 3
Command is [./opt]
Argument 0 is [./opt]
Argument 1 is [-s]
Argument 2 is [zoo.t]
songvm@ubuntu:~/works/xdn/boo$
提示加命令行参数
- 事实上你可以加任意命令行参数,然后通过循环输出!!!
- 如果没有参数需要要提示用加上参数!!!
- 测试代码如下:
/* filename: opt.c */
#include <stdio.h>
/**/
int
main (int argc, char *argv[])
{
printf ("Argument count : %d\n", argc);
printf ("Command is [%s]\n", argv[0]);
for (int i = 0; i < argc; i++)
printf ("Argument %d is [%s]\n", i, argv[i]);
if (argc == 1)
printf ("Please add some arguments!\n");
return 0;
}
编译运行,输出提示信息:
songvm@ubuntu:~/works/xdn/boo$ gcc opt.c -o opt
songvm@ubuntu:~/works/xdn/boo$ ./opt
Argument count : 1
Command is [./opt]
Argument 0 is [./opt]
Please add some arguments!
songvm@ubuntu:~/works/xdn/boo$
事实上main函数还有第三个参数envp
- 字符串指针envp保存系统的环境变量!!!
- 先读出一个环境变量看看什么情况,代码如下:
/* filename: opt.c */
#include <stdio.h>
/**/
int
main (int argc, char *argv[], char *envp[])
{
printf ("Environment Variables\n");
printf ("%s\n", envp[0]);
return 0;
}
编译运行,输出如下:
songvm@ubuntu:~/works/xdn/boo$ gcc opt.c -o opt
songvm@ubuntu:~/works/xdn/boo$ ./opt
Environment Variables
CLUTTER_IM_MODULE=xim
songvm@ubuntu:~/works/xdn/boo$
输出所有环境变量
- 不同编译运行环境可能输出的结果不同!!!
- 显示的是:CLUTTER_IM_MODULE=xim
- 环境变量名:CLUTTER_IM_MODULE,输入法模块,IM -> input method
- 环境变量值:xim ,Linux下的输入法工具,包含拼音和五笔等
- 可能是安装系统时最后安装设置的输入法,所以首先显示的是这一个环境变量!
- 试一试输出所有环境变量,代码如下:
/* filename: opt.c */
#include <stdio.h>
/**/
int
main (int argc, char *argv[], char *envp[])
{
printf ("Environment Variables\n");
printf ("%s\n", envp[0]);
int i = 0;
while (envp[i] != NULL)
{
printf ("%s\n", envp[i]);
i++;
}
return 0;
}
编译运行,输出如下:
songvm@ubuntu:~/works/xdn/boo$ gcc opt.c -o opt
songvm@ubuntu:~/works/xdn/boo$ ./opt
Environment Variables
CLUTTER_IM_MODULE=xim
CLUTTER_IM_MODULE=xim
LS_COLORS=
LC_MEASUREMENT=zh_CN.UTF-8
LESSCLOSE=/usr/bin/lesspipe %s %s
LC_PAPER=zh_CN.UTF-8
LC_MONETARY=zh_CN.UTF-8
XDG_MENU_PREFIX=gnome-
LANG=zh_CN.UTF-8
DISPLAY=:0
OLDPWD=/home/songvm/works/xdn
GNOME_SHELL_SESSION_MODE=ubuntu
USERNAME=songvm
INSIDE_EMACS=25.2.2,comint
XDG_VTNR=2
GIO_LAUNCHED_DESKTOP_FILE_PID=6401
SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
LC_NAME=zh_CN.UTF-8
XDG_SESSION_ID=2
USER=songvm
DESKTOP_SESSION=ubuntu
QT4_IM_MODULE=xim
TEXTDOMAINDIR=/usr/share/locale/
PWD=/home/songvm/works/xdn/boo
HOME=/home/songvm
TEXTDOMAIN=im-config
SSH_AGENT_PID=1535
QT_ACCESSIBILITY=1
XDG_SESSION_TYPE=x11
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
XDG_SESSION_DESKTOP=ubuntu
LC_ADDRESS=zh_CN.UTF-8
GJS_DEBUG_OUTPUT=stderr
LC_NUMERIC=zh_CN.UTF-8
GTK_MODULES=gail:atk-bridge
COLUMNS=80
PAPERSIZE=a4
WINDOWPATH=2
SHELL=/bin/bash
TERM=dumb
QT_IM_MODULE=xim
XMODIFIERS=@im=ibus
IM_CONFIG_PHASE=2
XDG_CURRENT_DESKTOP=ubuntu:GNOME
GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1
GIO_LAUNCHED_DESKTOP_FILE=/usr/share/applications/emacs25.desktop
XDG_SEAT=seat0
SHLVL=1
LANGUAGE=zh_CN:en
LC_TELEPHONE=zh_CN.UTF-8
GDMSESSION=ubuntu
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
LOGNAME=songvm
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
XDG_RUNTIME_DIR=/run/user/1000
XAUTHORITY=/run/user/1000/gdm/Xauthority
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
LC_IDENTIFICATION=zh_CN.UTF-8
GJS_DEBUG_TOPICS=JS ERROR;JS LOG
TERMCAP=
SESSION_MANAGER=local/ubuntu:@/tmp/.ICE-unix/1458,unix/ubuntu:/tmp/.ICE-unix/1458
LESSOPEN=| /usr/bin/lesspipe %s
GTK_IM_MODULE=ibus
LC_TIME=zh_CN.UTF-8
_=./opt
songvm@ubuntu:~/works/xdn/boo$
获取某一环境变量的值
- 以上输出内容挺多,在这N个环境变量中,如何取单独的某一个环境变量呢?如常用的:PATH
- getenv函数,在stdlib.h头文件中定义,测试代码如下:
/* filename: opt.c */
#include <stdio.h>
#include <stdlib.h>
/**/
int
main (int argc, char *argv[], char *envp[])
{
printf ("Get environ variable PATH : [%s]\n", getenv("PATH"));
return 0;
}
编译运行,达到预期,输出结果如下:
songvm@ubuntu:~/works/xdn/boo$ ./opt
Get environ variable PATH : [/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]
songvm@ubuntu:~/works/xdn/boo$
测试一下内存分配情况,发现分配和释放了一次内存,无疑是getenv函数所为
songvm@ubuntu:~/works/xdn/boo$ valgrind --leak-check=yes ./opt
==6975== Memcheck, a memory error detector
==6975== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6975== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==6975== Command: ./opt
==6975==
Get environ variable PATH : [/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]
==6975==
==6975== HEAP SUMMARY:
==6975== in use at exit: 0 bytes in 0 blocks
==6975== total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==6975==
==6975== All heap blocks were freed -- no leaks are possible
==6975==
==6975== For counts of detected and suppressed errors, rerun with: -v
==6975== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
songvm@ubuntu:~/works/xdn/boo$
- 命令行参数的解析相对复杂一些,以后再发博文讲解!!!